Skip to content
Advertisement

PHP flatten PDF form after filling fields with fpdm WITHOUT PDFtk

I’ve seen a lot of issues where the problem was the same, but most solutions still ended with installing PDFtk or they wouldn’t need the flatten function. Sadly not for me.


The issue

Using fpdm I’ve created an automatically filled PDF-form.

Problem is, that the values in checkboxes do not show up if the PDF is opened and printed in the browsers PDF viewer (on all tested browsers). As it will be users who will download this PDF the values should be visible no matter in what program the PDF is being viewed in.

So my idea was to flatten the PDF but fpdm needs PDFtk to flatten the PDF which cannot be installed on the server.

Also as we’re using TYPO3 CMS I would like to avoid adding a complete framework (such as zend-framework).

I have tried to flatten the PDF using graphicsMagick, but first off the quality was really bad and secondly one of two pages was missing using the following command from PHP:

shell_exec('gm -flatten form.pdf form.pdf')

Do I simply need to change the gm-command? If yes, how so?

And if GM doesn’t work does anyone know an open source/free solution to flatten PDF forms?

Thanks in advance for your help.


Update

I found a command to flatten the PDF with graphicsMagick and it looks a lot better than with my last attempt:

shell_exec('gm convert -density 300 ' . $tmpFile . ' +adjoin -append -flatten ' . $PDFFile);

It’s still not perfect as the fonts change to what was used before flattening, but maybe I’ll find a solution for that too. But contrary to my last attempt no page is missing.

Yet, I’m still wondering if PDFtk is the only free PHP compatible tool to fill AND flatten PDF forms (ignoring all the tools that in the end are dependant from PDFtk)?

Advertisement

Answer

Instead of using graphicsMagick to flatten the pdf we stumbled upon pdftocairo. The following command then flattens the pdf without changing fonts and support for comb-fields (which wasn’t supported neither by graphicsMagick nor GhostScript):

shell_exec("pdftocairo -pdf $tmpFile $PDFFile");

So if anyone needs to flatten PDFs on a Ubuntu 18.04 server pdftocairo contrary to PDFtk can be installed on it without additional packages and works like a charm for what I needed. It also has more PDF-relevant functions which could probably be comparable to PDFtk.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement