XML to PDF

Das Beispiel beschreibt wie eine erstellte XML Datei und eine XSL Datei in das Ausgabeformat PDF transferiert wird. Die Beschreibungen über die Abhängigkeiten, befinden sich im Menüpunkt eine Ebene höher.

class XmlToPdf


package de.snowbits.fop;

import java.io.OutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;

import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;

public class XmlToPdf extends Generator {
	
	private UserAgent _userAgent;
	
	public XmlToPdf() {
		this(null);
	}
	
	public XmlToPdf(UserAgent userAgent) {
		_userAgent = userAgent;
	}
	
	@Override
	public void convert(Source xsltSource, Source xmlSource, String outputPDF) throws Exception {
		FopFactory fopFactory = FopFactory.newInstance();
		FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
		FopHelper.setUserAgent(foUserAgent, _userAgent);
		
		OutputStream out = new java.io.FileOutputStream(outputPDF);
		out = new java.io.BufferedOutputStream(out);
		try {
		    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
		    Result saxResult = new SAXResult(fop.getDefaultHandler());
		    TransformerFactory factory = TransformerFactory.newInstance();
		    Transformer transformer = factory.newTransformer(xsltSource);
		    
		    transformer.setParameter(FopHelper.VERSION_PARAM, FopHelper.VERSION);
		    transformer.transform(xmlSource, saxResult);
		} finally {
			FopHelper.close(out);
		}
	}
}
	

class XmlToPdfTest


package de.snowbits.fop;

import java.io.File;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class XmlToPdfTest extends AbstractFoTestBase {
	
	@Before
	public void setUp() throws Exception {
		deleteFile(OUTPUT_PDF_PATH);
	}

	@After
	public void tearDown() throws Exception {
		deleteFile(OUTPUT_PDF_PATH);
	}

	@Test
	public void convertXslStremXmlStreamPdfFileParams() throws Exception {
		testee().convert(getXslStylesStream(), getXmlDataStream(), new File(OUTPUT_PDF_PATH));
		assertTest();
	}

	@Test
	public void convertXslStremXmlStreamPdfStringParams() throws Exception {
		testee().convert(getXslStylesStream(), getXmlDataStream(), OUTPUT_PDF_PATH);
		assertTest();
	}
	
	@Test
	public void convertXslStringXmlStreamPdfFileParams() throws Exception {
		testee().convert(INPUT_XSL_PATH, getXmlDataStream(), new File(OUTPUT_PDF_PATH));
		assertTest();
	}
	
	@Test
	public void convertXslStringXmlStreamPdfStringParams() throws Exception {
		testee().convert(INPUT_XSL_PATH, getXmlDataStream(), OUTPUT_PDF_PATH);
		assertTest();
	}
	
	@Test
	public void convertOnlyForFileParams() throws Exception {
		testee().convert(new File(INPUT_XSL_PATH), new File(INPUT_XML_PATH), new File(OUTPUT_PDF_PATH));
		assertTest();
	}
	
	@Test
	public void convertOnlyForStringParams() throws Exception {
		testee().convert(INPUT_XSL_PATH, INPUT_XML_PATH, OUTPUT_PDF_PATH);
		assertTest();
	}

	private void assertTest() {
		Assert.assertTrue("File not exist", existFile(OUTPUT_PDF_PATH));
		Assert.assertTrue("File is empty", getFileSize(OUTPUT_PDF_PATH) > 0);
	}
	
	private XmlToPdf testee() {
		return new XmlToPdf(getUserAgent());
	}
}
	

Adressen.xml


<?xml version="1.0" encoding="UTF-8"?>
<adressen>
   <adresse id="1">
      <name>Max Mustermann</name>
      <strasse>Mustertraße 45</strasse>
      <plz>12345</plz>
      <ort>Musterstadt</ort>
   </adresse>
   <adresse id="2">
      <name>Maxi Musterfrau</name>
      <strasse>Musterweg 36</strasse>
      <plz>12345</plz>
      <ort>Musterstadt</ort>
   </adresse>
   <adresse id="3">
      <name>Max und Moritz</name>
      <strasse>Zu Hause 1a</strasse>
      <plz>12345</plz>
      <ort>Musterstadt</ort>
   </adresse>
</adressen>
	

