关于PDFLib的另一个问题。
当我尝试从以下类调用setupMultilineTextflow()函数时,前两行文本未分开
// Abstract PDF class, holding generalised functions and
// intialising a new PDF file
//namespace synergetic\realscan-middleware-pdflib;
class AbstractPDF{
protected $pdf;
protected $searchpath;
protected $fontName;
public function __construct(){
$this->pdf = PDF_new();
$this->searchpath = "fonts/";
/*
pdf_set_parameter($this->pdf,"errorpolicy", "return"); // check return values of load_font() etc.
pdf_set_parameter($this->pdf,"hypertextencoding", "winansi"); // used to prevent problems with japanese systems
pdf_set_parameter($this->pdf,"SearchPath", $searchpath); // **set search path parameter in pdf
*/
if (pdf_begin_document($this->pdf,"", "") == 0) {
die("Error: " . pdf_get_errmsg($this->pdf));
}
pdf_set_option($this->pdf,"errorpolicy=return");
pdf_set_option($this->pdf,"searchpath={" . $this->searchpath . "}");
pdf_set_option($this->pdf,"stringformat=utf8");
}
protected function startAFourPage(){
pdf_begin_page_ext($this->pdf, 0, 0, "width=a4.width height=a4.height");
}
protected function startPage($format){
switch (true){
case($format="a4"):
pdf_begin_page_ext($this->pdf, 0, 0, "width=a4.width height=a4.height");
break;
/*
case("usletter"):
//pdf_begin_page_ext($this->pdf, 0, 0, "width=a4.width height=a4.height");
break;
*/
}
}
// When setting up any of the PDF content types, one should
// remember that in PDFLib, x=>y axis start with 0(zero) at
// lower left corner.
// The text line is set up in space by setting up the
// coordinates of the lower left corner and then providing
// height and width of the object as separate values
protected function setupTextLine($xcoordinate, $ycoordinate, $width, $height,
$fontName, $fontEncoding, $fontSize, $text, $textPosition = "left"){
//adding text directly through the PDFLib documentation
$font = PDF_load_font($this->pdf, $fontName, $fontEncoding, "");
PDF_setfont($this->pdf, $font, $fontSize);
//PDF_set_text_pos($this->pdf, 25, 650);
//PDF_show($this->pdf, $text);
PDF_fit_textline ($this->pdf, $text, $xcoordinate, $ycoordinate, "boxsize {".$width." ".$height."} position=left");
}
// When setting up any of the PDF content types, one should
// remember that in PDFLib, x=>y axis start with 0(zero) at
// lower left corner.
// The text flow is set up by providing the coordinate for
// lower left corner and upper right, as a rule.
// But overall PDFLib will placed it by coordinates for
// two corners diagonal to each other.
// For this class we will identify these corners as
// lowLeft and upperRight
protected function setupMultilineTextflow($lowLeftX, $lowLeftY, $upperRightX, $upperRightY,
$fontName, $fontEncoding, $fontSize, $text){
//two types of text can be submitted string and array
if (gettype($text) == "array"){
foreach ($text as $line){
if ($count == 0){
$textFlow = PDF_create_textflow($this->pdf, $line,
"fontname=".$fontName."
fontsize=".$fontSize."
encoding=".$fontEncoding);
}
else{
$textFlow = PDF_add_textflow($this->pdf, $textFlow, $line,
"fontname=".$fontName."
fontsize=".$fontSize."
encoding=".$fontEncoding);
}
$count++;
}
}
else{
$textFlow = PDF_create_textflow($this->pdf, $line,
"fontname=".$fontName."
fontsize=".$fontSize."
encoding=".$fontEncoding);
}
PDF_fit_textflow($this->pdf, $textFlow, $lowLeftX, $lowLeftY, $upperRightX, $upperRightY,"");
}
protected function setupTable($headers=array('test'=>''), array $field){
}
protected function endDocument(){
//this is fo the end of the document
pdf_end_page_ext($this->pdf,"");
pdf_end_document($this->pdf,"");
//exit;
$data = pdf_get_buffer($this->pdf);
$len = strlen($data);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=hello.pdf");
print $data;
$this->pdf = 0;
}
}
对函数的调用发生在以下类中,其中值作为值数组提供: “基准:”, “Auftrags-NR:” “Auftragsname:” “Kunden-NR:”
require 'AbstractPDF.php';
class Lieferschein extends AbstractPDF{
public function __construct(){
parent::__construct();
}
public function generateContent(){
//set up general order set fields
$orderDetailNames = array("Datum:", "Auftrags-NR:", "Auftragsname:", "Kunden-Nr:");
$fontName = "ZEISSFrutigerNextW1G-Light";
parent::setupMultilineTextflow(111, 350, 191, 520,
$fontName, "unicode", 9, $orderDetailNames);
parent:: endDocument();
return "";
}
}
调用结果在pdf中如下所示 基准:Auftrags-NR: Auftragsname: Kunden-NR:
我一直在努力解决这个问题,但无论我如何更改代码,它仍然最终会有两个顶线: - (
再次感谢一群人。
答案 0 :(得分:1)
我一直在努力解决这个问题,但无论我如何 改变它仍然最终有两个顶线的代码: - (
我无法从您的代码中看到您在文本段之间添加某种换行符。因此,当行具有足够的空间以容纳多个块时,您将在单行中获取文本。
如果您想要换行符,请添加换行符(例如在$行添加\n
)。
只需注意一点:没有必要从create_textflow()开始。当您应用为文本流句柄“0”(PHP,否则为-1)时,也可以使用add_textflow()创建新的文本流句柄。