如何生成在文档查看器中工作的纯文本源代码 PDF 示例?

sda*_*aau 5 linux pdf command-line pdf-generation

我刚刚发现帖子Adobe 论坛:规范中的简单文本字符串示例已损坏。,所以我对查找纯文本源代码 PDF 示例很感兴趣。

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

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

所以我试着把它保存为hello.pdf(_except 当你从 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
Run Code Online (Sandbox Code Playgroud)

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

evince hello.pdf
Run Code Online (Sandbox Code Playgroud)

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

Error: PDF file is damaged - attempting to reconstruct xref table...
Error: Couldn't find trailer dictionary
Error: Couldn't read xref table
Run Code Online (Sandbox Code Playgroud)

我还检查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
Run Code Online (Sandbox Code Playgroud)

我哪里出错了?

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

Kur*_*fle 2

您应该在文件末尾附加一个(语法正确的)xrefand部分。trailer这意味着: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 
Run Code Online (Sandbox Code Playgroud)