[Tex/LaTex] How to design LaTeX form with user input for data extraction

debianextractforms

Assume you have the following form in PDF ouput/equivalent in Fig. 1.
I am thinking how to design the LaTeX form well enough for good data extraction (and eventually seeding into PostgreSQL database).
You want to extract the following pieces of data in the form:

  1. Question 1 answer
  2. Question 2 answer
  3. Summary result

Code to generate the PDF file

% https://tex.stackexchange.com/a/384801/13173
\documentclass{article}
\usepackage{hyperref}
\begin{document}

\begin{Form}
\begin{enumerate}
\item \ChoiceMenu[name=football,radio,default=0]{Do you play football?}{Much (2)=2,Little (1)=1,Not at all (0)=0}
\item \ChoiceMenu[name=ice-hockey,radio,default=0]{Do you play ice-hockey?}{Much (2)=2,Little (1)=1,Not at all (0)=0}
\end{enumerate}

\TextField[readonly=true,value=0,calculate={event.value=this.getField("football").value+this.getField("ice-hockey").value;}]{Summary score:}
\end{Form}

\end{document}

Fig. 1 Output

enter image description here

Testing accsupp [rejected because cannot take user input] (Steven)

Code which is not good example because it has integrated values and is not taking values from the user;

\documentclass{beamer}    
\usepackage[english]{babel}    
\usetheme{Berkeley} 
\usepackage{accsupp} % https://ctan.org/pkg/accsupp

\begin{document}

\begin{frame}
\frametitle{Field}
\section{Field 2}

\begin{equation}
    \BeginAccSupp{
        method=pdfstringdef,
        unicode,
        ActualText={%
            a\texttwosuperior +b\texttwosuperior
            =c\texttwosuperior
            }
        }
    a^2 + b^2 = c^2
    \EndAccSupp{}
\end{equation}

\end{frame}

\end{document}

Output in Fig. 2 where I do not really see the point of this package with user inputs, since it is not asking them in the form.

Fig. 2 Output of too simple basic example of accsupp

enter image description here

OS: Debian 9
TeXLive: 2017

Best Answer

As described in Save fillable forms, you can create forms that send their values back to you by email if you click the submit button:

\documentclass{article}
\usepackage{hyperref}
\begin{document}

\begin{Form}[action=mailto:forms <forms@stackexchange.invalid>?subject=The submitted form]
\begin{enumerate}
\item \ChoiceMenu[name=football,radio,default=0]{Do you play football?}{Much (2)=2,Little (1)=1,Not at all (0)=0}
\item \ChoiceMenu[name=ice-hockey,radio,default=0]{Do you play ice-hockey?}{Much (2)=2,Little (1)=1,Not at all (0)=0}
\end{enumerate}

\TextField[name=summary,readonly=true,value=0,calculate={event.value=this.getField("football").value+this.getField("ice-hockey").value;}]{Summary score:}

\Submit[export=xfdf]{Submit}
\end{Form}

\end{document}

If you click the Submit button, an email to the provided address (here forms@stackexchange.invalid) will be composed in your default email program with an attached .fdf file. This attachment contains the submitted data as XML:

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"
><fields
><field name="Submit"
/><field name="football"
><value
>1</value
></field
><field name="ice-hockey"
><value
>2</value
></field
><field name="summary"
><value
>3</value
></field
></fields
><ids original="4C4F1F968A20B15FEDBFD76188D43221" modified="4C4F1F968A20B15FEDBFD76188D43221"
/></xfdf
>

The XML file generated by Adobe Acrobat (Reader) looks a bit peculiar, but can be further processed by any XML parser. Other possible output formats for the .fdf file, specified by the export option of the submit button, are:

  • export=html: exports the data in query string syntax.
  • export=fdf: uses Adobes own Forms Data Format, basically a simplified version of the PDF file format, which might be useful if you want to process the data further using Adobe software.
  • export=pdf: attaches the whole, filled-out PDF file to the email, which you can then process as described in the second part of this answer.

As an alternative, you could consider using the open source PDFtk to extract data from a saved filled-out PDF document: running

pdftk document.pdf dump_data_fields

on a filled-out document document.pdf will report something like

---
FieldType: Button
FieldName: football
FieldFlags: 49152
FieldValue: 0
FieldValue: 1
FieldJustification: Left
FieldStateOption: 0
FieldStateOption: 1
FieldStateOption: 2
FieldStateOption: Off
---
FieldType: Button
FieldName: ice-hockey
FieldFlags: 49152
FieldValue: 0
FieldValue: 2
FieldJustification: Left
FieldStateOption: 0
FieldStateOption: 1
FieldStateOption: 2
FieldStateOption: Off
---
FieldType: Text
FieldName: summary
FieldFlags: 1
FieldValue: 3
FieldJustification: Left