[Tex/LaTex] PDF form as background image – filling the form fields

formspandocpdfpdftextemplates

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.

Best Answer

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.

Related Question