使用PHP CURL显示PDF文件只会产生奇怪的文本
我正在使用PHPCURL访问PDF文件,除了最终结果不可读之外,其他一切都正常工作。我的代码如下:使用PHP CURL显示PDF文件只会产生奇怪的文本,php,curl,pdf-generation,Php,Curl,Pdf Generation,我正在使用PHPCURL访问PDF文件,除了最终结果不可读之外,其他一切都正常工作。我的代码如下: $cookie = 'cookies.txt'; $timeout = 30; $url = "http://www.somesite.com/sample_report.pdf"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); cur
$cookie = 'cookies.txt';
$timeout = 30;
$url = "http://www.somesite.com/sample_report.pdf";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Encoding: none','Content-Type: application/pdf'));
$result = curl_exec($ch);
echo $result;
当我在浏览器中手动输入URL时,PDF会正确显示。但是当使用CURL时,它会显示乱七八糟的文本页面
查看浏览器中的“代码”视图,我看到许多类似于以下内容的行:
5 0 obj
<</Length 6 0 R/Filter /FlateDecode>>
stream
50对象
流动
在.pdf应显示的页面上,有以下代码:
<body>
<p>
<object id='mypdf' type='application/pdf' width='100%' height='90%' data='sample_report.pdf'>
</object>
</p>
</body>
有人有什么建议吗?试试这个(保持前3行不变)
我注释掉的那句话似乎没有任何作用。我添加了“标题”行。如果我把那句话删掉,我会像你一样胡言乱语。如果它仍然在说“headers ready sent”,那么肯定有什么东西在别处设置了一个header
编辑:
我还建议改变这一点:
<object id='mypdf' type='application/pdf' width='100%' height='90%' data='sample_report.pdf'></object>
为此:
<iframe src="sample_report.pdf" width="100%" height="90%"></iframe>
或者,如果您的PHP脚本名为sample_report.pdf(例如),请尝试将iframe src更改为sample_report.PHP。这样就消除了所有乱七八糟的文本,但它没有显示任何错误或其他相关内容。这只是为了查看在显示pdf之前是否发生了任何错误。我想不是。你可以从出口出去。更多建议,请参见修订后的答案。当我使用标题进行修订时,会出现“标题已发送”的错误。您可以发布更多代码吗?在执行发布的代码之前是否使用header()?看看这是否有什么不同。
<iframe src="sample_report.pdf" width="100%" height="90%"></iframe>