2014-02-10 278 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; 

?>