mPDF 错误:某些数据已输出到浏览器,无法发送 PDF 文件


mPDF error: Some data has already been output to browser, can't send PDF file

我在尝试使用mPDF时遇到这样的错误。mPDF 错误:某些数据已输出到浏览器,无法发送 PDF 文件

这是代码:

<?php
include("mpdf60/mpdf.php");
$mpdf=new mPDF('c','A4','','' , 0 , 0 , 0 , 0 , 0 , 0); 
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
$mpdf->WriteHTML(file_get_contents('invoice.html'));
$mpdf->Output();

$to      = $_POST["email"];
$subject = 'the subject';
$message = 'hello';
$headers = 'From: narehh@mail.ru' . "'r'n" .
    'Reply-To: narehh@mail.ru' . "'r'n" .
    'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);

你有没有遇到过这个问题?如何解决?谢谢

include("mpdf60/mpdf.php");之前尝试使用ob_end_clean 也许可以解决您的问题。

我遇到了同样的问题,从Khushal Nayani的回答中,我想出了什么问题:在我的 *.php 文件中,我正在打印$PDFcontent ( print($PDFcontent); ((作为检查步骤,同时调用新的 pdf:(( mpdf = new 'Mpdf'Mpdf... etc. ((。因此错误:数据已经发送到输出...

是的,当我评论((//print($PDFcontent)((时,事情奏效了。

我遇到了同样的错误。

Data has already been sent to output, unable to output PDF file

这意味着在使用 mPDF 创建 pdf 之前,一些数据存储在发送到浏览器的缓冲区中。因此,它无法创建 PDF。

将此 php 内置函数添加到页面第一行下方,您正在为 pdf 准备数据。

ob_start();

并在 mPDF 代码之前添加这个内置的 php 函数(在你调用 mpdf 之前(。

ob_end_flush();
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new 'Mpdf'Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output();

这将在处理 mPDF 之前清除所有缓冲区输出。

确保如果您使用任何函数,然后将其保留在同一页面

中,请不要包含您保留所有函数的函数页面。