Test Driven Development of PDFs With PHPUnit and PDFBox
On several of the sites I manage we use wkhtmltopdf to generate PDF files. One of the downsides to this process is that it’s hard to test the generation of the PDF. To overcome this we use PDFBox so we can inspect what’s inside the PDF so we can use TDD.
Install the Necessary Software
If you don’t already have it installed you’ll need to install Java.
Next you’ll need to download PDFBox and place it somewhere on your system. The file is “only” 14 MB so I actually include it in the repo for my project so everyone who is working on the projects is using the same version.
Helper Function
The PHPUnit documentation has you extend the TestCase
class. I always create a TestCase
class specific to my project that extends TestCase
so I add functions I use over and over again (for example, you might need to initialize the same type of class without dependency injection over and over). To this class we’re going to add a helper function so we can use PDFBox to extract the data .
Example
You’ll still need to do a visual inspection of a generated PDF but this will make sure the important parts are included.
Scott Keck-Warren
Scott is the Director of Technology at WeCare Connect where he strives to provide solutions for his customers needs. He's the father of two and can be found most weekends working on projects around the house with his loving partner.
Top Posts
- Working With Soft Deletes in Laravel (By Example)
- Fixing CMake was unable to find a build program corresponding to "Unix Makefiles"
- Upgrading to Laravel 8.x
- Get The Count of the Number of Users in an AD Group
- Multiple Vagrant VMs in One Vagrantfile
- Fixing the "this is larger than GitHub's recommended maximum file size of 50.00 MB" error
- Changing the Directory Vagrant Stores the VMs In
- Accepting Android SDK Licenses From The OSX Command Line
- Fixing the 'Target class [config] does not exist' Error
- Using Rectangle to Manage MacOS Windows