如何使用API​​生成pdf文档?

时间:2014-02-10 17:19:15

标签: php pdf-generation

我使用API​​生成pdf文档。在这个API的文档中,我有:

http://...../view_awb.php?user=nume_utilizator&parola=123456&awb=CGS12345678A
If success, the view_awb.php file will return a PDF document

接下来,我有这些代码:

require "../dbcommon2.php";

$url = "http://webexpress.cargus.ro/custom_print/shipment_import/view_awb.php?user=canai_test&parola=test&awb=TSD21185178T";

$pdf = file_get_contents($url, false);
require_once("../../dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html($pdf);
$dompdf->render();
$dompdf->stream("sample.pdf");

所有看起来都正确,但是当我打开pdf文件时,我只看到

%PDF-1.3 3 0 obj > endobj 4 0 obj > stream  xY[ sÚ8¾ï¯Ðe{UgÙ¹£tèd!KHg/:³ãñ.`Öì7ýõß«Æ6HM;M¤WÞãG¡oýûáHL"ð¥D¦é4Á

问题出在哪里?如何正确生成pdf文件? 我用Adobe Reader打开pdf文件sample.pdf

1 个答案:

答案 0 :(得分:1)

看起来http://webexpress.cargus.ro/custom_print/shipment_import/view_awb.php?user=canai_test&parola=test&awb=TSD21185178T的可用内容已经是.pdf。

您可以尝试(我基于http://davidwalsh.name/curl-download上的示例):

<?php

header("Content-Type: application/pdf");

function get_data($url)
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

$url = "http://webexpress.cargus.ro/custom_print/shipment_import/view_awb.php?user=canai_test&parola=test&awb=TSD21185178T";

$pdf = get_data($url);

echo $pdf;

?>