# PDF form as background image - filling the form fields

I'd like to elaborate my proof of concept so that I can be confident with selected tools/packages for this task before I start implementing it.

Scenario. I have several documents (each document has its own template) and in order to provide a user friendly document creation process, I needed to include some pdf forms in certain documents so that end user gets just one pdf, not bunch of pdfs. Number of included pdfs is most of the time just one, but there are some occasions where more than one included pdf are necessary.

Top-level workflow. I have a web application that generates html code based on user input. I use Pandoc with Latex template to which I pass required variables. Pandoc generates pdf output for end users (pdflatex). This html -> Pandoc/Latex -> pdf workflow works well and it is out of the scope regarding those questions below, just for your information.

Included pdfs. These pdfs may or may not be filled as forms. Each pdf equals one page.

My initial concept.

Include pdf as background image using package pdfpages

\documentclass[finnish,a4paper,11pt]{article}
\usepackage[a4paper]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[final]{pdfpages}
\usepackage{graphicx}

\begin{document}
\setboolean{@twoside}{false}
\includepdf[pages=-]{file.pdf}
\end{document}


Since I do all the form filling in automated manner (form data as pandoc template variables), I ignore form filling features and just write text to absolute coordinates.

Quickly add text to existing pdf files / fill forms

Questions.

Q1. Any other tools that you might consider for this task?

Q2. Some examples that you'd recommend?

Q3. Better way to pass variables to forms than Pandoc command line variables?

Q4. General comments and suggestions for this concept are welcome.

UPDATE.

I made a test document that includes a pdf form, based on the method presented below. I passed the form data via Pandoc variables to Latex template and I can live with this method since everything happens automatically, once programmed. However, this leads to another question that follows

Q5. Since there are number of possible forms to be included based on the end user's selections in web application, I'm forced to make this kind of "if" structure into my template header

\ifthenelse{\equal{\detokenize{$headertitle$}}{\detokenize{myformA}}}
{ % load packages, make new commands to set coordinates for form fields, ... }


and similar structure in templates document part

\ifthenelse{\equal{\detokenize{$headertitle$}}{\detokenize{myformA}}}
{ % run those new commands based on Pandoc variables }


When the number of the possible forms grows, my template size grows accordingly and as a programmer (but as a Latex newbie) I'm a bit concerned if this is viable solution to add everything in one template file. So, I'm asking if there is a better way organizing this kind of code like in sub-templates or alike?

I updated my own answer below with new example code.

• Related?? tex.stackexchange.com/questions/108374/…. This allows one to create a .tex document that results in a filled out form. – Steven B. Segletes Jan 4 '15 at 17:27
• Thanks. The principle in your suggested link is at the same ball park. I may consider this approach. I understand that there's no shortcut here and I need to find coordinates for each text I'm about to write on top of the form – Pasi Jan 4 '15 at 18:01
• That is correct. You need to determine the coordinates, though it works quite fast to do that by trial and error. The potentially more difficult thing is to determine whether the input is restricted to one line or a \parbox, etc. – Steven B. Segletes Jan 4 '15 at 20:51

I verified the first part of my concept based on the link Steven (@steven-b-segletes) provided in his comment. Here is a short, working example for A4 sized pdf as a background (don't mind the coordinates...):

\documentclass[finnish,a4paper,11pt]{article}
\usepackage[a4paper]{geometry}
\usepackage{setspace}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{ifthen}

\ifthenelse{\equal{\detokenize{$headertitle$}}{\detokenize{MyTitle}}}
{
\newenvironment{myForm}{
\clearpage % new page for pdf form
\begin{singlespace}
\begin{picture}(210,297)(113,460)
\thispagestyle{empty} % no page numbers here
\includegraphics[width=21cm, height=29.7cm]{file.pdf} % include pdf form
}{
\end{picture}
\end{singlespace}
}
\newcommand\MyFirstField[1]{\put(-481,653){#1}}
}

\begin{document}

% load document body from Pandoc
$body$

% if name of current document equals MyTitle then add and fill form
\ifthenelse{\equal{\detokenize{$headertitle$}}{\detokenize{MyTitle}}}
{
\begin{myForm}
\MyFirstField{$MyFirstVariable$}
\end{myForm}
}

\end{document}


I guess this could be done using pdfpages as well, but I am not able to tell pros/cons using pdfpages. I already made one template for a form that consisted of 25 fields and it took roughly 90 minutes for finding the right coordinates. Since I have dozens of forms (and counting) that I should implement this way, I need to either find or make a tool for accelerating this process. Some sort of point-n-click with mouse.

The 3rd part of my question, about Pandoc and command line variables is still open. What is the best practise when passing multiple variables to template that is used by Pandoc when generating a pdf. In my actual template I will provide all the form inputs as parameters/variables. I know how to do it with Pandoc's --variable, but if there is more robust way, I would be glad to explore that.

• One thing that expedites form creation in many cases is that multiple entries on a form share the same x coordinate (same column) or y coordinate (same row). – Steven B. Segletes Jan 4 '15 at 21:35
• Exactly, that is the case with majority of forms. I just can't help myself being such a perfectionist when seeking for exact coordinates. Even though I'm sure that end users wouldn't care less if something were off by one pt :) – Pasi Jan 4 '15 at 21:41
• Obviously easiest would be to integrate the answers into the source of the form and compile it. But if you are stuck with the form-as-background-image paradigm, perfectionism is misplaced, I would think. – cfr Apr 30 '16 at 2:43
• Put a grid over the form to find the coordinates, if you aren't already doing so. – cfr Apr 30 '16 at 2:44