2009-08-25 13 views
9

私はZend_Pdfは複数行で、問題を抱えている、私の問題は、私が私pdf.Myテキストにテキスト全体を書くことができないということである。このようになります:http://pastebin.com/f6413f664Zend FrameworkのPDF複数行の問題

しかし、ときに私が開きます私の.pdfファイルは、テキストは次のようになります。http://screencast.com/t/1CBjvRodeZQd

そしてここでは私のコードです:すべての<p>はブレーク(<br /> \ n)を持っており、にするために、私が何をしたいのか

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

、ありますテキスト全体を表示します。

解決方法これに代えて

答えて

17

// 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「定数」をプレイする必要があります。

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

はこの試み(それを試していなかった)を得ました。

希望します。

+0

ええ、それはうまくいきますが、もう1つの質問があります。どうすればトップのテキスト、タイトルを置くことができますか? – Uffo

+0

コード内の820を〜800に変更し、別のdrawText呼び出しを追加してタイトルを印刷します –

+0

Thx a lot man! – Uffo