5

我刚刚发现Adob​​e 论坛:规范中的简单文本字符串示例已损坏。,所以我对查找纯文本源代码 PDF 示例产生了兴趣。

所以,通过那个帖子,我最终发现:

PDF 1.7 规范在第 699 页附录“_Annex H(信息性)示例 PDF 文件”;从那里开始,我想尝试“H.3 简单文本字符串示例”(“经典 Hello World”)。

所以我尝试将其另存为hello.pdf(_除了注意当您从 PDF32000_2008.pdf 复制时,您可能会得到“ %PDF-1. 4” - 即在 之后插入的空格1.,必须删除_):

%PDF-1.4
1 0 obj
  << /Type /Catalog
      /Outlines 2 0 R
      /Pages 3 0 R
  >>
endobj

2 0 obj
  << /Type /Outlines
      /Count 0
  >>
endobj

3 0 obj
  << /Type /Pages
      /Kids [ 4 0 R ]
      /Count 1
  >>
endobj

4 0 obj
  << /Type /Page
      /Parent 3 0 R
      /MediaBox [ 0 0 612 792 ]
      /Contents 5 0 R
      /Resources << /ProcSet 6 0 R
      /Font << /F1 7 0 R >>
  >>
>>
endobj

5 0 obj
  << /Length 73 >>
stream
  BT
    /F1 24 Tf
    100 100 Td
    ( Hello World ) Tj
  ET
endstream
endobj

...我正在尝试打开它:

evince hello.pdf

...但是,evince无法打开它:“无法打开文档/PDF文档已损坏”;并且:

Error: PDF file is damaged - attempting to reconstruct xref table...
Error: Couldn't find trailer dictionary
Error: Couldn't read xref table

我还检查qpdf

$ qpdf --check hello.pdf
WARNING: hello.pdf: file is damaged
WARNING: hello.pdf: can't find startxref
WARNING: hello.pdf: Attempting to reconstruct cross-reference table
hello.pdf: unable to find trailer dictionary while recovering damaged file

我哪里错了?

非常感谢您的任何答案,
干杯!

4

2 回答 2

2

啊该死的——我只复制了一部分代码;OP 代码是 pg 701 上的代码 - 然后有一个让我感到困惑的页脚;否则代码在 pg 702 上继续:/

编辑:另请参阅PDF 简介 - GNUpdf存档)以获取类似的更详细的示例

所以这里是完整的代码:

%PDF-1.4
1 0 obj
  << /Type /Catalog
      /Outlines 2 0 R
      /Pages 3 0 R
  >>
endobj

2 0 obj
  << /Type /Outlines
      /Count 0
  >>
endobj

3 0 obj
  << /Type /Pages
      /Kids [ 4 0 R ]
      /Count 1
  >>
endobj

4 0 obj
  << /Type /Page
      /Parent 3 0 R
      /MediaBox [ 0 0 612 792 ]
      /Contents 5 0 R
      /Resources << /ProcSet 6 0 R
      /Font << /F1 7 0 R >>
  >>
>>
endobj

5 0 obj
  << /Length 73 >>
stream
  BT
    /F1 24 Tf
    100 100 Td
    ( Hello World ) Tj
  ET
endstream
endobj

6 0 obj
  [ /PDF /Text ]
endobj

7 0 obj
  << /Type /Font
    /Subtype /Type1
    /Name /F1
    /BaseFont /Helvetica
    /Encoding /MacRomanEncoding
  >>
endobj

xref
0 8
0000000000 65535 f
0000000009 00000 n
0000000074 00000 n
0000000120 00000 n
0000000179 00000 n
0000000364 00000 n
0000000466 00000 n
0000000496 00000 n

trailer
  << /Size 8
    /Root 1 0 R
  >>
startxref
625
%%EOF

确实,正如错误消息所说,缺少外部参照部分

然而,这还没有结束——虽然这个文档会在 中打开evince,但 evince 仍然会抱怨:

$ evince hello.pdf 
Error: PDF file is damaged - attempting to reconstruct xref table...

......所以会qpdf

$ qpdf --check hello.pdf
WARNING: hello.pdf: file is damaged
WARNING: hello.pdf (file position 625): xref not found
WARNING: hello.pdf: Attempting to reconstruct cross-reference table
checking hello.pdf
PDF Version: 1.4
File is not encrypted
File is not linearized
WARNING: hello.pdf (object 5 0, file position 436): attempting to recover stream length

因此,实际上要获得一个适当的示例,如Adob​​e 论坛:规范中的简单文本字符串示例已损坏。指出,需要重建外部参照表(具有正确的字节偏移)。

为了做到这一点,我们可以使用pdftk修复 PDF 损坏的外部参照表和流长度(如果可能) ”:

$ pdftk hello.pdf output hello_repair.pdf

...现在hello_repair.pdf打开evince没有问题 - 并qpdf报告:

$ qpdf --check hello_repair.pdf
checking hello_repair.pdf
PDF Version: 1.4
File is not encrypted
File is not linearized
No errors found

好吧,希望这对某人有所帮助,
干杯!

于 2012-06-07T12:53:09.103 回答
2

您应该在文件末尾附加一个(语法正确)xreftrailer部分。这意味着:PDF 中的每个对象都需要外部参照表中的一行,即使字节偏移量没有正确说明也是如此。然后 Ghostscript、pdftk 或 qpdf 可以重新建立正确的外部参照并渲染文件:

[...]
endobj
xref 
0 8 
0000000000 65535 f 
0000000010 00000 n 
0000000020 00000 n 
0000000030 00000 n 
0000000040 00000 n 
0000000050 00000 n 
0000000060 00000 n 
0000000070 00000 n 
trailer 
<</Size 8/Root 1 0 R>> 
startxref 
555 
%%EOF 
于 2012-06-07T13:03:17.227 回答