【问题标题】:Using DomPDF to create invoice PDF使用 DomPDF 创建发票 PDF
【发布时间】: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 谢谢。我添加了错误消息。

标签: php pdf dompdf


【解决方案1】:

尝试在以下位置添加完整的网址:

$invoice = file_get_contents_curl('http://...view/invoice?id=1');

【讨论】:

  • 这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
  • 我认为这是真正的问题......因为他试图卷曲 view/invoice?id=1 curl 显然返回空白数据......他必须提供完整的 url 来获取数据……第二,当我发布这个时,我的代表低于 50,所以这是我唯一的选择:)
  • @AliArshad 我更改了它,但现在我得到了登录屏幕的 PDF;>
  • 所以这证实了这个问题是 http...它在这里...#
  • 除了复制发票中的代码之外,您还可以通过 curl 传递身份验证凭据。我自己没有这样做,但您可以在 SO 上找到该信息(例如 herehere)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 2023-03-22
相关资源
最近更新 更多