无法摆脱mPDF中的PHP通知

时间:2012-01-30 17:15:43

标签: php notice mpdf

我正在使用mPDF library直接从HTML输出生成PDF文档。问题是这个mPDF库是按原样编写的,它产生了许多通知(未定义的索引,未定义的偏移量等)。我试图阻止输出它们,但没有任何帮助。

我尝试将error_reporting(E_ALL ^ E_NOTICE);以及我插入到我的index.php中的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()),即使我使用复杂的HTML,PDF也是有效的,因此仍然可以读取,但仍然会将通知打印到浏览器中。无论如何,我需要提供下载的PDF,而不是保存到文件系统中。

好的,我找到了一个解决方案,虽然这不是最好的做法(但它确实有效):我用ob_start();ob_end_clean();附上代码,同时删除我输出的$ pdf字符串而不是MPDF。

最终代码:

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;

3 个答案:

答案 0 :(得分:0)

虽然没有答案,而且因为我没有找到任何其他适当的解决方案,所以这里是我迄今为止所得内容的摘要(主要是从我上面的问题复制):

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生成通知并且还没有忘记&#34;修复&#34; mpdf.php(带有1.34 MB的PHP代码),因此这是(现在)唯一适合我的解决方案。

也许它会帮助某人。

答案 1 :(得分:0)

当它试图为每个页面写出新的表格标题时,似乎发生了错误。 我在V 5.4中评论过 第26210行

#$this->TableHeaderFooter($tablefooter,$tablestartpage,$tablestartcolumn,'F',$level, $firstSpread, $finalSpread);   // mPDF 5.3.36

如果没有如此评论这条线而没有被抽出的标题除了杀死通知之外没有任何效果。

答案 2 :(得分:0)

如果您在$mpdf->Output()之后使用ob_end_clean(),您甚至可以在浏览器中显示没有通知的PDF!我在OpenCart中使用它。但您需要使用ob_start()ob_end_clean()

相关问题