【发布时间】:2013-04-09 08:45:20
【问题描述】:
欢迎
我在将下载的 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);
不幸的是,我不知道如何更改它们以使其正常工作......我请求您的帮助。谢谢
【问题讨论】:
-
我认为您的代码中出现了一些错误。将您的代码包装在一个 try-catch 块中并添加一些调试步骤。
-
我没有测试,但我认为
filesize()和readfile()你需要一个真实的文件路径而不是字符串的内容。