[英]PHP : Saving base64 encoded PDF file
我正在尝试从响应中保存PDF文件。 这是API文档
Creates a shipping label for a given order. The labelData field returned in the response is a base64 encoded PDF value. Simply decode and save the output as a PDF file to retrieve a printable label
Creates a shipping label for a given order. The labelData field returned in the response is a base64 encoded PDF value. Simply decode and save the output as a PDF file to retrieve a printable label
。
这是我保存pdf文件的代码。
$response = json_decode(curl_exec($ch));
$data = base64_decode($response->labelData);
header('Content-Type: application/pdf');
//echo $data;
$path = 'E:/newly_created_file'.time().'.pdf';
file_put_contents($path,$data);
当我在浏览器中echo $data
时,它显示如下响应
%PDF-1.4 % 2 0 obj <>stream x + r 26S0 4SI r *T0T0BC]#=]ccC=# \\ 4C | @.GF endstream endobj 4 0 obj <>>>/Contents 2 0 R/Parent
当我保存数据时,它会生成一个无法打开的空pdf文件。
怎么了
更新:Respose具有正确的数据,如所附图片所示
仅当您在浏览器中显示pdf文件而不存储在文件中时,才需要此标头。
header('Content-Type: application/pdf');
去掉它。
没有关于API数据的信息,很难找到问题。 执行代码:
$response = json_decode(curl_exec($ch));
echo $response->labelData;
exit;
并在此处发布结果,因此我们可以尝试找出问题所在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.