【问题标题】:How to generate plain-text source-code PDF examples that work in a document viewer?如何生成在文档查看器中工作的纯文本源代码 PDF 示例?
【发布时间】:2012-06-11 12:27:49
【问题描述】:

我刚刚找到Adobe Forums: Simple Text String Example in specification broken. 的帖子,所以我对寻找纯文本源代码 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

我在哪里做错了?

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

【问题讨论】:

    标签: linux pdf command-line pdf-generation


    【解决方案1】:

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

    编辑:另请参阅Introduction to PDF - GNUpdf (archive) 以获得类似的更详细的示例

    所以这里是完整的代码:

    %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
    

    所以要真正得到一个正确的例子,正如Adobe Forums: Simple Text String Example in specification broken. 指出的那样,需要重建外部参照表(具有正确的字节偏移量)。

    为了做到这一点,我们可以使用pdftk to "Repair a PDF's Corrupted XREF Table and Stream Lengths (If Possible)":

    $ pdftk hello.pdf output hello_repair.pdf
    

    ...现在hello_repair.pdfevince 中打开没有问题-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
    

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

    【讨论】:

      【解决方案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 
      

      【讨论】:

      • 确实-感谢@pipitas;我也意识到了这一点,并记录在this post;干杯!
      • @KurtPfeifle 我发现对于简单的 PDF 文件(例如没有压缩流),只需放置连续的对象 id,跳过外部参照表并将 startxref 0 %%EOF 用作 acrobat 阅读器的魅力打开文件并生成一个新表。
      • @yms:是的,但是“保存”的 PDF 可能包含与您的原始 PDF 源代码完全不同的代码。因此,对于您想要编写用作学习/教学/学习材料的 PDF 文件的情况,它不是一个选项。
      猜你喜欢
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      相关资源
      最近更新 更多