Adressen.xsl


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0" 
				xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
				xmlns:fo="http://www.w3.org/1999/XSL/Format" 
				xmlns:xlink="http://www.w3.org/1999/xlink">               

   <!-- ATTRIBUT-SETS -->
   <xsl:attribute-set name="header-color">
      <xsl:attribute name="background-color">#9bafde</xsl:attribute>
      <xsl:attribute name="color">#ffffff</xsl:attribute>
   </xsl:attribute-set>   
   <xsl:attribute-set name="cell">
      <xsl:attribute name="background-color">#FFFFFF</xsl:attribute>
      <xsl:attribute name="color">#000000</xsl:attribute>
      <xsl:attribute name="padding-start">5.15pt</xsl:attribute>
      <xsl:attribute name="vertical-align">top</xsl:attribute>
      <xsl:attribute name="padding-top">0.0pt</xsl:attribute>
      <xsl:attribute name="padding-end">5.15pt</xsl:attribute>
      <xsl:attribute name="number-columns-spanned">1</xsl:attribute>
      <xsl:attribute name="height">0.0pt</xsl:attribute>
      <xsl:attribute name="padding-bottom">0.0pt</xsl:attribute>
   </xsl:attribute-set>
   <xsl:attribute-set name="border">
      <xsl:attribute name="border-top">.10pt solid #000000</xsl:attribute>
      <xsl:attribute name="border-bottom">.10pt solid #000000</xsl:attribute>
      <xsl:attribute name="border-start-width"> .10pt</xsl:attribute>
      <xsl:attribute name="border-start-color">#000000</xsl:attribute>
      <xsl:attribute name="border-start-style">solid</xsl:attribute>
      <xsl:attribute name="border-end-width">.10pt</xsl:attribute>
      <xsl:attribute name="border-end-color">#000000</xsl:attribute>
      <xsl:attribute name="border-end-style">solid</xsl:attribute>
   </xsl:attribute-set>
   <xsl:attribute-set name="body-font">
      <xsl:attribute name="height">12.0pt</xsl:attribute>
      <xsl:attribute name="font-family">Times</xsl:attribute>
      <xsl:attribute name="white-space-collapse">false</xsl:attribute>
      <xsl:attribute name="font-size">10pt</xsl:attribute>
      <xsl:attribute name="font-weight">normal</xsl:attribute>
   </xsl:attribute-set>   

   <!-- SEITENAUFTEILUNG -->
   <xsl:template match="/">
      <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
         <fo:layout-master-set>
            <fo:simple-page-master master-name="DIN-A4"
                  page-height="29.7cm" page-width="21cm"
                  margin-top="2cm"     margin-bottom="2cm"
                  margin-left="2.5cm"  margin-right="2.5cm">
               <fo:region-body
                  margin-top="1.5cm" margin-bottom="1.8cm"
                  margin-left="2cm"  margin-right="2.5cm"/>
               <fo:region-before region-name="header" extent="1.3cm"/>
               <fo:region-after  region-name="footer" extent="1.5cm"/>
               <fo:region-start  region-name="left"   extent="1cm"/>
               <fo:region-end    region-name="right"  extent="2cm"/>
            </fo:simple-page-master>
         </fo:layout-master-set>
         <fo:page-sequence master-reference="DIN-A4">
            <fo:static-content flow-name="header">
               <fo:block font-size="14pt" text-align="center">
                  Mein Adressbuch
               </fo:block>
            </fo:static-content>
            <fo:static-content flow-name="footer">
               <fo:block text-align="center">
                  Seite <fo:page-number/> von <fo:page-number-citation ref-id="LastPage"/>
               </fo:block>
            </fo:static-content>
            <fo:flow flow-name="xsl-region-body">
               <xsl:apply-templates/>
               <fo:block id="LastPage"/>
            </fo:flow>
         </fo:page-sequence>
      </fo:root>
   </xsl:template>

   <!-- TEMPLATE Adressen = Root -->
   <xsl:template match="adressen">
      <fo:table xsl:use-attribute-sets="border" table-layout="fixed" width="100%">
         <fo:table-column column-width="3cm"/>
         <fo:table-column column-width="3cm"/>
         <fo:table-column column-width="4cm"/>
         <fo:table-header>
            <xsl:call-template name="table-head"/>
         </fo:table-header>
         <fo:table-body>
            <xsl:apply-templates select="adresse"/>
         </fo:table-body>
      </fo:table>
   </xsl:template>

   <!-- TABELLENKOPF -->
   <xsl:template name="table-head">
      <fo:table-row>
         <fo:table-cell xsl:use-attribute-sets="cell border header-color">
            <fo:block xsl:use-attribute-sets="body-font"
                      text-align="center">Name</fo:block>
         </fo:table-cell>
         <fo:table-cell xsl:use-attribute-sets="cell border header-color">
            <fo:block xsl:use-attribute-sets="body-font"
                      text-align="center">Straße</fo:block>
         </fo:table-cell>
         <fo:table-cell xsl:use-attribute-sets="cell border header-color">
            <fo:block xsl:use-attribute-sets="body-font"
                      text-align="center">PLZ und Ort</fo:block>
         </fo:table-cell>
      </fo:table-row>
   </xsl:template>
   
   <!-- TEMPLATE adresse -->
   <xsl:template match="adresse">
      <fo:table-row>
         <fo:table-cell xsl:use-attribute-sets="cell border">
            <fo:block xsl:use-attribute-sets="body-font">
               <xsl:value-of select="name"/>
            </fo:block>
         </fo:table-cell>
         <fo:table-cell xsl:use-attribute-sets="cell border">
            <fo:block xsl:use-attribute-sets="body-font">
               <xsl:value-of select="strasse"/>
            </fo:block>
         </fo:table-cell>
         <fo:table-cell xsl:use-attribute-sets="cell border">
            <fo:block xsl:use-attribute-sets="body-font">
               <xsl:value-of select="plz"/>
               <xsl:text> </xsl:text>
               <xsl:value-of select="ort"/>
            </fo:block>
         </fo:table-cell>
      </fo:table-row>
   </xsl:template>

</xsl:stylesheet>
	

Ergebnis:


Adressbuch.pdf


Top


Example
Sitemap Kontakt Impressum