在PHP中从url调用pdf时,Chrome“无法加载PDF文档”错误消息。但适用于Firefox和Edge

时间:2018-02-06 01:58:36

标签: php google-chrome pdf

我想在浏览器中使用PHP从REST服务中打开一个pdf文件,但该文件无法打开。 Chrome称,“无法加载PDF文档”。但在Firefox和Edge中正常工作。

这是我的print.php

<?php

header("Pragma: public"); // required
header('Content-Type: application/pdf');
header('Accept-Ranges: bytes');
header('Content-Transfer-Encoding: binary');

$data = ...; //definition of my json object

$baseUrl = 'http://www.website.com:8080/report/api/something/';
$newUrl = $baseUrl .'path';

$additional_headers = array(
    'Content-Type: application/json',
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $additional_headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$server_output = curl_exec($ch);

echo  $server_output;

?>

更新

我注意到,当我的报告中的记录数量超过一定数量时(总是在Chrome中,其他浏览器完美运行)会发生这种情况。但我仍然无法解决问题。

1 个答案:

答案 0 :(得分:-1)

最后,我找到了解决方案!!!

首先,我更改了我的CURL调用,将CURLOPT_RETURNTRANSFER更改为true然后,使用Postman调用我的PHP页面,我注意到结果的内容看起来像这样:

<br />
<b>Deprecated</b>:  Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in
<b>Unknown</b> on line
<b>0</b>
<br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent in
<b>Unknown</b> on line
<b>0</b>
<br />
<br />
<b>Warning</b>:  session_start(): Cannot send session cookie - headers already sent in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>4</b>
<br />
<br />
<b>Warning</b>:  session_start(): Cannot send session cache limiter - headers already sent in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>4</b>
<br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>5</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>24</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>77</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>77</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>77</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>77</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>77</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>77</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>77</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>77</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>77</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>77</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>77</b>
<br />
<br />
<b>Warning</b>:  Creating default object from empty value in
<b>C:\xampp\htdocs\project\print.php</b> on line
<b>77</b>
<br />
%PDF-1.5
%����
3 0 obj<
</ColorSpace/DeviceRGB/Subtype/Image/Height 515/Filter/DCTDecode/Type/XObject/Width 545/BitsPerComponent 8/Length 47996>>stream
���� ........................................... 

PDF正确地从REST服务下载了PDF,但由于这个原因,PHP显示太多错误Chrome无法打开PDF文件(因此,当我的PDF文件的数据数量为&lt; = 5时行显示在Chrome中,因为记录数越少,PHP启动的警告量就越低。为了解决这个问题,我把error_reporting(0)放在PHP文件的顶部(显然更好地修复了警告):

<?php

error_reporting(0); // to prevent show warning or errors, use carefully

header("Pragma: public"); // required
header('Content-Type: application/pdf');
...

然后,当我再次执行我的PHP页面时,页面的内容(如果只有PDF文件)

%PDF-1.5
%����
3 0 obj<
</ColorSpace/DeviceRGB/Subtype/Image/Height 515/Filter/DCTDecode/Type/XObject/Width 545/BitsPerComponent 8/Length 47996>>stream
���� ...........................................