【问题标题】:Zend Framework PDF multiline problemsZend Framework PDF 多行问题
【发布时间】:2010-11-22 18:04:10
【问题描述】:

我有一个问题,使用 Zend_PDF 多行,我的问题是我无法将整个文本写入我的 pdf。我的文本如下所示: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());
}

我想做的是让每个&lt;p&gt; 休息一下(&lt;br /&gt;\n) 并显示整个文本。

有什么解决办法吗?

【问题讨论】:

    标签: php zend-framework pdf


    【解决方案1】:

    而不是这个:

     // 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 个“常量”来获得最佳结果。

    希望对你有帮助。

    【讨论】:

    • 是的,它有效!但我还有一个问题,我怎样才能在文本的顶部放一个标题?
    • 将代码中的 820 更改为 ~800 并添加另一个 drawText 调用以打印您的标题
    猜你喜欢
    • 2010-11-22
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    相关资源
    最近更新 更多