代码之家  ›  专栏  ›  技术社区  ›  Uffo

Zend框架PDF多行问题

  •  9
  • Uffo  · 技术社区  · 16 年前

    我有一个问题,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());
    }
    

    我想做的是 <p> 休息一下( <br /> \ n)并显示整个文本。

    有什么解决办法吗?

    1 回复  |  直到 16 年前
        1
  •  18
  •   André Hoffmann    16 年前

    而不是这个:

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

    显然,您需要使用这两个“常量”来获得最佳结果。

    希望它有帮助。