You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by Max Dcosta <Ma...@Netdecisions.com> on 2002/06/17 12:51:55 UTC
How do i create a basic table of contents like this ???
Dear Foppers,
How do i create a basic table of contents which i would like to display like
this
===============================================
Table of Contents
1. Chapter 1
1.1 xxxx
...................................................................... Pg 2
1.2 yyyy
...................................................................... Pg 3
1.3 zzzz
...................................................................... Pg 4
2. Chapter 2
2.1 xxxx
...................................................................... Pg 5
2.2 yyyy
...................................................................... Pg 6
2.3 zzzz
...................................................................... Pg 7
3. Conclusion
3.1 Acknowledgements
..................................................Pg8
===============================================
Can anyone pls give me a working example in FO with a internal hyperlink,
for example, when i click 1.1 xxxx, I directly go to Page 2 where the xxx
matter is stored within a block and how to i get the corresponding page
numbers appearing to the right in the correct order where the particular
content is stored. Is this dynamically taken care of. I am confused on how
it is done. Please guide me with some example or so my friends.
Please help me on this one my friends.
Thanks and best wishes,
Max :)
Max William D'costa | Interactive Designer | netdecisions
6th Floor, MET Building, Gen. A K Vaidya Chowk,
Bandra Reclamation, Mumbai 400050, India
t +91 (0)22 644 0000 Ext: 165 f +91 (0)22 655 8048
http://www.netdecisions.com
max.dcosta@netdecisions.com
Re: How do i create a basic table of contents like this ???
Posted by Bruno Rondepierre <br...@mdeo.fr>.
You will find in the rest of this mail:
- an extract of an XML file,
- an extract of the XSLT file,
- an sample servlet.
that generates a PDF file with both a table of content and bookmarks (for
PDF).
DESCRIPTION OF THE SAMPLE
-------------------------------------
The goal of the sample is to create a PDF file containing book's description
(a title and a description). Books are classified using book's type (such as
Java, Perl).
In the generated PDF:
- I force a page break for each book's type,
- I want bookmarks both for the book's types and the books
To used the sample servlet, you first have to adapt pathname (in the
beginning of the method called "ProcessRequest") and then call
<yourserver/servlet/tstBookmark> to generate PDF from xml and xsl files.
THE XML FILE (tstBookmark.xml)
----------------------------------------------------------------------------
-- Begin
<?xml version="1.0" encoding="ISO-8859-1"?>
<page>
<navigation name="XML">
<menu id="1">
<label>Exemples</label>
<url>
<pathinfo>/xml/servlet/xmlHome</pathinfo>
</url>
<description>Exemples de Servlets permettant de valider l'utilisation des
différents outils</description>
<menu id="2">
<label>Snoop Servlet</label>
<url>
<pathinfo>/xml/servlet/SnoopServlet</pathinfo>
<querystring>trace=on</querystring>
</url>
<description>Servlet de test de l'environnement d'un servlet (Session,
paramètres...)</description>
</menu>
<menu id="3">
<label>TstXsltServlet (Xml File)</label>
<url>
<pathinfo>/xml/servlet/TstXslManager</pathinfo>
</url>
<description>Test de création d'une page HTML par processing XML / XSLT
à partir d'un fichier XML</description>
</menu>
<menu id="10">
<label>TstXsltServlet (DOM)</label>
<url>
<pathinfo>/xml/servlet/xmlHome</pathinfo>
</url>
<description>Test de création d'une page HTML par processing XML / XSLT
à partir d'une arborescence DOM</description>
</menu>
<menu id="9">
<label>Test FOP (File fo)</label>
<url>
<pathinfo>/xml/servlet/TstFopFileFo</pathinfo>
</url>
<description>Création d'un fichier PDF à partir d'un fichier
XSL-FO.</description>
</menu>
<menu id="11">
<label>Test FOP (File xml)</label>
<url>
<pathinfo>/xml/servlet/TstFopFileXml</pathinfo>
</url>
<description>Création d'un fichier PDF à partir d'un fichier xml et
d'une feuille de style xslt.</description>
</menu>
<menu id="12">
<label>TstXmlServlet (JDOM)</label>
<url>
<pathinfo>/xml/servlet/xmlBookHome</pathinfo>
</url>
<description>Test de création d'un arbre JDOM.</description>
</menu>
</menu>
<menu id="4">
<label>Accueil</label>
<url>
<pathinfo>/xml/servlet/xmlHome</pathinfo>
</url>
<menu id="5">
<label>Recherche</label>
<menu id="6">
<label>Alphabétique</label>
</menu>
<menu id="7">
<label>Multi-critères</label>
</menu>
</menu>
<menu id="8">
<label>Référence</label>
</menu>
</menu>
</navigation>
<books>
<book id="2">
<title>Java and XML</title>
<parution>
<month>9</month>
<year>2001</year>
</parution>
<isbn>0-596-00197-5</isbn>
<images>
<image>javaxml.gif</image>
<thumbail>javaxml.gif</thumbail>
</images>
<abstract>New chapters on Advanced SAX, Advanced DOM, SOAP, and data
binding, as well as new examples throughout, bring the second edition of
Java & XML thoroughly up to date. The book focuses entirely on using XML
from Java applications.</abstract>
<description>While the XML "buzz" still dominates talk among Internet
developers, the critical need is for information that cuts through the hype
and lets Java programmers put XML to work. Java & XML shows how to use
the APIs, tools, and tricks of XML to build real-world applications, with
the end result that both the data and the code are portable.
This second edition of Java & XML adds chapters on Advanced SAX and
Advanced DOM, new chapters on SOAP and data binding, and new examples
throughout. A concise chapter on XML basics introduces concepts, and the
rest of the book focuses on using XML from your Java applications. Java
developers who need to work with XML, or think that they will in the
future--as well as developers involved in the new peer-to-peer movement,
messaging, or web services--will find the new Java & XML a constant
companion.
This book covers:
- The basics of XML, including DTDs, namespaces, XML Schema, XPath, and XSL
- The SAX API, including all handlers, the SAX 2 extensions, filters, and
writers
- The DOM API, including DOM Level 2, Level 3, and the Traversal, Range,
CSS, Events, and HTML modules.
- The JDOM API, including the core, a look at XPath support, and JDOM as a
JSR
- Using web publishing frameworks like Apache Cocoon
- Developing applications with XML-RPC
- Using SOAP and UDDI for web services
- Data Binding, using both DTDs and XML Schema for constraints
- Building business-to-business applications with XML
- Building information channels with RSS and dynamic content with XSP
Includes a quick reference on SAX 2.0, DOM Level 2, and JDOM.</description>
<authors>
<author id="2">
<lastname>McLaughlin</lastname>
<firstname>Brett</firstname>
</author>
</authors>
</book>
<book id="1">
<title>Java and XSLT</title>
<subtitle>XML transformations in Java</subtitle>
<parution>
<month>9</month>
<year>2001</year>
</parution>
<isbn>0-596-00143-6</isbn>
<images>
<image>javaxslt.gif</image>
<thumbail>javaxslt.gif</thumbail>
</images>
<abstract>Learn how to use XSL transformations in Java programs ranging
from stand-alone applications to servlets. Java and XSLT introduces XSLT and
then shows you how to apply transformations in real-world
situations.</abstract>
<description>The power of XSLT is its ability to change the structure or
format of any content that can be converted to XML. Java and XSLT shows you
how to use XSL transformations in Java programs ranging from stand-alone
applications to servlets. After an introduction to XSLT, the book focuses on
applying transformations in some real-world scenarios, such as developing a
discussion forum, transforming documents from one form to another, and
generating content for wireless devices.
Java and XSLT discusses several common XSLT processors and the TRAX API,
paying special attention to performance issues. Although there's a brief
tutorial introduction to the XSLT language, the primary focus of the book
isn't on learning XSLT or developing stylesheets; it's on making practical
use of transformations in Java code.
The book covers:
- Introduction and Technology Review
- XSLT--The Basics
- XSLT--Beyond The Basics
- Java Web Architecture
- Programmatic Interfaces to XSLT Processors
- Using XSLT with Servlets
- Discussion Forum Implementation
- Advanced XSLT Web Techniques
- Testing, Tuning and Development Environments
- WAP and WML
- XSLT and Wireless Examples</description>
<authors>
<author id="1">
<lastname>M. Burke</lastname>
<firstname>Eric</firstname>
</author>
</authors>
</book>
</books>
</page>
----------------------------------------------------------------------------
-- End
THE XSL FILE (tstBookmark.xslfo)
----------------------------------------------------------------------------
-- Begin
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:fox="http://xml.apache.org/fop/extensions">
<xsl:output method="xml"/>
<!-- Styles -->
<xsl:attribute-set name="catBg">
<xsl:attribute name="background-color">#00139F</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="catTitle">
<xsl:attribute name="font-family">Helvetica</xsl:attribute>
<xsl:attribute name="line-height">30pt</xsl:attribute>
<xsl:attribute name="font-size">24pt</xsl:attribute>
<xsl:attribute name="font-weight">bold</xsl:attribute>
<xsl:attribute name="color">#FFFFFF</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="bookTitle">
<xsl:attribute name="font-family">Helvetica</xsl:attribute>
<xsl:attribute name="line-height">20pt</xsl:attribute>
<xsl:attribute name="font-size">18pt</xsl:attribute>
<xsl:attribute name="font-weight">bold</xsl:attribute>
<xsl:attribute name="color">#00139F</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="bookSubTitle">
<xsl:attribute name="font-family">Helvetica</xsl:attribute>
<xsl:attribute name="line-height">14pt</xsl:attribute>
<xsl:attribute name="font-size">12pt</xsl:attribute>
<xsl:attribute name="color">#00139F</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="bookNormal12">
<xsl:attribute name="font-family">Helvetica</xsl:attribute>
<xsl:attribute name="line-height">14pt</xsl:attribute>
<xsl:attribute name="font-size">12pt</xsl:attribute>
<xsl:attribute name="text-align">justify</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="toc1">
<xsl:attribute name="font-family">Helvetica</xsl:attribute>
<xsl:attribute name="line-height">16pt</xsl:attribute>
<xsl:attribute name="font-size">14pt</xsl:attribute>
<xsl:attribute name="font-weight">bold</xsl:attribute>
<xsl:attribute name="color">#00139F</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="toc2">
<xsl:attribute name="font-family">Helvetica</xsl:attribute>
<xsl:attribute name="line-height">14pt</xsl:attribute>
<xsl:attribute name="font-size">12pt</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="headerStyle">
<xsl:attribute name="font-family">Helvetica</xsl:attribute>
<xsl:attribute name="line-height">11pt</xsl:attribute>
<xsl:attribute name="font-size">10pt</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="footerStyle">
<xsl:attribute name="font-family">Helvetica</xsl:attribute>
<xsl:attribute name="line-height">11pt</xsl:attribute>
<xsl:attribute name="font-size">10pt</xsl:attribute>
<xsl:attribute name="text-align">center</xsl:attribute>
</xsl:attribute-set>
<!-- DOCUMENT DEFINITON -->
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:fox="http://xml.apache.org/fop/extensions">
<fo:layout-master-set>
<fo:simple-page-master master-name="A4"
page-height="297mm" page-width="210mm"
margin-right="75pt" margin-left="75pt" margin-bottom="25pt"
margin-top="25pt"
>
<fo:region-before extent="25pt"/>
<fo:region-body margin-top="50pt" margin-bottom="50pt"/>
<fo:region-after extent="25pt"/>
</fo:simple-page-master>
</fo:layout-master-set>
<xsl:apply-templates select="page/content/booktypes/booktype"
mode="bookmark"/>
<xsl:apply-templates select="page/content/booktypes" mode="toc"/>
<xsl:apply-templates select="page/content/booktypes/booktype"
mode="content"/>
</fo:root>
</xsl:template>
<!-- BOOKMARKS FOR THE PDF -->
<!-- Bookmark: book's type -->
<xsl:template match="booktype" mode="bookmark">
<fox:outline internal-destination="toc{@id}">
<fox:label><xsl:value-of select="label"/></fox:label>
<xsl:if test="descendant::books/book">
<xsl:apply-templates select="books/book" mode="bookmark">
<xsl:with-param name="cat" select="@id"/>
</xsl:apply-templates>
</xsl:if>
</fox:outline>
</xsl:template>
<!-- Bookmark: book -->
<xsl:template match="books/book" mode="bookmark">
<xsl:param name="cat"/>
<fox:outline internal-destination="toc{$cat}book{@id}">
<fox:label><xsl:value-of select="title"/></fox:label>
</fox:outline>
</xsl:template>
<!-- TABLE OF CONTENT -->
<!-- the table of content itself -->
<xsl:template match="booktypes" mode="toc">
<fo:page-sequence master-reference="A4" initial-page-number="1">
<fo:static-content flow-name="xsl-region-before">
<fo:block xsl:use-attribute-sets="headerStyle">Table of
content</fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-after">
<fo:block xsl:use-attribute-sets="footerStyle">Page
<fo:page-number/></fo:block>
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<fo:wrapper font-family="Times" font-size="12pt" text-align="justify">
<fo:table table-layout="fixed">
<fo:table-column column-width="5mm"/>
<fo:table-column column-width="120mm"/>
<fo:table-column column-width="40mm"/>
<fo:table-body>
<fo:table-row xsl:use-attribute-sets="catBg">
<fo:table-cell number-columns-spanned="3">
<fo:block id="toc" font-family="Helvetica"
line-height="30pt" font-size="24pt" font-weight="bold" color="#FFFFFF">Table
of content</fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:apply-templates select="booktype" mode="toc"/>
</fo:table-body>
</fo:table>
</fo:wrapper>
</fo:flow>
</fo:page-sequence>
</xsl:template>
<!-- Type of books: level 1 -->
<xsl:template match="booktype" mode="toc">
<xsl:if test="descendant::books/book">
<!-- A space before each book's type -->
<fo:table-row>
<fo:table-cell number-columns-spanned="3">
<fo:block space-before.optimum="20pt" >
<xsl:text>
</xsl:text>
</fo:block>
</fo:table-cell>
</fo:table-row>
<!-- The book's type itself -->
<fo:table-row xsl:use-attribute-sets="toc1">
<fo:table-cell number-columns-spanned="2">
<fo:block xsl:use-attribute-sets="toc1">
<fo:basic-link internal-destination="toc{@id}"><xsl:value-of
select="label"/></fo:basic-link>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block xsl:use-attribute-sets="toc1"
text-align="end"><fo:page-number-citation ref-id="toc{@id}"/></fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:apply-templates select="books/book" mode="toc">
<xsl:with-param name="cat" select="@id"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
<!-- A book in the table of content -->
<xsl:template match="books/book" mode="toc">
<xsl:param name="cat"/>
<fo:table-row>
<fo:table-cell>
<fo:block xsl:use-attribute-sets="toc2"></fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block xsl:use-attribute-sets="toc2">
<fo:basic-link
internal-destination="toc{$cat}book{@id}"><xsl:value-of
select="title"/></fo:basic-link>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block xsl:use-attribute-sets="toc2"
text-align="right"><fo:page-number-citation
ref-id="toc{$cat}book{@id}"/></fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
<!-- DOCUMENT'S CONTENT -->
<!-- Booktype: new page for each book's type -->
<xsl:template match="booktype" mode="content">
<xsl:if test="books/book">
<fo:page-sequence master-reference="A4">
<fo:flow flow-name="xsl-region-body">
<fo:wrapper font-family="Times" font-size="12pt"
text-align="justify">
<fo:table>
<fo:table-column column-width="165mm"/>
<fo:table-body>
<fo:table-row background-color="#00139F">
<fo:table-cell>
<fo:block id="toc{@id}" font-family="Helvetica"
line-height="30pt" font-size="24pt" font-weight="bold" color="#FFFFFF">
<xsl:value-of select="label"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
<xsl:apply-templates select="books" mode="content">
<xsl:with-param name="cat" select="@id"/>
</xsl:apply-templates>
</fo:wrapper>
</fo:flow>
</fo:page-sequence>
</xsl:if>
</xsl:template>
<!-- the books of one book's type -->
<xsl:template match="books" mode="content">
<xsl:param name="cat"/>
<fo:table>
<fo:table-column column-width="165mm"/>
<fo:table-body>
<xsl:apply-templates select="book">
<xsl:with-param name="cat" select="$cat"/>
</xsl:apply-templates>
</fo:table-body>
</fo:table>
</xsl:template>
<!-- one book -->
<xsl:template match="book">
<xsl:param name="cat"/>
<fo:table-row>
<fo:table-cell>
<fo:block space-before.optimum="10pt">
<xsl:text>
</xsl:text>
</fo:block>
</fo:table-cell>
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block id="toc{$cat}book{@id}" font-family="Helvetica"
line-height="20pt" font-size="18pt" font-weight="bold" color="#00139F">
<xsl:value-of select="title"/>
</fo:block>
<xsl:if test="abstract">
<fo:block space-before.optimum="5pt" ><xsl:value-of
select="abstract"/></fo:block>
</xsl:if>
</fo:table-cell>
</fo:table-row>
</xsl:template>
</xsl:stylesheet>
----------------------------------------------------------------------------
-- End
THE SAMPLE SERVLET
----------------------------------------------------------------------------
-- Begin
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.log.*;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.Version;
import org.apache.fop.apps.XSLTInputHandler;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
public class tstBookmark extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{ ProcessRequest(request, response); }
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{ ProcessRequest(request, response); }
public void ProcessRequest(HttpServletRequest request, HttpServletResponse
response) throws ServletException
{
// Files used
String xmlFile =
"E:/web/jakarta-tomcat-3.3.1/webapps/xml/data/xml/tstBookmark.xml";
String xslFile =
"E:/web/jakarta-tomcat-3.3.1/webapps/xml/data/xslfo/tstBookmark.xslfo";
// Generate PDF
try
{
// Initialize Logger
Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
Logger log = hierarchy.getLoggerFor("fop");
log.setPriority(Priority.WARN);
// Initialier Input Handler
XSLTInputHandler input = new XSLTInputHandler(new File(xmlFile), new
File(xslFile));
// Render
ByteArrayOutputStream out = new ByteArrayOutputStream();
Driver driver = new Driver();
driver.setLogger(log);
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
driver.render(input.getParser(), input.getInputSource());
// show PDF
response.setContentType("application/pdf");
byte[] content = out.toByteArray();
response.setContentLength(content.length);
response.getOutputStream().write(content);
response.getOutputStream().flush();
}
catch (Exception e)
{ throw new ServletException("error while creating pdf directly: " +
e.toString()); }
}
}
----------------------------------------------------------------------------
-- End
----- Original Message -----
From: "Max Dcosta" <Ma...@Netdecisions.com>
To: <fo...@xml.apache.org>
Sent: Monday, June 17, 2002 12:51 PM
Subject: How do i create a basic table of contents like this ???
> Dear Foppers,
>
> How do i create a basic table of contents which i would like to display
like
> this
>
>
> ===============================================
>
> Table of Contents
>
> 1. Chapter 1
> 1.1 xxxx
> ...................................................................... Pg
2
> 1.2 yyyy
> ...................................................................... Pg
3
> 1.3 zzzz
> ...................................................................... Pg
4
>
> 2. Chapter 2
> 2.1 xxxx
> ...................................................................... Pg
5
> 2.2 yyyy
> ...................................................................... Pg
6
> 2.3 zzzz
> ...................................................................... Pg
7
>
> 3. Conclusion
> 3.1 Acknowledgements
> ..................................................Pg8
>
> ===============================================
>
>
> Can anyone pls give me a working example in FO with a internal hyperlink,
> for example, when i click 1.1 xxxx, I directly go to Page 2 where the xxx
> matter is stored within a block and how to i get the corresponding page
> numbers appearing to the right in the correct order where the particular
> content is stored. Is this dynamically taken care of. I am confused on how
> it is done. Please guide me with some example or so my friends.
>
>
> Please help me on this one my friends.
>
> Thanks and best wishes,
>
> Max :)
>
>
>
>
> Max William D'costa | Interactive Designer | netdecisions
> 6th Floor, MET Building, Gen. A K Vaidya Chowk,
> Bandra Reclamation, Mumbai 400050, India
> t +91 (0)22 644 0000 Ext: 165 f +91 (0)22 655 8048
> http://www.netdecisions.com
> max.dcosta@netdecisions.com
>
>