简体   繁体   中英

Generate two exactly equal PDFs with Java or iText

I have generated two PDFs by using this example (FirstPDF) removing the "new Date()" sentence.

They look equal but when calculating a md5 hash on them, they are really different.

I've examinated them and they register a creationDate , even if the sentence document.addCreationDate() is not included in the source code.

The question is very simple: is it possible in any way with any API to generate two PDFs that are exactly equal byte to byte?

This is how it SHOULD be. Apart from the date in the metadata, there's also a unique ID that is added every time a PDF is generated from scratch.

from

If you need two identical files giving you the same MD5 hash, why not copy one that's been created already?

If you need to create two identical files by two separate API calls, then you can use any PDF-creating API that's worth it's money:

  • Because each of these APIs have contain a call to set the creation and the modification date of the output PDF to any value that you need... Just don't let this setting happen automatically! Use the same setting two times.
  • Attention! PDF also supports the setting of a document UUID. Some of these APIs also do set an arbitrary UUID for each new document (which would break your MD5 hash), unless you actively prevent this to happen.

As described here , files are not equals because they have different 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 (eg the entries in the information dictionary).
.
File identifiers are involved (and mandatory) in document encryption. As a result, encrypted PDF files with different file identifiers will have streams that are completely different.

By design, you should never be able to create two identical PDFs using the same code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM