views:

9698

answers:

10

I need to compare large count of PDF files for it optical content. Because the PDF files was created on different platforms and with different versions of the software there are structural differences. For example:

  • the chunking of text can be different
  • the write order can be different
  • the position can be differ some pixels

It should compare the content like a human people and not the internal structure. I want test for regressions between different versions of the PDF generator that we used.

+11  A: 

A partial answer would be to use pdftotext and compare the text contained.

Sklivvz
But this will ignore all non text informations like lines, boxes, pictures, charts, etc. I think also that it not show the optical positions of text else the structural position.
Horcrux7
I agree, it is not a sufficient criteria. On the other hand it is a necessary criteria, therefore it is adequate as a unit test.
Sklivvz
You can always add a better unit test later!
Sklivvz
If there are images on pages, and you want a human-like evaluation for those, there's not much you can do but have a human compare those pages, unless you want to work on a whole new project, just as big as your current one, to try it out.
Chris Charabaruk
+1  A: 

Never actually been in your situation before, but I've tried ExamDiff Pro to compare PDFs and it worked for me.

cubex
A: 

I think your best approach would be to convert the PDF to images at a decent resolution and than do an image compare.

To generate images from PDF you can use Adobe PDF Library or the solution suggested at http://stackoverflow.com/questions/75500/best-way-to-convert-pdf-files-to-tiff-files.

To compare the generated TIFF files I found GNU tiffcmp (for windows part of GnuWin32 tiff) and tiffinfo did a good job. Use tiffcmp -l and count the number of lines of output to find any differences. If you are happy to have a small amount of content change (e.g. anti-aliasing differences) then use tiffinfo to count the total number of pixels and you can then generate a percentage difference value.

By the way for anyone doing simple PDF comparison where the structure hasn't changed it is possible to use command line diff and ignore certain patterns, e.g. with GNU diff 2.7:

diff --brief -I xap: -I xapMM: -I /CreationDate -I /BaseFont -I /ID --binary --text

This still has the problem that it doesn't always catch changes in generated font names.

danio
I think the comparing of 2 images is more complex then comparing the PDF files self.
Horcrux7
Comparing images can be done with GnuWin32 tiffcmp. I will update my answer to elaborate on this.
danio
+1  A: 

I think Bitmap check should work in your case. I use a automation tool to compare 2 images using bitmap check point

Chanakya
+4  A: 

I've used a home-baked script which

  • converts all pages on two PDFs to bitmaps
  • colors pages of PDF 1 to red-on-white
  • changes white to transparent on pages of PDF 2
  • overlays each page from PDF 2 on top of the corresponding page from PDF 1
  • runs conversion/coloring and overlaying in parallel on multiple cores

Software used:

  • GhostScript for PDF-to-bitmap conversion
  • ImageMagick for coloring, transparency and overlay
  • inotify for synchronizing parallel processes
  • any PNG-capable image viewer for reviewing the result

Pros:

  • simple implementation
  • all tools used are open source
  • great for finding small differences in layout

Cons:

  • the conversion is slow
  • major differences between PDFs (e.g. pagination) result in a mess
  • bitmaps are not zoomable
  • only works well for black-and-white text and diagrams
  • no easy-to-use GUI

I've been looking for a tool which would do the same on PDF/PostScript level.

Here's how our script invokes the utilities (note that ImageMagick uses GhostScript behind the scenes to do the PDF->PNG conversion):

$ convert -density 150x150 -fill red -opaque black +antialias 1.pdf back%02d.png
$ convert -density 150x150 -transparent white +antialias 2.pdf front%02d.png
$ composite front01.png back01.png result01.png # do this for all pairs of images
akaihola
+1  A: 

We've also used pdftotext (see Sklivvz's answer) to generate ASCII versions of PDFs and wdiff to compare them.

Use pdftotext's -layout switch to enhance readability and get some idea of changes in the layout.

To get nice colored output from wdiff, use this wrapper script:

#!/bin/sh
RED=$'\e'"[1;31m"
GREEN=$'\e'"[1;32m"
RESET=$'\e'"[0m"
wdiff -w$RED -x$RESET -y$GREEN -z$RESET -n $1 $2
akaihola
+2  A: 

Because there is no such tool available that we have written one. You can download the i-net PDF content comparer and use it. I hope that help other with the same problem. If you have problems with it or you have feedback for us then you can contact our support.

Horcrux7
The advantage of this tool is, that it's neither a pure text comparer nor an image comparer. It compares by structure, checks if the containing elements are "the same" - so your compared PDFs do not have to match 100% but be within a definable similarity.And it's for free.
gamma
A: 

blubeam pdf software will do this for you

M Jenkins
A: 

Tarkware Pdf Comparer may suite your needs. But it's not free and requires Adobe Acrobat.

erks
A: 

Our product, PDF Comparator - http://www.premediasystems.com/pdfc.html" - will do this quite elegantly and efficiently. It's also not free, and is a Mac OS X only application.

Peter Truskier
This tool compare pixel by pixel. This is very simple. The question was a compare like a human people do it.
Horcrux7