【发布时间】:2020-08-04 15:53:37
【问题描述】:
我正在使用在本地运行良好的 Mpdf,我遇到的问题是 pdf 不会输出并且只是得到一个空白屏幕
这是错误
致命错误:未捕获的 Mpdf\MpdfException:数据已发送到输出(/customers/3/c/2/includes/header.php 第 14 行),无法在 /customers/3/c 中输出 PDF 文件/2/pdf/vendor/mpdf/mpdf/src/Mpdf.php:9510 堆栈跟踪:#0 /customers/3/c/2: Mpdf\Mpdf->Output('sold.pdf', 'I') # 1 /customers/3/c/2/include('/customers/3/c/...') #2 {main} 抛出 /customers/3/c/2/pdf/vendor/mpdf/mpdf/src /Mpdf.php 第 9510 行
我尝试了几件事,UTF 和 DOM 的编码是正确的,我没有空格,一切似乎都很好
这是标题代码
<?php
session_start();
if(empty($_SESSION['user_id']))
{
header("Location: https://www.home.co.uk");
}
require '../dbcon/database.php';
$db = DB();
require '../lib/library.php';
$app = new WIDGET();
require '../lib/carbon.php';
$user = $app->UserDetails($_SESSION['user_id']);
?>
我也试过
<?php
session_start();
require '../dbcon/database.php';
$db = DB();
require '../lib/library.php';
$app = new WIDGET();
require '../lib/carbon.php';
$user = $app->UserDetails($_SESSION['user_id']);
?>
这没有什么不同。
这是Mdpf.php第9510行的代码
case Destination::INLINE:
if (headers_sent($filename, $line)) {
throw new \Mpdf\MpdfException(
sprintf('Data has already been sent to output (%s at line %s), unable to output PDF file', $filename, $line)
);
}
就像提到的那样,我得到的只是一个空白页,如果我输出到 iframe 就可以了。 在 127.0.0.1 上工作正常
非常感谢任何帮助,并提前感谢任何帮助
更新:
抱歉忘记提及或显示调用 pdf 的代码。
我已经试过 ob_clean() 还是不行
require_once 'pdf/vendor/autoload.php';
if(ob_get_length() > 0) {
ob_clean();
}
$mpdf = new \Mpdf\Mpdf();
$html ='<html>
<body>
<h2 style="font-size: 120px; text-align:center;">SOLD</h2>
<h2 style="font-size: 50px; text-transform: uppercase; text-align:center;">'. $row->widget_model .'</h2>
<h2 style="font-size: 50px; text-transform: uppercase; text-align:center;">'. $row->widget_serial .'</h2><br>
<h2 style="font-size: 30px; text-transform: uppercase; text-align:center;">Delivery Date: '. (new DateTime($_POST['expdelivery']))->format('d-m-Y') .'</h2>
<br>
<h2 style="font-size: 20px; text-align:center;">Contact: '. $_POST['name'] .'</h2>
</body>
</html>';
$mpdf->WriteHTML($html);
$mpdf->Output('sold.pdf','I');
ob_end_flush();
}
编辑 2:-
查看网络选项卡,它只显示所有 js 调用和 favicon.ico
它还显示此页面 updatewidget.php?id=2178
<?php
$page_title = "Widget Tracker - Amend Widget";
$page = '';
include "includes/header.php";
include "includes/sidebar.php";
$id = isset($_GET['id']) ? $_GET['id'] : NULL;
$sth = $db->prepare("SELECT * FROM `widgettracker` WHERE `id` = :id");
$sth->bindParam(':id', $id, PDO::PARAM_INT);
$sth->setFetchMode(PDO::FETCH_OBJ);
$sth->execute();
$row = $sth->fetch();
?>
<section class="content">
<div class="container-fluid">
<?php
if($row->widgetstatus == "Sold")
{include "sold.php";}
else
{include "widgetdetails.php";}
?>
</div>
</section>
<?php
include "includes/footer.php";
include "includes/customjs.php";
?>
Mpdf 所在的页面是“widgetdetails.php”,我需要插入销售日期并更新并生成 pdf。然后报错
【问题讨论】:
-
错误提示输出或标头已经发送。这可能是您的 PHP 文件在
?>标记之后的一些输出,但是使用您提供的小 sn-ps,尚不清楚它的来源。如果一切都失败了,您可以在尝试输出 Mpdf 结果之前调用ob_clean()。 -
抱歉更新问题
-
感谢更新。你能检查从浏览器收到的原始数据吗?应该可以通过 Inspect Element 访问或右键单击白页并查看源代码。
-
第二次更新问题