Zend Framework PDF多行问题 [英] Zend Framework PDF multiline problems

查看:79
本文介绍了Zend Framework PDF多行问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,使用Zend_PDF多行,我的问题是我不能将整个文本写到我的pdf .我的文本看起来像这样:

I have a problem, with Zend_PDF multiline, my problem is that I can't write the entire text to my pdf.My text looks like this: http://pastebin.com/f6413f664

但是当我打开.pdf文件时,文本如下所示: http://screencast.com/t /1CBjvRodeZQd

But when I open my .pdf file, the text looks like this: 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>都有一个break(<br /> \ n)并显示整个文本.

What I want to do, is for every <p> to have a break(<br />\n) and to display the entire text.

有解决方案的人吗?

推荐答案

代替此:

 // 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个常量"一起玩才能获得最佳结果.

You obviously need to play with those 2 "constants" to get the best results.

希望有帮助.

这篇关于Zend Framework PDF多行问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