DocBookToPDF

HomePage | RecentChanges | Preferences

__________ From DocBook XML to PDF __________

DocBook? and Maven

Tools

Documentation

From docbook XML to FO

With Saxon using Ant

You can tell Ant that you want to use Saxon by specifying it on the classpath attribute of the xslt or style task:

<property name="docbook.home"    value="c:/java/docbook-xsl-1.68.1"/>
<property name="saxon.dir"       value="c:/java/saxon6_5_3"/>
<property name="xml.dir"         value="xml"/>
<property name="fo.dir"          value="fo"/>

<target name="xslt" depends="clean,init" 
        description="Converts docbook xml to fo">
     <xslt  style="${docbook.home}/fo/docbook.xsl"
            classpath="${saxon.dir}/saxon.jar"
            basedir="${xml.dir}"
            extension=".fo"
            destdir="${fo.dir}" />
</target>

E:\java\docbook>ant xslt
Buildfile: build.xml

xslt:
     [xslt] Transforming into E:\java\docbook\fo
     [xslt] Processing E:\java\docbook\xml\anotherbook.xml to E:\java\docbook\fo\anotherbook.fo
     [xslt] Loading stylesheet C:\java\docbook-xsl-1.68.1\fo\docbook.xsl
     [xslt] Making portrait pages on USletter paper (8.5inx11in)
     [xslt] Processing E:\java\docbook\xml\saxonbook.xml to E:\java\docbook\fo\saxonbook.fo
     [xslt] Making portrait pages on USletter paper (8.5inx11in)

BUILD SUCCESSFUL
Total time: 24 seconds
E:\java\docbook>

From FO to PDF

With Apache FOP Using Ant

Once you [finally] get the .fo file created, use the 'fop' Ant task provided by Apache FOP to do the final transform:

    <property name="fop.home" value="c:/java/fop-0.20.5"/>
    <property name="fo.dir" value="fo"/>
    <property name="pdf.dir" value="pdf"/>

    <taskdef name="fop"
            classname="org.apache.fop.tools.anttasks.Fop">
        <classpath>
            <pathelement location="${fop.home}/build/fop.jar"/>
            <pathelement location="${fop.home}/lib/avalon-framework-cvs-20020806.jar"/>
            <pathelement location="${fop.home}/lib/batik.jar"/>
        </classpath>
    </taskdef>

<!-- This example converts all XSL-FO files within an entire directory to PDF: -->

    <target name="generate-multiple-pdf"
            description="Generates multiple PDF files">
        <fop format="application/pdf" outdir="${pdf.dir}">
            <fileset dir="${fo.dir}">
                <include name="*.fo"/>
            </fileset>
        </fop>
    </target>
    

<!-- The following example converts a single XSL-FO file to a PDF document: -->

    <target name="generate-pdf" description="Generates a single PDF file">
        <fop format="application/pdf"
                fofile="mybook.fo"
                outfile="mybook.pdf" />
    </target>

Other XML to FO options

Cygwin and 'xmlto'

The xmlto command in Cygwin converts xml to various formats, one of which is pdf. You can use the default stylesheets

     $ xmlto fo mybook.xml

or you can tell it what stylesheet to use

     $ xmlto -x /path/to/docbook-xsl-1.68.1/fo/docbook.xsl fo mybook.xml

Alternately: Use Xalan

Same thing as the Saxon example above, only with xalan.jar specified instead of saxon.jar

<target name="xalan" depends="clean,init"
            description="Converts docbook xml to fo">
        <xslt style="${docbook.home}/fo/docbook.xsl"
                classpath="${xalan.dir}/xalan.jar"
                basedir="${xml.dir}"
                extension=".fo"
                destdir="${fo.dir}" />
    </target>

Saxon in an Ant <java> task

If for some reason the xslt (or style) task starts misbehaving, you can use Saxon in a <java> task, as if you were using it from the command line:

See docbook-apps mailing list archives for 2/17/2005: http://lists.oasis-open.org/archives/docbook-apps/200502/msg00122.html

Command Line Saxon: http://saxon.sourceforge.net/saxon6.5.3/using-xsl.html#Command-line

<property name="docbook.home" value="c:/java/docbook-xsl-1.68.1"/>
<property name="input.xml.file"  value="newbook.xml"/>
<property name="output.fo.file"  value="newbook.fo"/>
<property name="saxon.dir"       value="c:/java/saxon6_5_3"/>

<path id="saxon.classpath">
        <pathelement location="${saxon.dir}/saxon.jar"/>  
</path>

<target name="saxon" description="Converts docbook xml to fo">
    <java  classpathref="saxon.classpath"
           classname="com.icl.saxon.StyleSheet"
           fork="yes"
           dir=".">
      <arg line="-o ${output.fo.file}"/>
      <arg line="${input.xml.file}"/>
      <arg line="${docbook.home}/fo/docbook.xsl"/>
    </java>
</target>

Possible Errors

Xalan

With JDK 1.5.0_01 and Xalan 2.6.0, if you see this:

U:\java\docbook>ant xslt-multiple
Buildfile: build.xml

xslt-multiple:
     [xslt] Transforming into U:\java\docbook
     [xslt] Processing U:\java\docbook\mybook.xml to U:\java\docbook\mybook.fo
     [xslt] Loading stylesheet C:\java\docbook-xsl-1.68.1\fo\docbook.xsl

BUILD FAILED
java.lang.ExceptionInInitializerError

Total time: 5 seconds
U:\java\docbook>

Try this:

Error with the JDK 1.5.0_01 default

JDK 1.5 uses Xalan XSLTC by default, which cannot handle the current docbook stylesheets

<property name="docs.src" value="."/>
<property name="docs.dest" value="."/>
<property name="docbook.home" value="c:/java/docbook-xsl-1.68.1"/>

<!-- An attempt to convert docbook xml to fo using Ant -->

<target name="xslt-multiple" description="Converts docbook xml to fo">
     <xslt  style="${docbook.home}/fo/docbook.xsl"
            destdir="."
            extension=".fo"
            includes="*.xml"
            force="true" />
</target>

$ ant xslt
Buildfile: build.xml

xslt:
    [style] Transforming into U:\java\docbook
    [style] Processing U:\java\docbook\build.xml to U:\java\docbook\build.fo
    [style] Loading stylesheet C:\java\docbook-xsl-1.68.1\fo\docbook.xsl
    [style] : Error! file:/C:/java/docbook-xsl-1.68.1/fo/formal.xsl: line 493: Attribute 'border-left-style' outside of element.
    [style] : Error! file:/C:/java/docbook-xsl-1.68.1/fo/formal.xsl: line 494: Attribute 'border-right-style' outside of element.
    [style] : Error! file:/C:/java/docbook-xsl-1.68.1/fo/formal.xsl: line 495: Attribute 'border-top-style' outside of element.
    [style] : Error! file:/C:/java/docbook-xsl-1.68.1/fo/formal.xsl: line 496: Attribute 'border-bottom-style' outside of element.
    [style] : Error! The first argument to the non-static Java function 'dayAbbreviation' is not a valid object reference.
    [style] : Error! Cannot convert data-type 'void' to 'reference'.
    [style] : Fatal Error! Could not compile stylesheet
    [style] Failed to process U:\java\docbook\build.xml

BUILD FAILED
U:\java\docbook\build.xml:32: Fatal error during transformation

Total time: 7 seconds

wsmoak@A2000018 /cygdrive/u/java/docbook
$

Contact

Wendy Smoak wsmoak@gmail.com


HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited January 25, 2008 8:32 pm by WendySmoak (diff)
Search: