Search code examples
pdfcomparisoncompare

Visual diff of PDF files in order to determine pixel perfectness


I need to refactor some reports (generated with Jasper) using MS Reporting Services. Copies of original reports are available in PDF. The requirement is to make the new reports "pixel perfect" which is very cumbersome...

For making life easier I would like to have a tool which overlays the original and generated report PDFs in order to measure if they are pixel perfect or not.

Is such a tool out there?


Solution

  • The most simple, immediately available method to do this: use ImageMagick's compare (which is also available on Windows/Linux/Mac and other).

    It can even compare PDF pages (though it uses Ghostscript as its delegate to render the PDF pages to pixel images first):

     compare.exe         ^
        tested.pdf[0]    ^
        reference.pdf[0] ^
       -compose src      ^
        delta.pdf
    

    The resulting delta.pdf will depict each pixel as red which has a different color between the two compared PDF pages. All identical pixels will be purely white. The [0] tell compare to use the first pages of each file for comparison (page count is zero-based).

    You can see how this works out with the following example:

     compare.exe                      ^
        http://qtrac.eu/boson1.pdf[1] ^
        http://qtrac.eu/boson2.pdf[1] ^
       -compose src                   ^
        delta.pdf
    

    Here are the respective pages (converted to scaled-down PNGs for web display). The reference page is on the left, the modified page is the middle one, the 'delta-pixel-are-red' image is on the right:

    first page second page delta image

    A slightly different visual result you can get by skipping the -compose src parameter. Then you'll get the original file's pixels as a gray-shaded background (for context) with the delta pixels in red:

     compare.exe                      ^
        http://qtrac.eu/boson1.pdf[1] ^
        http://qtrac.eu/boson2.pdf[1] ^
        delta.pdf
    

    first page second page delta.pdf

    If you don't like the red color for pixel differences, use -highlight-color:

     compare.exe                      ^
        http://qtrac.eu/boson1.pdf[1] ^
        http://qtrac.eu/boson2.pdf[1] ^
       -highlight-color green         ^
        delta.pdf
    

    The default resolution used to render the PDF pages is 72 dpi. Should you need a higher precision, you can switch to 300 dpi using the -density parameter like this:

     compare.exe                      ^
       -density 300                   ^
        http://qtrac.eu/boson1.pdf[1] ^
        http://qtrac.eu/boson2.pdf[1] ^
        delta.pdf
    

    Note, switching to higher densities will slow down the process and create bigger files.

    You can even create a *.txt file for the delta image which describes each pixel's coordinates and the respective color values:

     compare                          ^
        http://qtrac.eu/boson1.pdf[1] ^
        http://qtrac.eu/boson2.pdf[1] ^
       -compose src                   ^
       -highlight-color black         ^
        delta.txt
    

    Then simply count the number of total vs. black pixels (sorry, this is Unix/Linux/MacOSX syntax):

     total_pixels=$(( $(cat delta.txt | wc -l) - 1))
     black_pixels=$(( $(grep black delta.txt | wc -l) -1 ))
    

    In the example used for the illustrations above, I get

     total_pixels=500990
     black_pixels=8727
    

    Of course the 'ideal' result would be

     black_pixels=0