Skip to Content

In one of my last blogs I showed how to create Word documents using XSLT. But what about PDF? PDF generation without Adobe Forms or other commercial tools is much more difficult. This holds even for XSL-FO formatters because the standard is so complicated that only commercial tools support every feature. Nevertheless I suggest to use the Open Source Apache FOP framework written in Java. You can run under the Java stack or use the virtual machine of the application or even the presentation server.

Form a conceptual point of view the Extensible Stylesheet Language consists of two parts:

  • SLT for transforming XML documents to other XML documents (indeed XSLT can produce any computable Unicode stream) and
  • XSL-FO (FO stands for “formatting objects”) which is a kind of XML based language comparable to LaTeX.

There are rendering engines (think of above mentioned Apache FOP) that can generate LaTeX or even PDF from an XSL-FO document. Of course it is a matter of taste but I think LaTeX is much more fun compared to XSL-FO.  But XSL-FO has a big advantage: We can use the “XML infrastructure” to generate XSL-FO from XML documents using transformation languages like XSLT or Simple Transformations.

A Simple but Instructive Example

ABAP programming is easy – but getting access to the whole ABAP development infrastructure is hard. ABAP developers spend most of their time to get access to transactions like SM58, SM59 or SCIF on their development systems. One might think the “Basis” knows that we need SICF for BSP and Web Dynpro programming and that every 1/2K SDN member knows how to get SAP_ALL within seconds but they defend access to certain profiles and roles like their own life. Usually developers make mistakes when talking to Basis people by saying that we “know what we’re doing” and that “there are more subtle ways to ruin the development system without access to ‘critical’ transactions” and so on. Even sending SU53 spools to the admin won’t help you. I learned the only chance is to print out a paper like the following and put it on the admin’s desk:

/wp-content/uploads/2007/05/su53_126627.jpg

Let’s consider creating this document as PDF. \ In the following I’ll present a report and a Simple Transformation that generates an XSL-FO stylesheet that will generate the PDF document shown above. At first we need the information from the last authority check in a structure of type usr07. Then we pass those structure to the Simple Transformation Z_SU53. This can be done with the following hack:

The Simple Transformation Z_SU53 consist of XSL-FO statements and adds values of the usr07 structure to the output:

Let’s try to start transaction SICF but instead of looking at SU53 we start the ABAP report above and get the following result:

Now we have to process this XSL-FO document with Apache FOP and we are ready.

Let’s look at the example in detail. At first we define a layout master using the element fo:layout-master-set and then a sequence of pages using fo:page-sequence. The content elements (text, graphic and so on) are children of the fo:flow element. This element contains a definition for the standard font defined by the attribute font-family.

Within the element fo:flow  we define several fo:block elements containing paragraphs of text. The attribute space-after.optimum=”6pt” defines space after each block.  Within each block we change the standard font (see fo:inline element).

Summary

In this blog entry I introduced XSL-FO and Apache’s FOP framework and showed how to use Simple Transformations to generate XSL-FO documents from ABAP data. And last but not least I gave a hint for a better communication with Basis people.

To report this post you need to login first.

5 Comments

You must be Logged on to comment or reply to a post.

    1. Tobias Trapp Post author
      Hi Peter,

      in fact I use this technique for something more serious.

      In output generation processes you have several systems for publishing data (i.e. generation of emails and PDF from XSF streams), batch archivers, a document management system and so on. If you don’t have access to those external systems in your development system you have to simulate them – and therefore we can use XSL-FO.

      Cheers
      Tobias

      (0) 
  1. Durairaj Athavan Raja
    Hi Tobias,

    Thanks for the excellent blog. waiting for the next one this using Apache for processing the XSL-FO.

    I was looking for a possibility run the conversion (XML / XSL-FO => PDF) from within the ABAP environment without having a apache installation but to just use those FOP classes. Do you have an idea?

    Raja

    (0) 
    1. Tobias Trapp Post author
      Hi Raja,

      besides Adobe Forms (see the link in my blog) which has an ABAP binding you could use a PDF library because generation of PDF manually is no fun. Here are my suggestions:

      1.) If you are a scripting guy then you could use Perl. There is a great PDF library PDFlib (www.pdflib.com) which is free fpr non commercial applications. There are bindings for C++, Tcl, Python, Perl, Visual Basic und Java. You can use the perl binding the following way:
      $p = new PDF;
      $p->open_file(“output.pdf”);
      $p->begin_page(“A4”);

      I think it would be a great Open Source project to create an ABAP binding perhaps as a wrapper of an existing Perl binding. Perhaps would be a nice challenge for the scripting guys cimmunity here on SDN.

      2.) We could try to create DocBook – this is a much more simpler format (DocBook for instance – see http://www.docbook.org/) that can be converted to XSL-FO and then to PDF.

      3.) You could try to use xmlroff: http://xmlroff.org/

      4.) If you are interested in a subset of PDF functionalities you could create PS and use ps2pdf. Bur I recommend it only if you are an PostScript guru who loves it to write snail mail letters that way.

      By the way, I head to learn that XSL-FO has some  drawbacks: running text along both sides of a floating object is not supported and you have to use tricks but there are many others. But for “business communication” XSL-FO seems to be OK.

      Cheers
      Tobias

      (0) 
      1. Durairaj Athavan Raja
        Hi,

        Thanks for the detailed reply.
        I had seen a example where all required jars (FOP) are placed in a folder and thru a .bat file (commandline) i can call this with inputfile location (may be a html or any type) and the location of XSL-FO and target output folder , then it would create the PDF and store it in the specified location.

        what i was thinking that i could simulate this from a BSP (as it runs in browser environment and jre will be available to interpret it)

        currently for pdf rendering we use itext (http://www.lowagie.com/iText/). we have this java (jsp) application deployed in java instance of webas and from BSP or ABAP all we need to pass is the definition and data of the pdf page as a string in our own custom format. (for example we split cells with ~ and rows with |, etc) which would then return the pdf in binary format. I was looking for a way to eliminate the need for j2ee engine.

        Regards
        Raja

        (0) 

Leave a Reply