How to get the diff of two PDF files using Python?

I do not know your use case, but for regression tests of script which generates pdf using reportlab, I do diff pdfs by

  1. Converting each page to an image using ghostsript
  2. Diffing each page against page image of standard pdf, using PIL

e.g

im1 = Image.open(imagePath1)
im2 = Image.open(imagePath2)

imDiff = ImageChops.difference(im1, im2)

This works in my case for flagging any changes introduced due to code changes.


What do you mean by "difference"? A difference in the text of the PDF or some layout change (e.g. an embedded graphic was resized). The first is easy to detect, the second is almost impossible to get (PDF is an VERY complicated file format, that offers endless file formatting capabilities).

If you want to get the text diff, just run a pdf to text utility on the two PDFs and then use Python's built-in diff library to get the difference of the converted texts.

This question deals with pdf to text conversion in python: Python module for converting PDF to text.

The reliability of this method depends on the PDF Generators you are using. If you use e.g. Adobe Acrobat and some Ghostscript-based PDF-Creator to make two PDFs from the SAME word document, you might still get a diff although the source document was identical.

This is because there are dozens of ways to encode the information of the source document to a PDF and each converter uses a different approach. Often the pdf to text converter can't figure out the correct text flow, especially with complex layouts or tables.

Tags:

Python

Pdf