Java to XML to PDF

Das Beispiel beschreibt wie aus einer Liste mit Kontakten ein XML String erstellt wird und dann mit einer XSL Datei in das Ausgabeformat PDF transferiert wird. Die Beschreibungen über die Abhängigkeiten, befinden sich im Menüpunkt eine Ebene höher.

class AdressbuchToPDF


package de.snowbits.fop;

import static de.snowbits.tools.XMLTools.concat;
import static de.snowbits.tools.XMLTools.createDoc;
import static de.snowbits.tools.XMLTools.elm;
import static de.snowbits.tools.XMLTools.elmConcat;
import static de.snowbits.tools.XMLTools.elmRoot;
import static de.snowbits.tools.XMLTools.makeToString;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class AdressbuchToPDF {
	
	private InputStream _inputXSLT;
	private File _outputPDF;
	
	public AdressbuchToPDF(InputStream inputXSLT, File outputPDF) {
		_inputXSLT = inputXSLT;
		_outputPDF = outputPDF;
	}

	public void generate(List<Kontakt> kontakte, UserAgent userAgent, String encoding) throws Exception {
		String xml = createXml(kontakte, encoding);
		XmlToPdf xmlToPdf = new XmlToPdf(userAgent);
		xmlToPdf.convert(_inputXSLT, new ByteArrayInputStream(xml.getBytes()), _outputPDF);
	}

	private String createXml(List<Kontakt> kontakte, String encoding) throws Exception {
		Document doc = createDoc();
		Element root = elmRoot(doc, "adressen");

		for (Kontakt element : kontakte) {
			Element adresse = elmConcat(doc, "adresse", 
					elm(doc, "name", element.getName()), 
					elm(doc, "strasse", element.getStrasse()), 
					elm(doc, "plz", element.getPlz()), 
					elm(doc, "ort", element.getOrt()));
			concat(root, adresse);
		}
		
		
		return makeToString(doc, encoding);
	}
}
	

class Kontakt


package de.snowbits.fop;

public class Kontakt {

	private String _name;
	private String _strasse;
	private String _plz;
	private String _ort;
	
	public Kontakt(String name, String strasse, String plz,	String ort) {
		_name = name;
		_strasse = strasse;
		_plz = plz;
		_ort = ort;
	}

	public String getName() {
		return _name;
	}

	public String getStrasse() {
		return _strasse;
	}

	public String getPlz() {
		return _plz;
	}

	public String getOrt() {
		return _ort;
	}

}
	

class AdressbuchToPDFTest


package de.snowbits.fop;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

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

public class AdressbuchToPDFTest extends AbstractFoTestBase {
	
	private List<Kontakt> _kontakte;

	@Before
	public void setUp() throws Exception {
		_kontakte = null;
		deleteFile(OUTPUT_PDF_PATH);
	}
	
	@After
	public void tearDown() throws Exception {
		deleteFile(OUTPUT_PDF_PATH);
	}

	@Test
	public void generateAdressbuchPDF() throws Exception {
		load();
		generate();
		Assert.assertTrue("File not exist", existFile(OUTPUT_PDF_PATH));
		Assert.assertTrue("File is empty", getFileSize(OUTPUT_PDF_PATH) > 0);		
	}
	
	private void generate() throws Exception {
		testee().generate(_kontakte, getUserAgent(), "iso-8859-1");
	}

	private void load() {
		_kontakte = new ArrayList<Kontakt>();
		_kontakte.add(new Kontakt("Max Mustermann", "Mustertraße 45", "12345", "Musterstadt"));
		_kontakte.add(new Kontakt("Maxi Musterfrau", "Musterweg 36", "12345", "Musterstadt"));
		_kontakte.add(new Kontakt("Max und Moritz", "Zu Hause 1a", "12345", "Musterstadt"));
	}

	private  AdressbuchToPDF testee() throws Exception {
		return new AdressbuchToPDF(getXslStylesStream(), new File(OUTPUT_PDF_PATH));
	}
}
	

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