PHP:Adobe Reader无法打开使用mpdf创建的PDF文件
我正在使用mpdf即时创建PDF文件,并且这些文件在浏览器中可以正常打开,但是Adobe给我一个错误:
I'm using mpdf to create PDF files on the fly, and the files open fine in a browser but Adobe gives me an error:
Adobe Acrobat Reader DC无法打开'example-filename.pdf' 因为它不是受支持的文件类型,或者因为文件 已损坏(例如,它已作为电子邮件附件发送 并且未正确解码).
Adobe Acrobat Reader DC could not open 'example-filename.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).
I looked at other questions about this (another mpdf + adobe error), and checked out the pdf in a text editor. I found that the first part of the file looked like this:
<!DOCTYPE html>
<head>
<title>
CapstoneDB
</title>
%PDF-1.4
%âãÏÓ
删除所有内容后,直到%PDF-1.4
(包括选项卡),该文件都可以在Adobe中很好地打开,这很好,除了我需要能够使生成的pdf在Adobe中打开而无需手动摆弄每次都输入代码.
After I removed everything up to %PDF-1.4
(including the tab), the file opened fine in Adobe, which is great, except that I need to be able to get the generated pdfs to open in Adobe without manually fiddling with the code every time.
这是我的包装函数,该函数使用html和css调用mpdf:
Here's my wrapper function that calls mpdf with the html and css:
include('../mpdf/mpdf.php');
function user_download_pdf($html, $css_file, $filename) {
$mpdf = new mPDF();
$stylesheet = file_get_contents($css_file);
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html,2);
$mpdf->Output($filename, "D");
}
我从不向mpdf提供完整的html页面,通常只有h3和一个或多个表.也许我需要给mpdf一个完整的html页面,包括<head>
,<body>
等?有什么方法可以改变mpdf的配置,或者我在php中调用mpdf的方式可以摆脱pdf文件开头的html垃圾内容?
I never feed mpdf a full html page, usually just an h3 and one or more tables. Maybe I need to be giving mpdf an entire html page, including <head>
, <body>
, etc? Is there any way to change mpdf configuration or the way I call mpdf in php that would get rid of the html junk at the beginning of the pdf file that's gunking everything up?
放置
ob_clean();
立即之前
$mpdf->Output();
如果没有该mpdf,则有时会包含网站页面HTML,而不仅仅是您想要的PDF中的HTML,这可能是因为标头已经发送到了代码的其他位置.那会弄乱您的PDF,因此Adobe不会打开它.
Without this mpdf sometimes includes the website page HTML and not just the HTML that you want in the PDF, probably because headers have already been sent elsewhere in the code. That can mess up your PDF so Adobe won't open it.