Question

I am having an issue with generating pdf documents. My tabulator characters are not displayed properly. I already tried different fonts, or encodings (UTF-8, Windows1552). With some fonts the character is completely hidden. With some I get a square symbol displayed instead of my tab "\t".

Here is my code.

The question is "How to display tabs using Zend_PDF?"

public function generate()
{
    $pdf        = new Zend_Pdf();
    $page       = new Zend_Pdf_Page( Zend_Pdf_Page::SIZE_A4 );

    //render basic template
    $template   = Zend_Pdf_Image::imageWithPath( APPLICATION_PATH . '/resources/pdf/template.png' );
    $page->drawImage( $template, 0 ,0, 595, 842 );

    //render document title     
    $font = Zend_Pdf_Font::fontWithPath( APPLICATION_PATH . '/resources/pdf/arial-bold.ttf' );
    $page   ->setFont($font, 14)                
            ->drawText( 'Rechnung', 390, 700, 'utf-8' );

    //render reciever adress
    $font = Zend_Pdf_Font::fontWithPath( APPLICATION_PATH . '/resources/pdf/arial.ttf' );



    $adressText = array( 
        'Kundennummer' . "\t" . $this->_user->getUserIdString(),
        'Belegnummer' . "\t" . $this->_payin->getPayinIdString(),
        'Datum' . "\t\t\t" . $this->_payin->getDateCreated()->format( 'd.m.Y' ),
        'Seite' . "\t\t\t" . '1/1'
    );

    $page   ->setFont($font, 12);
    $adressY  =  680;

    foreach( $adressText as $line )
    {
        $page->drawText( $line, 390, $adressY , 'utf-8' );
        $adressY -= 12;
    }


    //add page to pdf document
    $pdf->pages[] = $page;

    //save pdf
    $pdf->save( $this->getOption( 'path' ) );   
}
Was it helpful?

Solution

It could be that the pdf doesn't understand \t.

Try replacing it with 'chr(9)' which is the ascii value of the tab character. For example:- $tab = chr(9); $adressText = array( 'Kundennummer' . $tab . $this->_user->getUserIdString(), 'Belegnummer' . $tab . $this->_payin->getPayinIdString(), // etc.. );

Correction:
As you have to supply the x,y coordinates to Zend_Pdf_Page::drawText() things like tabs, line feeds, etc will not work. You will have to set fixed coordinates for your tab stops.

For example:-

$tabs = array(5, 20, 30, 50);
$page->drawText("At 1st tab", $tabs[0], 10);
$page->drawText("At 2nd Tab", $tabs[1], 10);
$page->drawText("At 3rd Tab", $tabs[2], 10);

Hopefully you get the idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top