Zend Framework PDF multiline problems

蓝咒 提交于 2019-12-21 09:39:25

问题


I have a problem, with Zend_PDF multiline, my problem is that I can't write the entire text to my pdf.My text looks like this: http://pastebin.com/f6413f664

But when I open my .pdf file, the text looks like this: http://screencast.com/t/1CBjvRodeZQd

And here is my code:

    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());
}

What I want to do, is for every <p> to have a break(<br />\n) and to display the entire text.

Any solutions guys?


回答1:


Instead of this:

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

Give this a try(didn't try it):

// 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');
}

You obviously need to play with those 2 "constants" to get the best results.

Hope it helps.



来源:https://stackoverflow.com/questions/1330322/zend-framework-pdf-multiline-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!