[英]Using PHP CURL to display PDF file is just producing strange text
我正在使用PHP CURL来访问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);
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
在.pdf应该显示的页面上,是以下代码:
<body>
<p>
<object id='mypdf' type='application/pdf' width='100%' height='90%' data='sample_report.pdf'>
</object>
</p>
</body>
有没有人有什么建议?
尝试一下(保持前三行相同)
$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'));
header('Content-type: application/pdf');
$result = curl_exec($ch);
curl_close($ch);
echo $result;
我注释掉的那一行似乎没有做任何事情。 我添加了“标题”行。 如果我把那条线拿出来,我会像你一样乱码。 如果仍然说“标题已发送”,则必须在其他地方设置标题。
编辑:
我也建议更改此:
<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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.