Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

"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."

i read this paragraph in "Itext in action-second edition".(p 17).Can anyone please explain me what kind of differences the author's talking about.and reason why pdf format has this defect if i may say.

like image 871
programer8 Avatar asked Nov 18 '13 03:11

programer8


People also ask

Why does my document look different in PDF?

Reason: PDF and Microsoft Office and other document formats are totally different document formats. PDF is a fixed file format, it stores the location of all the elements including text characters, images, tables on a page. And it will also embed the font into the PDF data.

Why is the name of my PDF different when I open it?

Reason: The browser is simply reading the metadata that is saved in the actual PDF file as the document title, which may be different from the document file name. You may confirm this by opening the PDF file in Adobe Acrobat Reader > navigate to File > Properties.

Are all PDF files the same?

A single PDF can be opened on Mac or PC, Android or iPhone, Chromebook or Kindle, and you can rest assured that the file will look identical on all four, without even a minor variation.


1 Answers

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.

like image 196
Bruno Lowagie Avatar answered Sep 28 '22 10:09

Bruno Lowagie