【发布时间】:2015-05-18 16:21:44
【问题描述】:
我们网络服务器上发票的示例链接:
http://billing/view/invoice?id=1
这会在浏览器中显示发票。
我尝试将其保存为 PDF:
<?php
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
require_once("dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$invoice = file_get_contents_curl('view/invoice?id=1');
$dompdf->load_html($invoice);
$dompdf->set_paper('a4');
$dompdf->render();
$dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
exit(0);
?>
这显示一个空白页,没有发票或 PDF。
如果我改变了
$invoice = file_get_contents_curl('view/invoice?id=1');
$dompdf->load_html($invoice);
到
$invoice = "Hello";
$dompdf->load_html($invoice);
然后它显示一个包含“Hello”的 PDF,因此捕获动态 PHP 发票似乎是一个问题。
错误报告显示:
Warning: file_get_contents(view/order.php?id=1432923): failed to open stream: No error in C:\inetpub\wwwroot\billing\test.php on line 6
【问题讨论】:
-
当您在 PHP 中得到一个空白页面(“死机白屏”)时,如果需要一些输出,请确保您将错误报告一直打开并显示在屏幕上。总是在开发代码时,
error_reporting(E_ALL); ini_set('display_errors', 1);位于脚本的顶部,尽管对于发送带有Content-Type而非文本的内容的脚本,查看服务器的错误日志会更容易。 -
@MichaelBerkowski 谢谢。我添加了错误消息。