2009-08-25 160 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