Today I faced a problem with FPDF and FPDI when trying to concatenate some PDF files. The only output I got was ‘FPDF error: Unable to find xref table.’ When I did a search on this error message, I wasn’t able to extract the answer from it. After some searching, the problem was located.
One of the PDFs which should be merged was originally created from Word by a PDF creator which placed its signature in the properties of the PDF document. After removing this signature (in this case opening the PDF with Adobe Illustrator and saving it again) the problem was solved.
Compression and stream
The FPDF / FPDI classes use the input stream of the PDF file to create the concatenated PDF. Setasign remarks that the stream is only used (and altered) in the free version. The paid version should not give such errors. As I’ve experimented with the free version, I’ll treat that one here.
Because of compression, some applications might leave out or mess up the xref property. Although this results in a PDF file which can be opened in a PDF reader, the stream is not according to the ‘standards’. In such a case the document markup is ‘lost’ and applications / scripts using this property cannot handle these files any more.
PDF merging code example
To show how PDF files can be merged by using FPDF and FPDI I’ve compiled a little example which can be downloaded here. The archive consists of FPDF version 1.6, FPDI version 1.3.1, FPDI_TPL and two sample PDF files which were created with Microsoft Word 2007. The executing script is concat.php. Run this script to concatenate the two sample PDF files together in a new PDF file.
A paid version of FPDI might be an option, but let’s have a look at other possible solutions.
Opening the file and saving it again with a better pdf creator might fix the problem as it reformats the stream which is saved.
When you’re creating the source PDF files yourself, watch out which pdf creator you use. Although the visual result might be the same, the stream might not be.