如何将 pdf 数据渲染成实际的 pdf 文件? (使用 PHP)

How do I render a pdf data into an actual pdf file? (Using PHP)

我得到的 return 就像 pdf 的实际内容,但是当我尝试创建一个 pdf 文件并将此数据粘贴到文本编辑器上并将其另存为 pdf 时,当我尝试打开它时,它只是说无法打开文件。

这是我尝试创建 pdf 文件的示例数据:

%PDF-1.2
%âãÏÓ
1 0 obj
<<
/Type /XObject
/Subtype /Image
/Name /Im1
/Width 195
/Height 114
/BitsPerComponent 8
/ColorSpace /DeviceRGB
/Length 27065
/Filter [/ASCII85Decode /FlateDecode]
>>
stream
8;Z,gH#-71)TI[R>H3E->-63kC9"#[HuOqQpQS50ind<:+n:mApBkAg0goK2+c%;h
CCELjp]hBc,j)'u:">P1>Fj@DSc,'sA`']Fl^i'C4`A($XM)?Uf&#l%TD*Wgf9l#E
"U,'791he`$O\i`n`*-B]LEc-O^KOI_re[CEa!0#R8No%(!OcW.Fl)&`:;1UW-N
90]]R,W!E"I:M/`G:R[WI>_#mNYrAKDr/,]%RddEh]=[l(g$YDe6LJNd`d2.UdMkP
V!j]Jp%07nJ5;3I6/s+-f:8?N#-"T$Ch*S,TV.q[#7g6i[_I1`mf!%Wg@b:5k7Wj/
a*Bsf-O'9hG5(_h)^)YVmhh6KNb)g7eu;Rq0JK_SmFpJi0YVu4<kGZCZ@;"ArtZ8&
Uc.WY09\jk$pen#g9ihUg)N;(psc9AIXZ`EVG+i#f\"hfbEckU32SU1p%//b[h@
G4u6/]JGSb1PR[gh5aV\+sS>+&Ej9#"Ea(eq8J7.X<@d"Sa=@R@`3kb'Y"k[N[s
MJMJ@kmX;]UT7J"*ZQ0TTgOSR2/3*s1\^=n6]ju*U,7Qrs7OY8nduNW5(*-u@)Uts
BP8f71htMgDPp3u#'qk)[((MZ1+T&dZC>s[EiGf&8hZ#@l'pBh/o<>7m"XI>@gI_a
0ekE0WGUCu2/Cb_4*I]Fpa-IE;RWm`ncu%RbY8(V"'DO!hnFMu,P<21Y@#%ZeZ)&+
6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GjWo34`@d,4JoaRPm-"e,'
^$P*%#CQOHk7n&J>P6T24*L>]RKk+OccKh>-lVSsbK8;cM2:`7pV94.c7iZm8qR<W
i[Q81i5(&JaNV\M5o4-b+#[rfi*-[tH0(=dBUf2C=]To$[slc;rpq,&._m.%3k6
h1`F@phshZ?ki`-JYgqi%VN6UN3GZ>^A.7Hj2SPio.9jb[\p&@QDQT8$W>]bCH$l?
'EX&3+i.&4^rjjV#\efn3Eo<VJ.q9$f[rB'CMQ@4+u>5!><O>9P0go=lDlVLCY,^Z
++;WRa'YL\+ZoSM:HpGhDnYgE/.l;n^OEk0a<M;$,r\iWYH51,&qo&G3uq.%LK>9t
^G$./#.o3>JHoGG74eZ3='pB`2KAf%,tjOPkQ:cmb<r?u?I]Y2+*r1d`_H&;cXFh6
1UDGjk,B$co=C.8o&@@eh0KX`+Thq7'pVa`QBr@&7sr7thXD/V$Q<_hK[^TQ@V/`*
44]iZD][XNA/eX.-ZnNn!Qd8_F4[?GQ)5o",Nm:p9_mo3"Oh8Ue'<Sq&hG1S+52t,
Z'0E]o'_X#Ie@i*!lhGf6]GNk0bqQ6`<0

我不确定我是否还需要解析数据或对其进行编码,或者 php 中是否有某种工具来呈现此类响应?

任何帮助将不胜感激。谢谢

您在这里发布的内容确实是 PDF 文件的一小部分。您在此处粘贴的是文件的开头(看起来合法)和 XObject 的第一部分 - 这是 PDF 语言,在这种情况下用于表示图像。如果您想对这个文件做一些有用的事情,请注意两点:

1) 您在这里拥有的只是很小的一部分 - 您需要完整的 PDF 文件才能对其进行任何操作。

2) 行结尾在 PDF 中很重要,因为文件格式适用于对象和文件末尾的交叉引用 table,其中包含所有对象的字节偏移量。因此,如果你搞砸了行尾,none 的偏移量仍然有效,你自己就有了一个损坏的 PDF 文件。如果您将传入的数据视为二进制数据并将其写入一个文件,通常您应该得到一个好的 PDF 文件(假设他们向您发送完整的文件而不是您在此处显示的片段)。