Zend Framework PDF多线问题

时间:2009-08-25 19:04:33

标签: php zend-framework pdf

我有一个问题,使用Zend_PDF多行,我的问题是我无法将整个文本写入我的pdf.My文本如下所示:http://pastebin.com/f6413f664

但是当我打开我的.pdf文件时,文本如下所示:http://screencast.com/t/1CBjvRodeZQd

这是我的代码:

    public function pdfAction()
{
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();

    $theID = ($this->_getParam('id') !== NULL) ? (int)$this->_getParam('id') : false;

    ($theID === false) ? $this->_redirect('/home') : false;

    //Information
    $info = $this->artists->artistInfo($theID);

    // Create new PDF 
    $pdf = new Zend_Pdf(); 
    $pdf->properties['Title'] = "TITLE";
    $pdf->properties['Author'] = "AUTHOR";

    // Add new page to the document 
    $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); 
    $pdf->pages[] = $page; 

    // Set font 
    $page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 8); 

    // Draw text
     foreach (explode("</p>", $info[0]['biography']) as $i => $line) {
       $page->drawText($line, 0, 820 - $i * 10, 'UTF-8');
     }

    $this->getResponse()->setHeader('Content-type', 'application/x-pdf', true);
    $this->getResponse()->setHeader('Content-disposition', 'attachment; filename=my-file.pdf', true);
    $this->getResponse()->setBody($pdf->render());
}

我想要做的是让每个<p>休息一下(<br /> \ n)并显示整个文字。

任何解决方案的人?

1 个答案:

答案 0 :(得分:18)

而不是:

 // Draw text
 foreach (explode("</p>", $info[0]['biography']) as $i => $line) {
   $page->drawText($line, 0, 820 - $i * 10, 'UTF-8');
 }

尝试一下(没试过):

// Draw text    
$charsPerLine = 50;
$heightPerLine = 10;

$text = str_replace('<p>','',$info[0]['biography']);
$lines = array();

foreach (explode("</p>", $text) as $line) {
    $lines = array_merge(
                    $lines, 
                    explode(
                            "\n",
                            wordwrap($line, $charsPerLine, "\n")
                    )
    );
}

foreach ( $lines as $i=>$line ) {
    $page->drawText($line, 0, 820 - $i * $heightPerLine, 'UTF-8');
}

你显然需要使用这两个“常数”才能获得最佳效果。

希望它有所帮助。