简体   繁体   中英

Adobe Reader cannot open .pdf file created using mPDF in Zend Framework

I'm trying to generate a .pdf file using mPDF in a Zend Framework application, from the output of the action.

Here is the code of my action:

public function testAction()
{       
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout->disableLayout();

        $this->view->foo = 'bar';

        $this->render();
        $output = $this->getResponse()->getBody();

        $layout = new Zend_Layout();
        $layout->content = $output;     
        $layout->setLayoutPath(dirname(dirname(__FILE__)) . '/views/layouts/');
        $layout->setViewSuffix('tpl');
        $layout->setLayout('pdf');

        $html = $layout->render();

        $mpdf = new mPDF('utf-8', 'A4');
        $mpdf->WriteHTML($html);
        $mpdf->Output('report.pdf', 'D');
}

If the content to be displayed is long (ie a few paragraphs), when downloading the .pdf file, Adobe Reader throws the following error: Adobe Reader could not open 'report.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).

However, if I pass the same output to mpdf as a static variable, without using Zend Layout render, then the .pdf file opens without any errors. Also, Adobe Reader throws the issue if and only if the content is long (ie it works if there are only a couple of words).

Is there any limit I should be aware of?

在文件的开头和结尾不应该有空格,所以检查这些空间,

Adobe Reader is less forgiving than some othe PDF readers if the PDF file is corrupt. Open your PDF document in a text editor and check that the file starts with something like: %PDF-1.4 %âãÃÓ

Sometimes PHP error notices are found at the top of the file.

Source : mpdf forum IanBack's answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM