Why are PDF files different even if the content is the same?

╄→尐↘猪︶ㄣ 提交于 2019-11-28 01:25:05

Files that are created on a different moment, have a different value for the CreationDate and they have different file identifiers (having two files, created on a different moment, should have a different ID as defined in the PDF specification).

The file identifier is usually a hash created based on the date, a path name, the size of the file, part of the content of the PDF file (e.g. the entries in the information dictionary). I quote ISO-32000-1:

The calculation of the file identifier need not be reproducible; all that matters is that the identifier is likely to be unique. For example, two implementations of the preceding algorithm might use different formats for the current time, causing them to produce different file identifiers for the same file created at the same time, but the uniqueness of the identifier is not affected.

File identifiers are mandatory when encrypting a document because they are used in the encryption process. As a result, encrypted PDF files with different file identifiers will have streams that are completely different. This is not a flaw, this is by design. I'm a member of the ISO committee that is working on the PDF 2.0 specification and I can assure you that there are no plans to change this. Files created on a different point in time will be different, even when using the same code. (I'm also the author of the book you refer to.)

The ISO specification also allows other differences. For instance: the syntax that is used to display graphics and text on a page can be reorganized for whatever reason. See section 8.2 of ISO-32000-1 where it says:

The important point is that there is no semantic significance to the exact arrangement of graphics state operators. A conforming reader or writer of a PDF content stream may change an arrangement of graphics state operators to any other arrangement that achieves the same values of the relevant graphics state parameters for each graphics object.

When processing a PDF content stream a PDF processor may change an arrangement of graphics state operators to any other arrangement that achieves the same values of the relevant graphics state parameters for each graphics object. This can be done to optimize the page, to make it render more quickly, to make it easier to debug, to improve the compression, or for any other reason.

Another reason why two seemingly identical PDFs may differ internally concerns PDF dictionaries. The order of keys in a dictionary doesn't have any importance in PDF. Software that implements the specification to the letter, will for instance use a HashMap to story key/value pairs. Depending on the JVM, the same code can lead to two PDFs with dictionaries that are semantically identical, but of which the entries are sorted in a different way. This is not an error. This is completely compliant with ISO-32000-1.

Important: the internal differences between two PDF files created using the same code, but on a different moment, may not result in a visual difference when opening the document in a PDF viewer or when printing the document on paper.

In addition to the other answers, do not forget that there are always different ways to accomplish the same result in programming. Think about when HTML5 hit the scene.

<script>
   alert("Hey");
</script>

versus the older way of using JS....

<SCRIPT type="text/javascript">
  alert("Hey");
</script>

Just a not that there are always different ways to produce the same effect and two different people will use two different methods. That is why REST API's were created.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!