I have a workflow that combines many PDFs into a single PDF using pdfpages. Recently, one of my users has started submitting PDFs that include fillable forms. We do not need to preserve the form, but we do need to preserve its contents. I'd like to do the following:

  1. Detect if an embedded PDF contains a fillable form that has been filled.
  2. If it has, render the forms into a new PDF that doesn't have fillable forms, so I can include it with pdfpages.
  3. Alternatively, some other way to include the text.

I have reviewed Fillable fields vanish from PDF form when include'd in another PDF and that person's answer could be solved by this, but I think that I'm asking for something more general.

  • Can't you simply print to a pdf? – Ulrike Fischer Jul 30 '18 at 14:13
  • I don't understand why you rejected the suggestion in the now deleted answer. Imho that's the way to go, and I just tested it successfully: I had a pdf with a form that I had to fill out. I printed it (in my case on windows with "microsoft to pdf") to a pdf file and could include it along with the form contents without problems with pdfpages. – Ulrike Fischer Jul 30 '18 at 16:31
  • Agree with @UlrikeFischer. The reason given against seems erroneous. I expected the reason to be the volume of PDFs, but, of course, printing can be scripted, so that wouldn't be a valid objection. But the objection raised seems quite odd. – cfr Jul 31 '18 at 2:35
  • Rather than scripting LaTeX for this, I can imagine that setting up a folder whose file contents are automatically processed by an OS script or appropriate app to flatten will be easier, will have overall greater benefits, and will be more robust in the long term. I know of agencies that take this approach for PDF submissions as a matter of routine. – Jeffrey J Weimer Jul 31 '18 at 12:32

Assuming Linux, there are a few ways to flatten a pdf from the command line while preserving the form contents, for example using cups-pdf, pdf2ps followed by ps2pdf, or pdftocairo. From these, pdftocairo is both easy to use and preserves the form mark-up, so I'll use it in the example below.

In order to automate the conversion, \DeclareGraphicsRule from the graphicx package can be used. The pdfpages package uses \includegraphics internally, so settings and options for \includegraphics also apply to pdfpages. The \DeclareGraphicsRule macro defines an external command to be run for the specified type of file, where the output is expected to be named filename-originalextension-converted-to.newextension. When converting from pdf to pdf this would be, e.g., test-pdf-converted-to.pdf.

Example document with form:

\TextField[width=4cm]{First name:}

\TextField[width=4cm]{Last name:}

This document can be opened with Evince for example, filled in, and saved using Save as... in the Evince menu.

Assuming the file is saved as filledform.pdf the following code can be used to include this pdf in a new document. Note that the external command requires --shell-escape as a compiler flag (e.g., pdflatex --shell-escape myfile.tex).

\DeclareGraphicsRule{.pdf}{pdf}{.pdf}{`pdftocairo -pdf #1 `basename #1 .pdf`-pdf-converted-to.pdf}
\includepdf[frame,scale=0.65,pages=1,pagecommand={PDF form converted with \texttt{pdftocairo}:}]{filledform.pdf}

Result: enter image description here

Note that this approach will convert every included pdf file with pdftocairo. To perform the actual check mentioned in your question (i.e., flatten a file only if there is a pdf form), then you can use a test in a small shell script and call this script as external command in the graphics rule. The test itself can be performed, e.g., by pdfinfo (from Poppler), which outputs a number of properties of the file. If there is a form, pdfinfo will output Form: AcroForm or similar, otherwise the output will be Form: none. You can grep for this line and call pdftocairo for files with a form and cp otherwise.

Code (checkform.sh):

#!/usr/bin/env bash
if pdfinfo $1|grep -qE "Form: +none"; then
    cp $1 `basename $1 .pdf`-pdf-converted-to.pdf
    pdftocairo -pdf $1 `basename $1 .pdf`-pdf-converted-to.pdf

combined with

\DeclareGraphicsRule{.pdf}{pdf}{.pdf}{`./checkform.sh #1}

in your document.

  • Unfortunately, it's Windows, and I'm operating in an environment where I cannot use those fancy PDF tools. But those are great ideas. I just can't obviously use them... – vy32 Jul 31 '18 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.