无法摆脱mPDF中的PHP通知 [英] Can't get rid of PHP notices in mPDF

查看:69
本文介绍了无法摆脱mPDF中的PHP通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 mPDF库直接从HTML输出生成PDF文档.问题在于该mPDF库是按原样编写的,并且会生成许多通知(未定义的索引,未定义的偏移量等).我试图停止输出它们,但没有任何帮助.

我试图将插入到index.php中的error_reporting(E_ALL ^ E_NOTICE);以及error_reporting(E_ALL & ~E_NOTICE);放入直接包含mpdf.php以及mpdf.php开头的类和方法中.我还尝试了ini_set('display_errors', 0);的组合-所有这些指令都可用于整个Web应用程序,但适用于mpdf.因此,即使PDF格式正确且有效,我也无法输出(让用户下载).

当示例运行良好且没有任何提示时,我的HTML(简单表,真的没什么特别的)也发生了问题.

所以我需要帮助:摆脱公告,或者更好地帮助我找出mPDF对我不起作用的原因.

如果我使用此代码:

include_once(DIR_MPDF.'mpdf.php');
$mpdf = new mPDF();
$mpdf->useOnlyCoreFonts = true;
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetAutoFont(0);
$mpdf->WriteHTML('<table><tr><td>HELLO WORLD</td></tr></table>');
$mpdf->Output();
exit;

一切正常,但是如果我尝试输出此HTML:

$mpdf->WriteHTML('<table><tr><td>HELLO WORLD</td><td>HELLO WORLD</td></tr></table>');

我收到通知,因此无法输出PDF.

如果我将mPDF的输出保存到文件中(例如使用file_put_contents()),则PDF是有效的,因此即使我使用复杂的HTML也可以读取-但通知仍会打印在浏览器中.无论如何,我需要提供PDF以便下载,而不是保存到文件系统中.

好的,我找到了一个解决方案,尽管它不是最佳实践(但它可以工作):我将代码用ob_start();ob_end_clean();括起来,同时捕获了我输出的而不是mPDF的$ pdf字符串.

最终代码:

ob_start();
include(DIR_MPDF.'mpdf.php');
$html = $this->render(TRUE);

$mpdf = new mPDF('utf-8','A4');

$mpdf->useOnlyCoreFonts = true;
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetAutoFont(0);

$stylesheet = file_get_contents(DIR_APPLICATION.'view/stylesheet/declaration.css');
$mpdf->WriteHTML($stylesheet,1);

$mpdf->WriteHTML($html);

$pdf = $mpdf->Output('', 'S');
$ob = ob_get_contents();
ob_end_clean();

if (headers_sent())
    die('Some data has already been output to browser, can\'t send PDF file');
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream', false);
header('Content-Type: application/download', false);
header('Content-Type: application/pdf', false);
if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
    header('Content-Length: '.strlen($pdf));
}
header('Content-disposition: attachment; filename="invoice.pdf"');
echo $pdf;
exit;

解决方案

虽然没有答案,而且由于我没有找到任何其他合适的解决方案,所以这里是到目前为止的总结(主要是上面问题的副本) ):

ob_start(); // <--| This is very important to start output buffering and to catch out any possible notices
include(DIR_MPDF.'mpdf.php');
$html = $this->render(TRUE);

$mpdf = new mPDF('utf-8','A4');

$mpdf->useOnlyCoreFonts = true;
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetAutoFont(0);

$stylesheet = file_get_contents(DIR_APPLICATION.'view/stylesheet/declaration.css');
$mpdf->WriteHTML($stylesheet,1); // <--| By the second param we are saying to MPDF that it is icnluding only stylesheet

$mpdf->WriteHTML($html);

$pdf = $mpdf->Output('', 'S'); // <--| With the binary PDF data in $pdf we can do whatever we want - attach it to email, save to filesystem, push to browser's PDF plugin or offer it to user for download
$ob = ob_get_contents(); // <--| Here we catch out previous output from buffer (and can log it, email it, or throw it away as I do :-) )
ob_end_clean(); // <--| Finaly we clean output buffering and turn it off

// The next headers() section is copied out form mPDF Output() method that offers a PDF file to download
if (headers_sent())
    die('Some data has already been output to browser, can\'t send PDF file');
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream', false);
header('Content-Type: application/download', false);
header('Content-Type: application/pdf', false);
if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
    header('Content-Length: '.strlen($pdf));
}
header('Content-disposition: attachment; filename="invoice.pdf"');
echo $pdf; // <--| With the headers set PDF file is ready for download after we call echo
exit;

正如上面的评论中所述,然后就是我(或客户:-))如何处理从mPDF返回的PDF数据.我在整个应用程序的更多地方都使用了此PDF生成工具,并且大多数情况下都提供PDF以便下载,但我也将其附加到电子邮件中(用户付款,生成PDF发票并通过电子邮件发送).

