Version 5.9.40

Order form from the data

Task

Confixa sends regular customers an order form as a PDF. The customer data is already filled in, every article has a field for the quantity, and there are fields for the shipping method, a remark and the confirmation of the terms and conditions. The customer fills in the PDF in an ordinary PDF viewer, saves it and sends it back. Confixa then reads out the order automatically and passes it on to order processing.

Order form with prefilled customer data, an article table with quantity fields, radio buttons for the shipping method, a remark field and a check box for the terms and conditions

Decision

A fillable form can also be built by hand in Acrobat or InDesign. The publisher pays off as soon as one of the following questions is answered with yes:

  • Does the content come from data? Every customer gets their own prefilled values, the range of products changes, the number of article rows varies.
  • Should the returned forms be evaluated automatically? Then the fields need names that can be assigned to the records again, for example with the article number in the field name.
  • Are many forms created? One form per customer, per branch or per month is created in the same run as any other document.

There are three places for fields, which can be mixed:

PlaceSuitable for
In a paragraph, like a word in the textLabel and field in one line, check boxes in front of a sentence
Directly in a table cellOne row per article; the field fills the cell
With <PlaceObject> anywhere on the pageLarger fields such as a multi line remark field

Solution

The data

The article data is extended by an element customer with the customer data and the list of countries of delivery:

<data company="Confixa">
  <customer number="K-10482" name="Holzmann Joinery" contact="Anna Holzmann"
            country="DE"/>
  <country code="DE">Germany</country>
  <country code="AT">Austria</country>
  <country code="CH">Switzerland</country>
  <group name="Chipboard screws" code="CS">
    <article number="CS-3516" dim="3.5 × 16" pu="1000" price="5.40"/>
    <article number="CS-3520" dim="3.5 × 20" pu="1000" price="5.80"/>
    <article number="CS-4030" dim="4.0 × 30" pu="500" price="4.90"/>
    <article number="CS-4040" dim="4.0 × 40" pu="500" price="5.80"/>
    <article number="CS-5050" dim="5.0 × 50" pu="200" price="4.70"/>
  </group>
  <group name="Nylon plugs" code="ND">
    <article number="ND-0630" dim="6 × 30" pu="100" price="2.90"/>
    <article number="ND-0840" dim="8 × 40" pu="100" price="3.80"/>
    <article number="ND-1050" dim="10 × 50" pu="50" price="3.40"/>
  </group>
</data>

Step 1: Fields in the text, prefilled from the data

A field is created with <Formfield>. Only the name is required; the value is read out under this name later. The initial value comes from value, which, like all attributes, evaluates XPath expressions in curly braces:

<Paragraph textformat="form">
  <Value>Company&#9;</Value>
  <Formfield name="customer.name" value="{@name}" width="8cm" tooltip="Company"/>
</Paragraph>

The field stands in the paragraph like a word, its baseline is the baseline of the text. A tab stop in the text format (tab-stops="4cm") aligns the fields below each other. The customer number gets readonly="yes", so it cannot be changed in the viewer. The tooltip appears when the mouse is over the field and is read out by screen readers.

The country of delivery is a choice field (type="choice"). Its entries come from the data with <Option>; value selects the entry with this value:

<Formfield type="choice" name="customer.country" value="{@country}" width="4cm">
  <ForAll select="../country">
    <Option value="{@code}"><Value select="."/></Option>
  </ForAll>
</Formfield>

The form shows “Germany”, the returned data contain the code DE.

Step 2: A quantity field for each article

In the table, the field stands directly in the cell. Without width and height it fills the cell, so the column definition determines its width. The name contains the article number:

<Td border-bottom="0.25pt">
  <Formfield name="{concat('order.', @number, '.quantity')}"
             maxlength="5" tooltip="{concat('Quantity ', @number)}"/>
</Td>

Dots in the name build a hierarchy: all quantities are below order, with one node per article. The tools for reading out the data return the fully qualified names such as order.CS-3520.quantity, from which the article number can be extracted again. Each name may occur only once; a field that deliberately appears several times (for example in a repeated table head) needs shared="yes".

