9

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

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
programer8
  • 567
  • 1
  • 6
  • 17
  • I don't know the technical reason but it probably just comes down to how everything gets processed and then rendered. The differences are probably just issues with alignment and positions/spacing of things. – gloomy.penguin Nov 18 '13 at 04:10
  • 3
    *why pdf format has this defect* - first of all this is not a *defect* but merely a property of pdf. That been said pdf simply is a very flexible format allowing you to describe the same content in different ways. Furthermore it contains workflow information and ids designed to be unique for identification purposes. – mkl Nov 18 '13 at 05:25
  • 1
    no i think it is to be considered as a defect.what if the user wants the pdf to look exactly similar.i know about the advantages of pdf. i am just considered about the case in that statement. – programer8 Nov 18 '13 at 05:51
  • 2
    "Same code = small differences" can only mean `trailer`'s `ID` and `Info`'s `CreationDate` entries -- if they are put there automatically. Otherwise it makes no sense. – user2846289 Nov 18 '13 at 06:21
  • You can also get timestamps in the font files. – mark stephens Nov 18 '13 at 07:29
  • *i think it is to be considered as a defect* - I assume @Bruno's answer made clear why it isn't. – mkl Nov 18 '13 at 12:56

2 Answers2

11

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks Mr.Lowagie. So in short file identifiers that are generated based on creationDate of the document are different so these files are different.Can you please point out the kind of differences that the documents might have? visual differences in particular. – programer8 Nov 18 '13 at 08:45
  • 1
    I'm not sure how to interpret "visual differences". I've updated my answer. There will be visual differences when looking at the file in a text editor, but there shouldn't be any visual differences when looking at the file in a PDF viewer. – Bruno Lowagie Nov 18 '13 at 09:35
0

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.

morantis
  • 106
  • 8