2009-08-25 120 views
9

我有一个问题,与Zend_PDF多,我的问题是,我不能整个文本写信给我pdf.My的文字是这样的:http://pastebin.com/f6413f664Zend框架PDF多问题

但是当我打开我.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)和以显示整个文本。

任何解决方案的家伙?

回答

17

取而代之的是:

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

你显然需要与2“常量”发挥,以获得最好的结果。

希望它有帮助。

+0

是的,它的工作原理!但我还有一个问题,我怎么能在文本顶部,标题? – Uffo 2009-08-25 20:09:57

+0

将代码中的820更改为〜800,并添加另一个drawText调用以打印您的标题 – 2009-08-25 20:22:06

+0

Thx很多人! – Uffo 2009-08-25 20:27:42