Step 3: Shipping method, remark and confirmation

Radio buttons with the same name form a group, of which only one can be selected. Each button needs its own value:

<Formfield type="radio" name="shipping" value="standard" checked="yes" tooltip="Standard shipping"/>
<Value> Standard </Value>
<Formfield type="radio" name="shipping" value="express" tooltip="Express shipping"/>
<Value> Express</Value>

The remark field is a multi line text field (multiline="yes") that gets a width and height in grid cells with <PlaceObject>. The check box for the terms and conditions is marked as required with required="yes". Viewers highlight required fields, but not all of them check them when saving.

Step 4: The appearance

Without further settings, fields have neither a border nor a background. The appearance comes from CSS, as with the other commands:

<Stylesheet>
  formfield { background-color: #eef3fb; border-bottom: 0.5pt solid #7a8ca8 }
  formfield.box { border: 0.5pt solid #7a8ca8 }
</Stylesheet>

The remark field, the radio buttons and the check box get the class box. The publisher draws radio buttons round. Border and background of text fields and choice fields appear on the screen only; if the customer prints the filled in form, only the entries are printed, as on a paper form.

Step 5: Evaluating the returned form

pdfcpu and pdftk are suitable for reading out the data. pdfcpu writes all fields as JSON:

pdfcpu form export order.pdf order.json

With jq this becomes the order lines, that is all filled in quantity fields with their article number:

jq -r '.forms[0].textfield[]
       | select(.name | startswith("order.")) | select(.value != "")
       | "\(.name | split(".")[1]) \(.value)"' order.json
CS-3520 4
ND-0840 10

Radio buttons are listed under radiobuttongroup (here shipping with the value express), check boxes under checkbox (terms with true), choice fields under combobox (customer.country with DE). If you prefer to process text, use pdftk order.pdf dump_data_fields_utf8, which prints name and value for each field.

Complete example

The runnable project is also available in the examples repository.

<Layout xmlns="urn:speedata.de:2009/publisher/en"
  xmlns:sd="urn:speedata:2009/publisher/functions/en">

  <Pageformat width="210mm" height="297mm"/>
  <SetGrid width="5mm" height="12pt"/>
  <Pagetype name="page" test="true()">
    <Margin left="20mm" right="20mm" top="20mm" bottom="20mm"/>
  </Pagetype>

  <DefineFontfamily name="title" fontsize="16" leading="20">
    <Regular fontface="TeXGyreHeros-Bold"/>
  </DefineFontfamily>

  <!-- label on the left, field at the tab stop -->
  <DefineTextformat name="form" alignment="leftaligned" tab-stops="4cm"
                    margin-bottom="4pt"/>

  <Stylesheet>
    formfield { background-color: #eef3fb; border-bottom: 0.5pt solid #7a8ca8 }
    formfield.box { border: 0.5pt solid #7a8ca8 }
  </Stylesheet>

  <Record element="data">
    <PlaceObject>
      <Textblock>
        <Paragraph fontfamily="title"><Value>Order from Confixa</Value></Paragraph>
      </Textblock>
    </PlaceObject>

    <!-- customer data, prefilled from the data -->
    <PlaceObject column="1" row="4">
      <Textblock width="34">
        <ForAll select="customer">
          <Paragraph textformat="form">
            <Value>Customer no.&#9;</Value>
            <Formfield name="customer.number" value="{@number}" width="3cm"
                       readonly="yes" tooltip="Customer number"/>
          </Paragraph>
          <Paragraph textformat="form">
            <Value>Company&#9;</Value>
            <Formfield name="customer.name" value="{@name}" width="8cm" tooltip="Company"/>
          </Paragraph>
          <Paragraph textformat="form">
            <Value>Contact&#9;</Value>
            <Formfield name="customer.contact" value="{@contact}" width="8cm"
                       tooltip="Contact"/>
          </Paragraph>
          <Paragraph textformat="form">
            <Value>Country&#9;</Value>
            <Formfield type="choice" name="customer.country" value="{@country}"
                       width="4cm" tooltip="Country of delivery">
              <ForAll select="../country">
                <Option value="{@code}"><Value select="."/></Option>
              </ForAll>
            </Formfield>
          </Paragraph>
        </ForAll>
      </Textblock>
    </PlaceObject>
    <NextRow rows="2"/>

    <!-- articles with a quantity field -->
    <PlaceObject column="1">
      <Table stretch="max" padding="3pt">
        <Columns>
          <Column width="3cm"/>
          <Column width="1*"/>
          <Column width="2cm" align="right"/>
          <Column width="2.5cm" align="right"/>
          <Column width="2.5cm"/>
        </Columns>
        <Tablehead>
          <Tr background-color="lightgray">
            <Td><Paragraph><B><Value>Item no.</Value></B></Paragraph></Td>
            <Td><Paragraph><B><Value>Dimensions</Value></B></Paragraph></Td>
            <Td><Paragraph><B><Value>PU</Value></B></Paragraph></Td>
            <Td><Paragraph><B><Value>Price in €</Value></B></Paragraph></Td>
            <Td><Paragraph><B><Value>Quantity (PU)</Value></B></Paragraph></Td>
          </Tr>
        </Tablehead>
        <ForAll select="group">
          <Tr top-distance="6pt">
            <Td colspan="5" border-bottom="1pt">
              <Paragraph><B><Value select="@name"/></B></Paragraph>
            </Td>
          </Tr>
          <ForAll select="article">
            <Tr valign="middle">
              <Td border-bottom="0.25pt"><Paragraph><Value select="@number"/></Paragraph></Td>
              <Td border-bottom="0.25pt"><Paragraph><Value select="@dim"/></Paragraph></Td>
              <Td border-bottom="0.25pt"><Paragraph><Value select="@pu"/></Paragraph></Td>
              <Td border-bottom="0.25pt"><Paragraph><Value select="@price"/></Paragraph></Td>
              <Td border-bottom="0.25pt">
                <Formfield name="{concat('order.', @number, '.quantity')}"
                           maxlength="5" tooltip="{concat('Quantity ', @number)}"/>
              </Td>
            </Tr>
          </ForAll>
        </ForAll>
      </Table>
    </PlaceObject>

    <!-- shipping, remark, terms -->
    <NextRow rows="1"/>
    <PlaceObject column="1">
      <Textblock width="34">
        <Paragraph>
          <Value>Shipping: </Value>
          <Formfield type="radio" class="box" name="shipping" value="standard"
                     checked="yes" tooltip="Standard shipping"/>
          <Value> Standard   </Value>
          <Formfield type="radio" class="box" name="shipping" value="express"
                     tooltip="Express shipping"/>
          <Value> Express</Value>
        </Paragraph>
        <Paragraph><Value>Remark:</Value></Paragraph>
      </Textblock>
    </PlaceObject>
    <PlaceObject column="1">
      <Formfield name="remark" class="box" multiline="yes" width="34" height="4"
                 tooltip="Remark"/>
    </PlaceObject>
    <PlaceObject column="1">
      <Textblock width="34">
        <Paragraph>
          <Formfield type="checkbox" class="box" name="terms" required="yes"
                     tooltip="Terms read"/>
          <Value> I have read the terms and conditions.</Value>
        </Paragraph>
      </Textblock>
    </PlaceObject>
  </Record>
</Layout>

Limits

  • No calculations in the form: Sums, input validation or number formats need JavaScript in the PDF, which the publisher does not create. The evaluation does the calculations when the form comes back.
  • No submitting from the PDF: The publisher does not support buttons that send the form with a web request; they hardly work in browsers and in macOS Preview anyway. The customer saves the PDF and sends it back.
  • Viewers behave differently: Acrobat, Firefox and Chrome save the values reliably. macOS Preview does not write the value to the field for fields with a dot in their name that occur more than once (radio buttons, shared). Before reading out a file saved by Preview, qpdf file.pdf repaired.pdf helps. Details are in the manual under Forms.
  • Font while editing: The publisher sets the initial appearance with the font of the document. While typing, the viewer redraws the field, with Helvetica unless fontfamily is given. With fontfamily, the font is embedded completely for this purpose, which makes the file larger by the size of the font file.