Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

pdf generation - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...