pdflib 9 PDF_add_textflow 不分隔前两行

pdflib 9 PDF_add_textflow doesn't separate first 2 lines

关于 PDFLib 的另一个问题。

当我尝试从以下 class 调用 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;

    }

}

函数调用发生在以下 class 中,其中值作为值数组提供: "Datum:", "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中如下所示 Datum:Auftrags-NR: 标签名称: Kunden-Nr:

我已经为此苦苦挣扎了一段时间,但无论我如何更改代码,它仍然以两个顶行结尾:-(

再次感谢。

I've been struggling with this for a while now, but no matter how I change the code it still ends up with two top line together :-(

我从你的代码中看不出你在文本段之间添加了某种换行符。因此,当一行中的 space 足以容纳多个块时,您将在一行中获得文本。

如果你想有一个换行符,那么添加一个换行符(例如通过在 $line 添加 \n)。

请注意:不必以 create_textflow() 开头。当您应用为文本流句柄“0”(PHP,否则为 -1)时,您还可以使用 add_textflow() 创建新的文本流句柄。