将标准 Base64 编码流输出为 PDF 格式 (PHP) [英] Output a standard Base64 Encoded stream into PDF format (PHP)

查看:140
本文介绍了将标准 Base64 编码流输出为 PDF 格式 (PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Royal Mail Shipping API 生成打印的 PDF 标签,我的 PHP SoapClient 返回如下所示的有效响应(仅显示初始响应,因为整个响应很大).

I am using the Royal Mail Shipping API to generate printed PDF labels, my PHP SoapClient returns a valid response shown below (only shown the initial response as the whole response is huge).

%PDF-1.7 %äãÏÒ 4 0 obj <> stream xœ endstream endobj 3 0 obj 8 endobj 9 0 obj <> stream xÚí]bì*¤Höýoübƒ¤Æ-q²É>ø%Íë‚ÔFÒ<1ÆoÌúÝú¯ý?1Æ%Èa9Ò4QÌ!}üŠ ÆãS€ZÿŸ2Mô¨H}üßÇcŒ˜Z´½\¡´üý’y©1Æø$¨RÓd°úø’ÆÄŒ1Ægð´ ¨Š'ª°Z¾MCF1Æ}¥/¨{d˜ZQ•†Þ7Æ_P¢õ‘ kjŒ1.J¦ê"ÕÑŽ©,ž‹1ÆãNÿÅIü{}L%üÄcŒÑS Þª€êÁI"ÀÅÃcŒcHÚsïuP5Ð4Æ .ê2¤mbŒ1vU¼vè:ž>Æ<´¾1ÆØTŠûfÓ¢œÆcTŒ³wGF1Æ 

任何人都可以建议正确转换"这个 Base64 编码的 PDF 标签的最佳方法,以便我可以将其实际下载到浏览器.我下面的代码下载了 PDF 文件,但是当我尝试打开它时,文件大小始终为 57kb,我在 Acrobat Reader 中返回以下消息

Can anyone suggest the best method to 'convert' this Base64 encoded PDF label correctly so I can physically download this to the browser. My code below downloads the PDF file but when I attempt to open this the filesize is always 57kb and I returns the following message in Acrobat Reader

"There was an error opening this document. The file is damaged and could not be repaired."

我的代码如下:

$rm = new RoyalMailLabelRequest();
// provide shipment number, order tracking id, output format (e.g PDF/PNG)
$response = $rm->PrintLabel('TTT000358756GB', '276831601444829801', 'PDF');

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="doc-'.$document.'.pdf"');
$data = base64_decode($response);
file_put_contents('pdf/label.pdf', $data);

更新

当我尝试回应解码后的响应时,我得到以下信息……不确定这个响应发生了什么……奇怪.

UPDATE

When I try to echo the decoded response I get the following... not sure what is happening with this one.. odd.

$response = $rm->PrintLabel('TTT000358756GB', '276831601444829801', 'PDF');
$data = base64_decode($response);
echo $data;exit;

base64_decoded 后回显 $data 响应

Echo'd $data response after being base64_decoded

<1uï(n?Ëzx-‡}üX¥µêÿV­x7œ¡×¬¶·š›

如果对任何人有帮助,我还在 pastebin 上添加了我的代码返回的完整 base64_encode 响应http://pastebin.com/JEtmRURK

I've also added the full base64_encode response that is returned by my code here on pastebin if it helps anyone http://pastebin.com/JEtmRURK

推荐答案

排序 - 度过了漫长的一天.

Sorted - been a very long day.

我最终不需要对响应进行解码,因此以下内容将起作用(希望它可以帮助其他人进行 API 集成)

I didn't need to decode the response in the end so the following will work (hopefully it helps someone else doing the API integration)

function PrintLabelRequest($shipmentNumber, $transactionId)
{
   $rm = new RoyalMailLabelRequest();

   // function from library returns a response using SOAP
   $response = $rm->PrintLabel($shipmentNumber, $transactionId);

   // name the file & saved this label as a PDF in the following folder
   $filename = 'printedlabel-' . $shipmentNumber;
   file_put_contents(dirname(__FILE__) . '/labels/'. $filename .'.pdf', $response);
}

这篇关于将标准 Base64 编码流输出为 PDF 格式 (PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