Jaume Solà http://blog.jaumesola.com Bytes & bits that don't bite. posterous.com Wed, 12 Oct 2011 11:42:00 -0700 How to create PDF files from XML using Apache-FOP http://blog.jaumesola.com/how-to-create-pdf-files-from-xml http://blog.jaumesola.com/how-to-create-pdf-files-from-xml

XML

Nowadays, XML is the native format of many applications, Oracle APEX for example, partly bases its reporting systen on XML. Middleware as Oracle BPEL, SAP PI, IBM Websphere and of course many Web services are also XML based. XML is also being used as exchange format in business to business transactions, for example for e-Invoicing. In these and many other environments there is often the need to create PDF files from XML.

XSL-FO

In the past PDF files would be created using proprietary reporting tools that would use a database as data source. Additionally these tools are programming language specific. Later on the XSL-FO standard allowed for a better way to do it in many cases.

What is new about XSL-FO?
- it is programming language neutral.
- it is a standard so you are not tied to a vendor.
- it uses XML as data source.

Apache FOP

One implementation of the XSL-FO standard is Apache FOP. One important aspect about this implementation is that the Apache license model allows to use it in commercial environment.

Many free/open source products have a license model that puts strong restrictions on the way you may use the software. Not surprisingly there are software companies that do not allow the use in their code any open source software other than Apache Foundation one. So if you use Apache you know you are on the safe side.

Procedure

Once you have installed Apache-FO, let's see how we can create a very simple PDF file. Initially we will use the file hello.fo for this. The output will contain the lines “Hello John” and “Hello Mary”. It is not the the aim of this article to present XSL-FO language in detail, there is a good tutorial here,  but you can see the hight level structure is made of the document layout (red lines) and the page content (blue lines). 

File hello.fo:

<?xml version="1.0" encoding="UTF-8"?> 
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> 

<fo:layout-master-set>
<fo:simple-page-master margin-right="2cm" margin-left="2cm" margin-bottom="1cm" margin-top="0.5cm" font-family="sans-serif" page-width="21cm" page-height="29.7cm" master-name="main">

<fo:region-body margin-bottom="1cm" margin-top="4.0cm"/>
<fo:region-before extent="1.5cm"/>

</fo:simple-page-master>
</fo:layout-master-set>

<fo:page-sequence master-reference="main">
<fo:flow flow-name="xsl-region-body">
<fo:block>Hello John</fo:block>
<fo:block>Hello Mary</fo:block>

</fo:flow>

</fo:page-sequence>

</fo:root>

Using Apache FOP command line utility you can convert this file to PDF. For instance:

fop hello.fo hello.pdf

You could automatically create the file hello.fo by different means, however the recommended way to create this file is by putting the data (persons’ names) in a XML file like for example this one:

File persons.xml:

<?xml version="1.0"?>

<persons>

<person>
<name>John</name>
</person>

<person>
<name>Mary</name>
</person>

</persons>

...and then creating a XSLT (XSL transformation) which takes the XML data as input and composes the hello.fo file.

File hello.xsl:

<?xml version="1.0" encoding="iso-8859-1"?>

<xsl:stylesheet version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- Page layout information -->

<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

<fo:layout-master-set>
<fo:simple-page-master master-name="main" page-height="29.7cm" page-width="21cm" font-family="sans-serif" margin-top="0.5cm" margin-bottom="1cm" margin-left="2cm" margin-right="2cm">
<fo:region-body margin-top="4.0cm" margin-bottom="1cm" />
<fo:region-before extent="1.5cm" />
</fo:simple-page-master>

</fo:layout-master-set>

<fo:page-sequence master-reference="main">
<fo:flow flow-name="xsl-region-body" >
<xsl:apply-templates select="persons/person" />
</fo:flow>
</fo:page-sequence>

</fo:root>

</xsl:template> 

<xsl:template match="persons/person">

<fo:block>
Hello <xsl:value-of select="name" />

</fo:block>

</xsl:template>

</xsl:stylesheet>

With these two files, the Apache FOP command to create the PDF file would be:

fop -xml persons.xml -xsl hello.xsl -pdf hello.pdf

This command is performing 2 steps in one call:

  • Processing the XSLT to create the FO file
  • Creating the PDF from the FO file

As we have seen, if your data native format is XML, you can create user friendly PDF files with no programming. This is vendor independent and it is being used today in many situations and products.