我发现没有解决方案(也没有更多时间这样做)来停止mPDF生成通知,并且还没有迷失方向去修复" mpdf.php(带有1.34 MB的PHP代码).这是(目前)唯一适用于我的解决方案.

也许会帮助别人.

I'm using the mPDF library to generate PDF docs directly from HTML output. The problem is that this mPDF library is written as it is and it is generating dozens of notices (undefined index, undefined offset, etc). I tried anything to stop outputting them but nothing yet helped.

I tried to put error_reporting(E_ALL ^ E_NOTICE); as well as error_reporting(E_ALL & ~E_NOTICE); which i inserted into my index.php, into the class and method that is directly including mpdf.php and also at the start of mpdf.php. I also tried combinations with ini_set('display_errors', 0); - all these directives are working for whole the web application but for mpdf. Therefore even when PDF could be well formed and valid I cannot output it (let the user download it).

Also the problem occurs with my HTML (simple table, really nothing special) while the examples are working fine and with no notices.

So the help I would need: either get rid of notices or better help me find out why the mPDF is not working for me.

If I use this code:

include_once(DIR_MPDF.'mpdf.php');
$mpdf = new mPDF();
$mpdf->useOnlyCoreFonts = true;
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetAutoFont(0);
$mpdf->WriteHTML('<table><tr><td>HELLO WORLD</td></tr></table>');
$mpdf->Output();
exit;

everything is working good, but if I try to output this HTML:

$mpdf->WriteHTML('<table><tr><td>HELLO WORLD</td><td>HELLO WORLD</td></tr></table>');

I get notices and therefore PDF cannot be outputted.

If I save the output from mPDF into a file (using e.g. file_put_contents()), the PDF is valid and therefore readable even if I use complex HTML - but still the Notices are printed into the browser. Anyway, I need the PDF to be offered for download, not to be saved into filesystem.

OK, I found one solution though it is not the best practice (but it works): I enclose the code with ob_start(); and ob_end_clean(); while catching out the $pdf string that I output instead of mPDF.

Final code:

ob_start();
include(DIR_MPDF.'mpdf.php');
$html = $this->render(TRUE);

$mpdf = new mPDF('utf-8','A4');

$mpdf->useOnlyCoreFonts = true;
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetAutoFont(0);

$stylesheet = file_get_contents(DIR_APPLICATION.'view/stylesheet/declaration.css');
$mpdf->WriteHTML($stylesheet,1);

$mpdf->WriteHTML($html);

$pdf = $mpdf->Output('', 'S');
$ob = ob_get_contents();
ob_end_clean();

if (headers_sent())
    die('Some data has already been output to browser, can\'t send PDF file');
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream', false);
header('Content-Type: application/download', false);
header('Content-Type: application/pdf', false);
if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
    header('Content-Length: '.strlen($pdf));
}
header('Content-disposition: attachment; filename="invoice.pdf"');
echo $pdf;
exit;

解决方案

While there is no answer and because I've not found any other appropriate solution, here is summary of what I have so far (mainly copy from my question above):

ob_start(); // <--| This is very important to start output buffering and to catch out any possible notices
include(DIR_MPDF.'mpdf.php');
$html = $this->render(TRUE);

$mpdf = new mPDF('utf-8','A4');

$mpdf->useOnlyCoreFonts = true;
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetAutoFont(0);

$stylesheet = file_get_contents(DIR_APPLICATION.'view/stylesheet/declaration.css');
$mpdf->WriteHTML($stylesheet,1); // <--| By the second param we are saying to MPDF that it is icnluding only stylesheet

$mpdf->WriteHTML($html);

$pdf = $mpdf->Output('', 'S'); // <--| With the binary PDF data in $pdf we can do whatever we want - attach it to email, save to filesystem, push to browser's PDF plugin or offer it to user for download
$ob = ob_get_contents(); // <--| Here we catch out previous output from buffer (and can log it, email it, or throw it away as I do :-) )
ob_end_clean(); // <--| Finaly we clean output buffering and turn it off

// The next headers() section is copied out form mPDF Output() method that offers a PDF file to download
if (headers_sent())
    die('Some data has already been output to browser, can\'t send PDF file');
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream', false);
header('Content-Type: application/download', false);
header('Content-Type: application/pdf', false);
if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
    header('Content-Length: '.strlen($pdf));
}
header('Content-disposition: attachment; filename="invoice.pdf"');
echo $pdf; // <--| With the headers set PDF file is ready for download after we call echo
exit;

As written in the comment above, it is then just upon me (or client :-) ) what will be done with the PDF data returned from mPDF. I use this PDF generating on more places through the application and mostly offer PDF just for download but I also attaches it to email (user makes a payment, I generate the PDF invoice and send it via email).

I have found no solution (and also have no more time to do so) to stop mPDF generate notices and haven't yet lost my mind to "repair" the mpdf.php (with its 1.34 MB of PHP code) therefore this is (for now) the only solution that works for me.

Maybe it will help somebody.

这篇关于无法摆脱mPDF中的PHP通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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