简体   繁体   中英

Failed to Load Pdf Document with TCPDF

Now I'm currently working on convert html file to pdf in Zend Framework, I'm using TCPDF for doing this, following is my code.

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true,    'UTF-8', false);
$view = new Zend_view();
$htmlcontent = $view->render('media-file.phtml');
$pdf->AddPage(); 
$pdf->writeHTML($htmlcontent, true, 0, true, 0);
$pdf->lastPage();
$pdf->Output("single-page.pdf", 'D');

My problem is file is downloading, but when opened the downloaded file it shows 'Failed to load pdf document'. I don't know where I'm wrong. Please let me Know

There are many places where problems might be occurring.

Firstly, is the HTML valid? TCPDF expects valid HTML, only handles a subset of HTML and all HTML attributes must be enclosed in double-quotes, so you should check all those things before going any further.

http://www.tcpdf.org/doc/code/classTCPDF.html#ac3fdf25fcd36f1dce04f92187c621407

Next is the 'Failed to load pdf document' error. Which application is actually showing that error? Is the problem that the file wasn't downloaded successfully by the browser, or that your PDF reader couldn't open it?

Instead of sending the PDF file to the browser, try saving it to a file. That way you eliminate any issues that might be happening with the browser (and also any issues that might be happening with stray data being sent to the browser before the download).

Once you are writing the PDF directly to a file, as @MonkeyZeus said, try opening it up in Notepad or even just using the "head" command if you have it available to look at the first few lines. If it doesn't start with something like this then the PDF is not valid:

%PDF-1.7

If you still can't solve the problem, it would be helpful to post a link to a sample of the HTML and also to know what version of TCPDF you are using. Setting the PHP error level high as @MonkeyZeus suggested and including any error messages would also be helpful.

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