Search code examples
phppdflib

pdflib 9 PDF_add_textflow doesn't separate first 2 lines


Yet another question in regards to PDFLib.

When I am trying to call setupMultilineTextflow() function from the following class, the first two lines of text are not separated

// 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;

    }

}

The call to the function happens in the following class, where the values are being supplied as an array of values: "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 "";

    }


}

The result of the call look as follows in the pdf Datum:Auftrags-NR: Auftragsname: 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 :-(

Thanks a bunch again.


Solution

  • 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 :-(

    I can't see from your code that you add some kind of linebreak between the text segments. As a result you will get the text in a single line when the line has enough space for more than one chunk.

    If you want to have a line-break then add a linebreak (for example by adding \n at $line).

    Just one note: It's not necessary to start with create_textflow(). You can also create a new textflow handle by using add_textflow() when you apply as textflow handle "0" (PHP, otherwise -1).