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

Tags: inspect PDFiText 5

I've read the following paragraph in "iText in Action - Second Edition" (p 17).

"There's usually more than one way to create PDF documents that look like identical twins when opened in a PDF viewer. And even if you create two identical PDF documents using the exact same code, there will be small differences between the two resulting files. That's inherent to the PDF format."

Can anyone please explain me what kind of differences the author's talking about and the reason why the PDF format has this defect if I may say.

Posted on StackOverflow on Nov 18, 2013 by programer8

Files that are created on a different moment, have a different value for the CreationDate and they have different file identifiers.

Two files, created on a different moment, should have a different ID. 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.

The ISO specification also allows other differences. For instance: when introducing a font subset, the name of the font is prefixed with a tag that consists of six upper-case letters. The choice of these letters is arbitrary and usually chosen at random. Two identical documents that are generated sequentially may result in different tags for the same font subset.

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.

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.

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.