根据规格的最小 PDF 大小

Minimal PDF size according to specs

我正在阅读 PDF 规范,我对它的结构有一些疑问。

首先,文件签名是%PDF-n.m(8字节)。 在那之后,文档说 可能 至少 4 个字节的二进制数据(但也可能没有)。文档没有说明可能有多少个二进制字节,所以这是我的第一个问题。如果我试图解析 PDF 文件,我应该如何解析该部分?我怎么知道那里放了多少二进制字节(如果有的话)?我应该在哪里停止解析?

在那之后,应该有正文、外部参照 table 和预告片以及 %%EOF

PDF 的最小文件大小是多少,假设 PDF 文件中什么都没有(没有任何对象),并且假设文件开头不包含可选的二进制字节部分?

第三个也是最后一个问题:如果有多个正文+外部参照+预告片部分,那么在 %%EOF 指向之前会偏移哪里?第一个还是最后一个xreftable?

First of all, the file signature is %PDF-n.m (8 bytes). After that the docs says there might be at least 4 bytes of binary data (but there also might not be any). The docs don't say how many binary bytes there could be, so that is my first question. If I was trying to parse a PDF file, how should I parse that part? How would I know how many binary bytes (if any) where placed in there? Where should I stop parsing?

你有哪些文档? PDF 规范 ISO 32000-1 说:

If a PDF file contains binary data, as most do (see 7.2, "Lexical Conventions"), the header line shall be immediately followed by a comment line containing at least four binary characters—that is, characters whose codes are 128 or greater.

因此,那些至少4个字节的二进制数据没有紧跟在没有任何结构的文件签名之后,而是在评论!这意味着他们是

  1. 前面是 %(开始注释,即无论如何在解析时必须忽略的数据)和
  2. 后跟 end-of-line,即 CR、LF 或 CR LF。

所以解析的时候很容易识别。特别是它只是注释行的特例,没有什么需要特别处理的。

(唉,我刚看到你,@Jongware 在我写这篇文章的时候在评论中清除了这一点...)

What could be the minimal file size of a PDF, assuming there isn't anything at all (no objects, whatsoever) in the PDF file and assuming the file doesn't contain the optional binary bytes section at the beginning?

如果没有对象,则您没有 PDF 文件,因为 PDF 文件中需要某些对象,尤其是目录。那么您是指最小的有效 PDF 文件吗?

正如您所说,您的意思确实是最小有效 PDF。

请查看问题 What is the smallest possible valid PDF? on Whosebug, there are some attempts to create minimal PDFs adhering more or less strictly to the specification. Reading e.g. @plinth's answer 您将看到不再是 PDF 但仍被 Adob​​e 接受的内容 Reader。

Third and last question: If there were more than one body+xref+trailer sections, where would be offset just before the %%EOF be pointing to?

通常这将是最后一个交叉引用 table/stream 因为通常的用例是

  • 您从只有一个交叉引用部分的 PDF 开始;
  • 您附加一个增量更新,其中交叉引用部分指向原始 previous%%EOF 之前的新偏移量指向该新交叉引用;
  • 您附加了另一个增量更新,其中交叉引用部分指向第一个更新的交叉引用作为 previous%%EOF 之前的新偏移量指向最新的交叉引用;
  • 等...

例外情况是 线性化文档,其中 %%EOF 之前的偏移量指向初始交叉引用,后者又指向末尾的部分文件的 上一个 。详情请参阅。 ISO 32000-1.

的附件 F

当然,您可以对线性化文档应用增量更新,因此您可以使用混合形式。

一般来说,解析器最好能够解析任何顺序的部分交叉引用。别忘了,不仅有交叉引用部分,还有交叉引用流。