Machine generation of PDF files the easy way – part III

This third example will extend upon my XSL-FO script to convert the list into more of a table. The result will generate a form and will look like this.

data3 What I find to be really well done in XSL-FO and pretty useful is that it is possible to actually create tables that are formed from cells. What happens is that you create a table out of cells and decide which of these cells should have borders.

The change for this example is to turn the list output into a small table with borders. The additional xslt “code” for a table is pretty low. The following example is all you need to create a tiny table with one row of two cells.

<fo:table table-layout="fixed" width="60%">
  <fo:table-column column-width="proportional-column-width(80)" />
  <fo:table-column column-width="proportional-column-width(20)" />
    <fo:table-body>

    <fo:table-row >
      <fo:table-cell >
        <fo:block> value of cell 1, row 1 </fo:block>
      </fo:table-cell >
      <fo:table-cell  >
        <fo:block> value of cell 2, row 1 </fo:block>
      </fo:table-cell >
    </fo:table-row >
    </fo:table-body>
</fo:table>

When taken on its own this is really easy to take in. Simply define a table and the number of columns are necessary. Once that is done then it is just a matter of adding the actual rows full of data.

The table tag describes just how big (wide) our table will be . The attribute width on the table tag is using a relative width. This is defining the width to be equal to 60% if the width of our region body. This is a really flexible method if it is possible that the layout is likely to change during development.

The table-column tags essentially describes how wide each cell will be. The proportional-column-width attribute is also relative. This is proportioning our table with simple ratios. This may not be good enough when absolute dimensions are mandatory but for most forms this is a convenient trade off.

This example, although simple, would become quite tedious if you had to write the code for each row. The trick is to split up this table and row code into each of our templates so when the form is processed this is done for us automatically.

This is especially easy to do for the data file that we are using. Simply define the table layout somewhere before the person template is called and finish the table layout after all of the “people” are processed. In my case, the table definition is in the region body and the rows is for each person.

Region body

        <fo:flow flow-name="xsl-region-body">

          <fo:block font-size="16pt" font-weight="bold" space-after="5mm">List of <xsl:value-of select="/xmlroottag/listtype"/>
          </fo:block>

          <fo:block font-size="10pt">

              <fo:table table-layout="fixed" width="60%">
                  <fo:table-column column-width="proportional-column-width(80)" />
                  <fo:table-column column-width="proportional-column-width(20)" />
                  <fo:table-body>

                     <xsl:apply-templates />

                  </fo:table-body>
              </fo:table>


          <fo:block space-before="5mm">
                thats all folks!
          </fo:block>

          </fo:block>
        </fo:flow>

Person template

<xsl:template match="person">
    <fo:table-row >
       <fo:table-cell font-weight="bold" border-collapse="collapse" border-style="solid" border-width=".1mm" text-align="left" padding="1mm" >
           <fo:block>
           <xsl:value-of select="first"/>
           <xsl:value-of select="last"/>
           </fo:block>
       </fo:table-cell >
       <fo:table-cell font-weight="bold" border-collapse="collapse" border-style="solid" border-width=".1mm" text-align="left" padding="1mm" >
           <fo:block>
           <xsl:value-of select="party"/>
           </fo:block>
       </fo:table-cell >
    </fo:table-row >
</xsl:template>

This example works well because the of how homogeneous our data is. The data and xslt document was created with this structure in mind. It will perfectly process a list of data and prior to the list it will display a description about the list.

This is basically all the information that is necessary in order to create a simple form. The form itself would not be all that sophisticated as this is really only adding a few lines. The next set of changes provides the changes to create labels with small captions over them.

 

Download source and pdf for this example

Download files

The next set of changes for this can be found in part IV.

This entry was posted in programming and tagged , , , . Bookmark the permalink.