使用 PHP 合并不同的 PDF 格式?
Merging Different PDF formats with PHP?
我正在尝试将几个 PDF 文件与 Setasign FPDI 合并。此软件包对某些 PDF 格式工作正常,但对其他格式无效。
我可以找到三种不同格式的 PDF。
格式 1:
%PDF-1.4
%´µ¶·
%
1 0 obj
<<
/Type /Catalog
/PageMode /UseNone
/ViewerPreferences 2 0 R
/Pages 3 0 R
/PageLayout /OneColumn
>>
格式 2:
--uuid:3c4caf6a-2a7e-4ca5-9e0a-63346610deae
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <1>
%PDF-1.4
%âãÏÓ
1 0 obj
<</ColorSpace/DeviceGray/Subtype/Image
格式 3:
2550 4446 2d31 2e34 0a25 aaab acad 0a34
2030 206f 626a 0a3c 3c0a 2f43 7265 6174
6f72 2028 4170 6163 6865 2046 4f50 2056
6572 7369 6f6e 2031 2e30 290a 2f50 726f
6475 6365 7220 2841 7061 6368 6520 464f
5020 5665 7273 696f 6e20 312e 3029 0a2f
4372 6561 7469 6f6e 4461 7465 2028 443a
3230 3136 3131 3130 3135 3437 3532 5a29
0a3e 3e0a 656e 646f 626a 0a35 2030 206f
FPDI 适用于格式 1,但不适用于格式 2。
当我尝试从另一个 PDF 合并网站合并来自 格式 2 的两个文件时,我得到了格式 3 的合并 pdf。
我的问题是如何将 2 个 格式 2 文件合并为 PHP 中的任何格式。
如果有人能解释这些格式,那就太好了。
"Format 2" 是损坏的文件,因为它包含无效的 header 数据,这些数据会损坏 PDF 中的字节偏移位置(FPDI 不会修复此类文件,但需要有效的 PDF)。
"Format 3" 只是一堆十六进制值,不是 PDF 文件。
感谢 Setasign 的回答,我已将无效格式清除为有效格式。
我正在使用简单的内容拆分。
public function parseRawResponse($raw, $from)
{
$positionMap = [
'PDF' => [ 'init' => "%PDF-1.4\n", 'end' => "\n%%EOF"]
];
$initPos = strpos($raw,$positionMap[$from]['init']);
$endPos = strrpos($raw, $positionMap[$from]['end']) + strlen($positionMap[$from]['end']);
$content = substr($raw, $initPos, ($endPos - $initPos));
return $content;
}
其中 $raw
是格式 2,$content
是 PDF 的实际内容。
我正在尝试将几个 PDF 文件与 Setasign FPDI 合并。此软件包对某些 PDF 格式工作正常,但对其他格式无效。
我可以找到三种不同格式的 PDF。
格式 1:
%PDF-1.4
%´µ¶·
%
1 0 obj
<<
/Type /Catalog
/PageMode /UseNone
/ViewerPreferences 2 0 R
/Pages 3 0 R
/PageLayout /OneColumn
>>
格式 2:
--uuid:3c4caf6a-2a7e-4ca5-9e0a-63346610deae
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary
Content-ID: <1>
%PDF-1.4
%âãÏÓ
1 0 obj
<</ColorSpace/DeviceGray/Subtype/Image
格式 3:
2550 4446 2d31 2e34 0a25 aaab acad 0a34
2030 206f 626a 0a3c 3c0a 2f43 7265 6174
6f72 2028 4170 6163 6865 2046 4f50 2056
6572 7369 6f6e 2031 2e30 290a 2f50 726f
6475 6365 7220 2841 7061 6368 6520 464f
5020 5665 7273 696f 6e20 312e 3029 0a2f
4372 6561 7469 6f6e 4461 7465 2028 443a
3230 3136 3131 3130 3135 3437 3532 5a29
0a3e 3e0a 656e 646f 626a 0a35 2030 206f
FPDI 适用于格式 1,但不适用于格式 2。
当我尝试从另一个 PDF 合并网站合并来自 格式 2 的两个文件时,我得到了格式 3 的合并 pdf。
我的问题是如何将 2 个 格式 2 文件合并为 PHP 中的任何格式。
如果有人能解释这些格式,那就太好了。
"Format 2" 是损坏的文件,因为它包含无效的 header 数据,这些数据会损坏 PDF 中的字节偏移位置(FPDI 不会修复此类文件,但需要有效的 PDF)。
"Format 3" 只是一堆十六进制值,不是 PDF 文件。
感谢 Setasign 的回答,我已将无效格式清除为有效格式。 我正在使用简单的内容拆分。
public function parseRawResponse($raw, $from)
{
$positionMap = [
'PDF' => [ 'init' => "%PDF-1.4\n", 'end' => "\n%%EOF"]
];
$initPos = strpos($raw,$positionMap[$from]['init']);
$endPos = strrpos($raw, $positionMap[$from]['end']) + strlen($positionMap[$from]['end']);
$content = substr($raw, $initPos, ($endPos - $initPos));
return $content;
}
其中 $raw
是格式 2,$content
是 PDF 的实际内容。