简体   繁体   English

使用PHP CURL显示PDF文件只会产生奇怪的文本

[英]Using PHP CURL to display PDF file is just producing strange text

I'm using PHP CURL to access PDF files, and everything is working properly with the exception of the end result being unreadable. 我正在使用PHP CURL来访问PDF文件,除了最终结果不可读以外,其他所有功能都正常运行。 My code is as follows: 我的代码如下:

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

When I just manually enter the URL into the browser, the PDF properly displays. 当我只是在浏览器中手动输入URL时,PDF会正确显示。 But when using CURL, it displays pages of gibberish text. 但是,当使用CURL时,它将显示乱码页面。

Looking at the code view in the browser, I see a lot of lines like these: 在浏览器中查看代码视图,我看到很多类似下面的行:

5 0 obj
<</Length 6 0 R/Filter /FlateDecode>>
stream

And on the page where the .pdf should display, is the following code: 在.pdf应该显示的页面上,是以下代码:

<body>
    <p>
      <object id='mypdf' type='application/pdf' width='100%' height='90%' data='sample_report.pdf'>
      </object>
   </p>
</body>

Does anyone have any suggestions? 有没有人有什么建议?

Try it with this (keep your first 3 lines the same) 尝试一下(保持前三行相同)

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

The line I commented out doesn't appear to be doing anything. 我注释掉的那一行似乎没有做任何事情。 I added the "header" line. 我添加了“标题”行。 If I take that line out, I get a page of gibberish as you do. 如果我把那条线拿出来,我会像你一样乱码。 If it is still saying "headers already sent", there must be something setting a header elsewhere. 如果仍然说“标题已发送”,则必须在其他地方设置标题。

EDIT: 编辑:

I would also suggest changing this: 我也建议更改此:

<object id='mypdf' type='application/pdf' width='100%' height='90%' data='sample_report.pdf'></object>

To this: 对此:

<iframe src="sample_report.pdf" width="100%" height="90%"></iframe>

Or, if your PHP script is called sample_report.pdf (for instance) try changing iframe src to sample_report.php. 或者,如果您的PHP脚本名为sample_report.pdf(例如),请尝试将iframe src更改为sample_report.php。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM