PHP + PDF, how to save a downloaded PDF using curl?(PHP + PDF,如何使用 curl 保存下载的 PDF?)
问题描述
欢迎
我在将下载的 pdf 保存在页面上时遇到了一点问题.要下载 pdf,我使用 Curl:
I have a little problem with saving the downloaded pdf on the page. To download pdf I use 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:
Now in $Result(string) i have all PDF file content. And now begins my problem. I would like to save the downloaded 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 文件时,我得到一个空白文档.也许问题出在以下几行:
Unfortunately, when I save or open a new PDF file, I get a blank document. Perhaps the problem is with the last lines of:
header('Content-Length: '.filesize($Result));
readfile($Result);
不幸的是,我不知道如何更改它们以使其正常工作......我请求您的帮助.谢谢
Unfortunately, I do not know what to change them to make it work ... I ask for your help. Thanks
推荐答案
两者 filesize 和 readfile 接受文件作为参数.您提供的是字符串而不是文件.
Both filesize and readfile accepts files as arguments. You are providing a string instead of a file.
请试试这个.
$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;
这篇关于PHP + PDF,如何使用 curl 保存下载的 PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP + PDF,如何使用 curl 保存下载的 PDF?
- 带有通配符的 Laravel 验证器 2021-01-01
- Laravel 仓库 2022-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01