Using PHP to wrangle PDFs and apply for Flex Spending Account reimbursement

It’s the end of the year—a time for employees everywhere to say, “Whoa, I’d better apply for reimbursement from my flexible spending account. Ooh, look, someone from HR just brought some more cookies to the holiday party.”

We have the bad habit of waiting until the end of the year to apply for our FSA reimbursement, which means lots of paperwork to be reimbursed for pretty much any medical expense we’ve incurred this year.

The easiest way to document our expenses, we have found, is to turn in papers from the explanation of benefits (EOB) forms that the insurance company sends us. Conveniently, Anthem allows us to download all of our EOBs online, making them easy to access.

Which leads me to this problem: Each EOB form is several pages long. The only interesting page is the third page, which shows the service provider, the patient name, the amount the insurance paid, and the amount left for us to pay, etc.—everything we need to document for the reimbursement request.

This useful page is proceeded by two useless pages, one of which does nothing but inform you that “THIS IS NOT A BILL.”

Not wanting to waste paper by printing the EOB pages I don’t need, and not wanting to waste time by opening each PDF file, clicking print, changing the print settings to print only the third page, and then clicking “Print” again, I thought I would much prefer to spend that time programming.

I thought, “Couldn’t I write a program that would open all the files in this directory, pull page 3 from each file, and append that page to a new file that will contain all of the page 3s?”

The answer, of course, is yes. (Why else would I be writing this post?)

My online search for methods of wrangling PDFs with PHP led me to this post about using PHP to modify PDFs. This introduced me to FPDF and FPDFI, which turn PHP into a PDF-making machine. If you want to have PHP do some work on PDFs, then go ahead and download those.

<?php
//import some awesome new libraries to give yourself PDF powers
require_once('fpdf.php');
require_once('fpdi.php');

//create a new PDF object. This object will collect all the pages we want to keep and eventually dump them into a file
$pdf =& new FPDI();

//glob creates an array of files. In this case, my original PDFs were in a subdirectory called "originals"
$pdfs = glob('originals/*');

//loop through the original files...
foreach ( $pdfs as $file ) {
     //add a page to a our destination PDF
     $pdf->AddPage();

     //Import the current original file.
     //The variable is called $pagecount because this FPDFI function
     //returns the pagecount while loading the source file.
     $pagecount = $pdf->setSourceFile($file);

     //Import the third page of the file. This will add the content of the original file's third page on the recently added page of the destination PDF
     $tpl = $pdf->importPage(3);
     $pdf->useTemplate($tpl);
}

//save the destination PDF
$newfilename = "onebigfile.pdf";
$pdf->Output("newfiles/".$newfilename, "F");
echo "$newfilename created<br/>";
?>

This script accomplished its goal of stripping the 3rd page out of each original PDF and creating one destination PDF. I only had to click “Print” one time, woo hoo!

Fortunately, programming this did NOT take more time than it would have taken to open each PDF and print only the third page. Imagine how much time I will save next year when I have this already programmed!

There was one problem: The right hand side of the destination file seemed a bit crowded. I assume it has something to do with the original files having a different page size than the default destination file size. They were still entirely legible, so I wasn’t going to worry about it. If you are combining PDF pages and are having trouble, check the page sizes.