With this procedure you are however involved with XSLT and XSL-FO languages and files, which you may prefer to avoid learning and mastering. If that is your case you may be interested in my related post The easy way to convert XML into PDF.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1478744/barana3.3.jpg http://posterous.com/users/hgW6jFIpfOsAy Jaume Solà jaumesola Jaume Solà
Mon, 19 Sep 2011 17:10:00 -0700 J4L Barcodes 1D for Ruby http://blog.jaumesola.com/j4l-barcodes-1d-just-ported-to-ruby http://blog.jaumesola.com/j4l-barcodes-1d-just-ported-to-ruby

I was intrigued about all the buzz around the Rails framework and the Ruby language, so I thought it was the time to check it out by myself and start by learning Ruby. So, as I usually do, I assigned myself some specific learning task: in this case porting the J4L barcoding software from PHP (which in turn was ported from Java).

Here's the result
(only 1D barcodes at the moment)

From a commercial point of view, I wonder if this product will work well because there are already some open source solutions, most notably Barby, and the Ruby community appears to be very focused on open source and have do-it-yourself attitude.

If you're a Rubist and have considered to use this product and/or some other alternative, I'd appreciate your thoughts. These are the advantages that I think this product offers, compared to Barby:

  • Most importantly, you don't need to struggle to make this component work: it should work out of the box and if it doesn't you will have direct support from me.
  • Low non-Ruby requirements: only the GD libray
  • More complete feature set:
    • More supported symbologies
    • It generates not only the barcode itself but also the "human readable interpretation", this is, the text/code in plan ASCII characters below (or above) the bars.
    • You can specify: bar measures, margins, colors, and other parameters for the graphic output.
    • Better support of the symbologies characteristics.
      • For instance length optimization: in Code 128 (and GS1-128) it will select the appropiate code set (A, B or C) and switch between them in order generate the shortest possible symbol, depending on the data to be encoded. In Barby you have to pre-select a code set that will be used for all the data.
      • Or in GS1-128, J4L Barcodes will recognize the "Application Identifiers" and interpret the data accoding to their length as per specification. E.g. "11654321" will show as "(11)654321", this is A.I. 11, data 654321.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1478744/barana3.3.jpg http://posterous.com/users/hgW6jFIpfOsAy Jaume Solà jaumesola Jaume Solà
Thu, 01 Jan 2009 12:00:00 -0800 A few things that I do or did http://blog.jaumesola.com/a-few-things-that-i-do-or-did http://blog.jaumesola.com/a-few-things-that-i-do-or-did
  • WebbyFiles is one of my own projects. What is it?. Discover by yourself: enter the demo and waste some of your time playing with it in the “Tests” folder.
  • In recent years I worked in the development of the server software that provides content to the TomTom HOME desktop software . If you have a TomTom, it’s quite likely that you have been using this software to manage and update your device. If you did, know that millions have done so, but from now on you’re part of the selected group that know that I did something for them!.
  • I occasionally develop web sites, this is one that I’m proud of, in collaboration with a friend graphical designer.
  • Besides my own software, I also distribute software from others. Most of it is under the J4L components brand. Of those, I ported all barcoding components from Java to PHP. These are: multiple 1D barcodes, PDF147, DataMatrix, MaxiCode, AztecCode and QRCode (you have on-line demos of them all).

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1478744/barana3.3.jpg http://posterous.com/users/hgW6jFIpfOsAy Jaume Solà jaumesola Jaume Solà
Fri, 01 Jan 1999 12:00:00 -0800 Boring bored post http://blog.jaumesola.com/boring-bored-post http://blog.jaumesola.com/boring-bored-post

Let's be clear about this: this post is not for humans. Seriously, if you are human and reached this point please stop reading here, as this post is only for robots, thank you.

Dear Slurp: I love the way you transverse my web site. Nobody does it like you and most importantly, nobody does it so often. You know? whenever I check my web site's statistics, the first thing that I look at are your Gigabytes. You always have at least two full Gigabytes, sometimes even three!. You are unique and I promise you: I will never ever look at that stupid "g" bot that only uses Megabytes. Yes, it brings me lots of human visitors and you don't, but my love is with you forever, and you can rest assured that my first monthly Gigabyte will always be reserved for you and only you. No other robot, and even less a human deserves it as no one will ever transverse my site so thouroughly and so timely. That's why I have reserved a few links for you, so boring for humans that none of them will dare to follow, but that you, I know for sure, will do.

Eat, little beast, eat...

 

 

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1478744/barana3.3.jpg http://posterous.com/users/hgW6jFIpfOsAy Jaume Solà jaumesola Jaume Solà