简体   繁体   English

无法使用TCPDF加载Pdf文档

[英]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. 现在,我目前正在Zend Framework中将html文件转换为pdf,我正在使用TCPDF进行此操作,以下是我的代码。

$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'. 我的问题是文件正在下载,但是当打开下载的文件时,它显示“无法加载pdf文档”。 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? 首先,HTML有效吗? 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. TCPDF需要有效的HTML,仅处理HTML的一个子集,并且所有HTML属性必须用双引号引起来,因此,在进行任何进一步操作之前,应检查所有这些内容。

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

Next is the 'Failed to load pdf document' error. 接下来是“无法加载pdf文档”错误。 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? 是文件没有被浏览器成功下载,还是您的PDF阅读器无法打开文件?

Instead of sending the PDF file to the browser, try saving it to a file. 与其将PDF文件发送到浏览器,不如尝试将其保存到文件中。 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. 如@MonkeyZeus所说,将PDF直接写到文件后,尝试在记事本中打开它,或者如果可以查看前几行,甚至可以使用“ head”命令打开它。 If it doesn't start with something like this then the PDF is not valid: 如果不是以这样的开头,则PDF无效:

%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. 如果仍然不能解决问题,则将链接发布到HTML样本的链接,并了解所使用的TCPDF版本会很有帮助。 Setting the PHP error level high as @MonkeyZeus suggested and including any error messages would also be helpful. 按照@MonkeyZeus的建议将PHP错误级别设置为高,并包括所有错误消息也将有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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