1

我在生成 pdf 文档时遇到问题。我的制表符字符显示不正确。我已经尝试过不同的字体或编码(UTF-8、Windows1552)。对于某些字体,字符是完全隐藏的。有些我会显示一个方形符号,而不是我的标签“\ t”。

这是我的代码。

问题是“如何使用 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' ) );   
}
4

1 回答 1

2

可能是pdf看不懂\t

尝试用 'chr(9)' 替换它,这是制表符的 ascii 值。例如:- $tab = chr(9); $adressText = array( 'Kundennummer' . $tab . $this->_user->getUserIdString(), 'Belegnummer' . $tab . $this->_payin->getPayinIdString(), // etc.. );

更正:
由于您必须为Zend_Pdf_Page::drawText()制表符、换行符等提供 x、y 坐标,因此将无法正常工作。您必须为制表位设置固定坐标。

例如:-

$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);

希望你明白这一点。

于 2011-11-13T21:25:55.753 回答