簡體   English   中英

PHP + PDF,如何使用curl保存下載的PDF?

[英]PHP + PDF, how to save a downloaded PDF using curl?

歡迎

將下載的pdf保存在頁面上有一點問題。 要下載pdf,我使用Curl:

$CurlConnect = curl_init();
curl_setopt($CurlConnect, CURLOPT_URL, 'http://website.com/invoices/download/1');
curl_setopt($CurlConnect, CURLOPT_POST,   1);
curl_setopt($CurlConnect, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($CurlConnect, CURLOPT_POSTFIELDS, $request);
curl_setopt($CurlConnect, CURLOPT_USERPWD, $login.':'.$password);
$Result = curl_exec($CurlConnect);

現在在$ Result (字符串)中我擁有所有PDF文件內容。 現在開始我的問題。 我想保存下載的pdf:

header('Cache-Control: public'); 
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="new.pdf"');
header('Content-Length: '.filesize($Result));
readfile($Result);

不幸的是,當我保存或打開一個新的PDF文件時,我得到一個空白文檔。 也許問題出在最后幾行:

header('Content-Length: '.filesize($Result));
readfile($Result);

不幸的是,我不知道要改變它們以使其發揮作用...我請求你的幫助。 謝謝

filesizereadfile都接受文件作為參數。 您提供的是字符串而不是文件。

請試試這個。

$CurlConnect = curl_init();
curl_setopt($CurlConnect, CURLOPT_URL, 'http://website.com/invoices/download/1');
curl_setopt($CurlConnect, CURLOPT_POST,   1);
curl_setopt($CurlConnect, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($CurlConnect, CURLOPT_POSTFIELDS, $request);
curl_setopt($CurlConnect, CURLOPT_USERPWD, $login.':'.$password);
$Result = curl_exec($CurlConnect);

header('Cache-Control: public'); 
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="new.pdf"');
header('Content-Length: '.strlen($Result));
echo $Result;

也許那樣:

// ...
$Result = curl_exec($CurlConnect);
$file = 'file.pdf';
$fileName = 'fileName.pdf';
file_put_contents($file, $Result);

然后:

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');

readfile($file);

我希望我幫忙!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM