Test::PDF - A module for testing and comparing PDF files


  use Test::More plan => 1;
  use Test::PDF;
  cmp_pdf('foo.pdf', 'bar.pdf', '... our PDFs are identical');
  # or
  my $foo = CAM::PDF->new('foo.pdf');
  my $bar = CAM::PDF->new('bar.pdf');  
  cmp_pdf($foo, $bar, '... our PDFs are identical');


This module is meant to be used for testing custom generated PDF files, it provides only one function at the moment, which is cmp_pdf, and can be used to compare two PDF files to see if they are visually similar. Future versions may include other testing functions.

What is "Visually" Similar?

This module uses the CAM::PDF module to parse PDF files, then compares the parsed data structure for differences. We ignore cetain components of the PDF file, such as embedded fonts, images, forms and annotations, and focus entirely on the layout of each PDF page instead. Future versions will likely support font and image comparisons, but not in this initial release.

Important Disclaimer

It should be clearly noted that this module does not claim to provide a fool-proof comparison of generated PDFs. In fact there are still a number of ways in which I want to expand the existing comparison functionality. This module is actively being developed for a number of projects I am currently working on, so expect many changes to happen. If you have any suggestions/comments/questions please feel free to contact me.


cmp_pdf($got, $expected, ?$message)

This function will tell you whether the two PDF files are "visually" different, ignoring differences in embedded fonts/images and metadata.

Both $got and $expected can be either instances of CAM::PDF or a file path (which is in turn passed to the CAM::PDF constructor).


Testing Large PDFs

Testing of large PDFs (30+ pages) can take a long time, this is because, well, we are doing a lot of computation. In fact, this modules developer test suite includes tests against several large PDFs, however I am not including those in this distibution for obvious reasons.


More functions for more testing
Testing of font data
Testing of embedded image data


None that I am aware of. Of course, if you find a bug, let me know, and I will be sure to fix it. This is still a very early version, so it is always possible that I have just "gotten it wrong" in some places.


I use Devel::Cover to test the code coverage of my tests, below is the Devel::Cover report on this module test suite.

 ---------------------------- ------ ------ ------ ------ ------ ------ ------
 File                           stmt   bran   cond    sub    pod   time  total
 ---------------------------- ------ ------ ------ ------ ------ ------ ------
 Test/                    94.3   87.5   60.0  100.0  100.0  100.0   88.5
 ---------------------------- ------ ------ ------ ------ ------ ------ ------ 
 Total                          94.3   87.5   60.0  100.0  100.0  100.0   88.5
 ---------------------------- ------ ------ ------ ------ ------ ------ ------

NOTE: This code coverage report does not include a number of (long running) developer tests.


CAM::PDF - I could not have written this without this module.


Chris Dolan (author of CAM::PDF) for answering my many questions and writing CAM::PDF.


Stevan Little, <>


Copyright 2005 by Infinity Interactive, Inc.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.