7

欢迎

我在页面上保存下载的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 (string) 我有所有的 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);

不幸的是,我不知道如何改变它们以使其工作......我请求你的帮助。谢谢

4

2 回答 2

17

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;
于 2013-04-09T09:08:54.710 回答
2

也许是:

// ...
$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);

我希望我有所帮助!

于 2013-04-09T08:54:14.653 回答