PHP + PDF,如何使用curl保存下载的PDF? [英] PHP + PDF, how to save a downloaded PDF using curl?

查看:1215
本文介绍了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 (字符串)我有所有的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

推荐答案

$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;

请尝试这个。

filesize readfile 接受文件作为参数。您提供的是字符串,而不是文件。

filesize and readfile accepts files as arguments. You are providing a string instead of a file.

这篇关于PHP + PDF,如何使用curl保存下载的PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