2014-02-10 186 views
0

我使用API​​来生成PDF文档。在此API文档,我有:如何使用API​​生成PDF文档?

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

实际上,它看起来像PDF,你是什么意思 “我开” 呢?你用记事本打开它? – Andrey

+0

由Adobe Reader打开pdf文件sample.pdf –

+1

您是否将内容类型设置为'application/pdf'? –

回答

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 I):

<?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; 

?>