【发布时间】:2021-03-07 15:16:16
【问题描述】:
我在浏览器中从 REST API 响应打开 PDF 文档时遇到问题。
我的PHP函数如下:
<?php
function printdocument(){
$documentArray = array(
array('doc' => array('id' => '20613')),
);
#-> The JSON data.
$jsonData = array(
'userName' => 'username',
'password' => 'password',
'paperSize' => 'A4',
'parcels' => $documentArray
);
$jsResponse = apiRequest(https://api.call.com/print/', $jsonData);
header("Content-type: application/octet-stream");
header('HTTP/1.1 200 OK');
header('Content-type:application/pdf');
echo $jsResponse;
}
?>
几乎所有浏览器都收到错误消息:
Error
Failed to load PDF document.
【问题讨论】:
-
您确定 api 返回的是 pdf 文档而不是 base64 文本。如果是 base64,你应该先运行 base64_decode。并且无需设置多个内容类型标头。试试这个 header('Content-type: ' . 'application/octet-stream'); header('Content-Disposition: ' . '附件; filename=filename.pdf');回声 $jsResponse;