Saturday, January 12, 2008

My eclipse XSL-FO workflow

I hope that this helps someone. Keep in mind I do not give any warranty to any recommendations I make on this blog. If you are able to blow up your hard drive with it (or your Mom's hard drive), I assume no liability for damages.

Problem: I needed to quickly see the output of changes made to some XSL-FO XSLT stylesheets.

Resolution: Eclipse, Apache Ant, Adobe Acrobat

  1. I created an eclipse project and added the following to it:

    • Sample source XML:

      <?xml version="1.0" encoding="UTF-8"?>
      <!--
      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements. See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License. You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
      -->
      <!-- $Id: xslfoRef.xml 426576 2006-07-28 15:44:37Z jeremias $ -->
      <!--
      tbd: - internal linking
      - one line explanation
      - example
      please note: the short explanation of each fo is based (mostly verbatim) on the section 6.3 of
      the Extensible Stylesheet Language (XSL) Version 1.0 W3C Candidate Recommendation 21 November 2000.
      authors: Sharon Adler, Anders Berglund, Jeff Caruso, Stephen Deach
      Paul Grosso, Eduardo Gutentag, Alex Milowski, Scott Parnell,
      Jeremy Richman, Steve Zilles
      url: http://www.w3.org/TR/2000/CR-xsl-20001121/

      -->
      <root>
      <div0><head>formatting objects</head>
      <div id="fo:basic-link">
      <fo>fo:basic-link</fo>
      <explanation>represents the start resource of a simple link.</explanation>
      <content>(#PCDATA|%inline;|%block;)*</content>
      <properties>
      <property>common-accessibility-properties</property>
      <property>common-aural-properties</property>
      <property>common-border-padding-and-background-properties</property>
      <property>common-margin-properties-inline</property>
      <property>common-relative-position-properties</property>
      <property>alignment-adjust</property>
      <property>alignment-baseline</property>
      <property>baseline-shift</property>
      <property>destination-placement-offset</property>
      <property>dominant-baseline</property>
      <property>external-destination</property>
      <property>id</property>
      <property>indicate-destination</property>
      <property>internal-destination</property>
      <property>keep-together</property>
      <property>keep-with-next</property>
      <property>keep-with-previous</property>
      <property>line-height</property>
      <property>line-height-shift-adjustment</property>
      <property>show-destination</property>
      <property>target-processing-context</property>
      <property>target-presentation-context</property>
      <property>target-stylesheet</property>
      </properties>
      </div><div id="fo:bidi-override">
      <fo>fo:bidi-override</fo>
      <!-- snip -->


    • Sample XSL-FO for the source:

      <!--
      Licensed to the Apache Software Foundation (ASF) under one or more
      contributor license agreements. See the NOTICE file distributed with
      this work for additional information regarding copyright ownership.
      The ASF licenses this file to You under the Apache License, Version 2.0
      (the "License"); you may not use this file except in compliance with
      the License. You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      See the License for the specific language governing permissions and
      limitations under the License.
      -->
      <!-- $Id: xml2pdf.xsl 426576 2006-07-28 15:44:37Z jeremias $ -->
      <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
      xmlns:fo="http://www.w3.org/1999/XSL/Format">

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

      <!-- defines page layout -->
      <fo:layout-master-set>

      <fo:simple-page-master master-name="simple"
      page-height="29.7cm"
      page-width="21cm"
      margin-top="1.5cm"
      margin-bottom="1.5cm"
      margin-left="2.5cm"
      margin-right="2.5cm">
      <fo:region-body margin-top="1.5cm"/>
      <fo:region-before extent="1.5cm"/>
      <fo:region-after extent="1.5cm"/>
      </fo:simple-page-master>
      </fo:layout-master-set>

    • Sample Apache FOP config XML:

      <?xml version="1.0"?>
      <fop version="1.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation=
      "http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/src/foschema/fop-configuration.xsd?view=co">
      <renderers>
      <renderer mime="application/pdf">


    • A batch script to convert a source XML file to a PDF like this:

      set FOP_COMMAND=C:\OpenSource\fop-0.93\fop.bat
      set FOP_XML_CONFIG_FILE=C:\Projects\WelcomePages\conf\fop_config.xml
      set INPUT_XML=C:\Projects\WelcomePages\src\sample_data.xml
      set OUTPUT_PDF_FILE=C:\Projects\WelcomePages\out\welcomepages.pdf
      set FOP_XSLT=C:\Projects\WelcomePages\src\xslt\sample.xslt
      set XSLT_PARAMS=%XSLT_PARAMS% -param CREATE_WELCOME_PAGES 1

      %FOP_COMMAND% -c %FOP_XML_CONFIG_FILE% -xml %INPUT_XML% -xsl %FOP_XSLT% %XSLT_PARAMS% -pdf %OUTPUT_PDF_FILE% -pdfprofile PDF/A-1b

    • Ant build script (build.xml) in the project like this:

      <project name="welcome_page" default="all" basedir=".">
      <property file="build.properties"/>

      <target name="all" depends="createPDF, openPDF">
      </target>

      <target name="clean">
      <delete file="${pdf.file}" quiet="true" />
      </target>

      <target name="createPDF" depends="clean">
      <exec executable="cmd">
      <arg value="/c"/>
      <arg value="${create.pdf.script}"/>
      <arg value="${pdf.file}"/>
      </exec>
      </target>

      <target name="openPDF">
      <exec executable="cmd">
      <arg value="/c"/>
      <arg value="${open.pdf.script}"/>
      <arg value="${pdf.file}"/>
      </exec>
      </target>

      </project>


    • build.properties file for the Ant Script XML

      pdf.file=welcomepages.pdf
      create.pdf.script=welcomepage.bat
      open.pdf.script=C:/Progra~1/Adobe/ACROBA~1.0/Acrobat/acrobat.exe



  2. In a new project in Eclipse I configured a builder by right clicking and choosing properies on the project, then selected builders and clicked new Ant Build and used the build.xml in the project

  3. Workflow complete, now when changes are saved to any of the Eclipse project files, whether is it the XSLT transforms, the source input XML, or even the config files for Apache FOP, the output and effect of those changes is automatically created and the PDF is automatically opened.



*Right now you have to close the output pdf before saving changes in the eclipse project. I would like to improve this so that I can make changes to the source files even if the previously created pdf is still open in acrobat.

No comments:

About Me

My photo
Lead Java Developer Husband and Father

Tags