PHP将字节数组作为字符串转换为pdf

时间:2018-09-25 05:24:09

标签: php arrays string pdf

我有一个来自SOAP Webservice的php中的字符串格式的字节数组,想要转换为pdf。

Web服务响应数据

<RunProcessResponse IsReport="true" ReportFormat="pdf" xmlns="http://ie.org/ADInterface/1_0">
            <Data>255044462D312E350A25E2E3CFD30A342030206F626A0A3C3C2F436F6C6F7253706163652F4465766963655247422F537562747970652F496D6167652F486569676874203134372F46696C7465722F4443544465636F64652F547970652F584F626A6563742F5769647468203132342F42697473506572436F6D706F6E656E7420382F4C656E67746820383333303E3E73747265616D0AFFD8FFE000104A46494600010101006000600000FFE1006C45786966000049492A0008000000030012010300010000000100000031010200070000003200000069870400010000003A00000000000000476F6F676C650000030000900700040000003032323002A00400010000007C00000003A00400010000009300000000000000FFDB0043000201010201010202020202020202030503030303030604040305070607070706070708090B0908080A0807070A0D0A0A0B0C0C0C0C07090E0F0D0C0E0B0C0C0CFFDB004301020202030303060303060C0807080C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0CFFC00011080093007C03012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131</Data>
</RunProcessResponse>

PHP代码:

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="service.pdf"');
echo base64_decode($byte_string);

JAVA中Web服务返回pdf响应

Java代码:

file_type ="pdf";               
RunProcessResponse r= res.addNewRunProcessResponse();                        
r.setData(java.nio.file.Files.readAllBytes(pi.getPDFReport().toPath()));
r.setReportFormat(file_type);

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:2)

一项快速测试表明该字符串被编码为Base16 (hexadecimal)(一个奇怪的选择)。

PHP在hex2bin()

中提供了一个方便的功能
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="service.pdf"');
echo hex2bin($byte_string);
exit;