使用DomPDF创建发票PDF

时间:2015-03-16 14:03:44

标签: php pdf dompdf

我们网络服务器上发票的示例链接:

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

1 个答案:

答案 0 :(得分:2)

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

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