FO to PDF

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

class FoToPDF


package de.snowbits.fop;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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 javax.xml.transform.stream.StreamSource;

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 FoToPDF {

	private FopFactory _fopFactory = FopFactory.newInstance();
	private UserAgent _userAgent;
	private boolean _replaceEntity;

	public FoToPDF(boolean replaceEntity) {
		this(null, replaceEntity);
	}

	public FoToPDF(UserAgent userAgent, boolean replaceEntity) {
		_userAgent = userAgent;
		_replaceEntity = replaceEntity;
	}

	public void convert(InputStream inputfo, String outputPDF) throws Exception {
		convert(inputfo, new File(outputPDF));
	}

	public void convert(String inputfo, String outputPDF) throws Exception {
		convert(new File(inputfo), new File(outputPDF));
	}

	public void convert(InputStream inputfo, File outputPDF) throws Exception {
		ByteArrayOutputStream baos = replaceEntity(new BufferedReader(
				new InputStreamReader(inputfo)));

		convert(new StreamSource(new ByteArrayInputStream(baos.toByteArray())),
				outputPDF.getAbsolutePath());
	}

	public void convert(File inputfo, File outputPDF) throws Exception {
		ByteArrayOutputStream baos = replaceEntity(new BufferedReader(
				new FileReader(inputfo)));

		convert(new StreamSource(new ByteArrayInputStream(baos.toByteArray())),
				outputPDF.getAbsolutePath());
	}

	public void convert(Source inputfo, String outputPDF) throws Exception {
		OutputStream out = null;
		try {
			FOUserAgent foUserAgent = _fopFactory.newFOUserAgent();
			FopHelper.setUserAgent(foUserAgent, _userAgent);
			out = new FileOutputStream(outputPDF);
			out = new BufferedOutputStream(out);

			Fop fop = _fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent,
					out);
			Result saxResult = new SAXResult(fop.getDefaultHandler());
			TransformerFactory factory = TransformerFactory.newInstance();
			Transformer transformer = factory.newTransformer();

			transformer
					.setParameter(FopHelper.VERSION_PARAM, FopHelper.VERSION);
			transformer.transform(inputfo, saxResult);
		} finally {
			FopHelper.close(out);
		}
	}

	private ByteArrayOutputStream replaceEntity(BufferedReader reader)
			throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		String line;

		while ((line = reader.readLine()) != null) {
			if (_replaceEntity) {
				baos.write(line.replaceAll("&", "&").getBytes());
			} else {
				baos.write(line.getBytes());
			}
		}

		reader.close();

		return baos;
	}

}
	

class FoToPDFTest


package de.snowbits.fop;
import java.io.ByteArrayInputStream;
import java.io.File;

import junit.framework.Assert;

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


public class FoToPDFTest 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 convertStreamForFileParams() throws Exception {
		testee().convert(new ByteArrayInputStream(readFile(INPUT_FOP_PATH).getBytes()), new File(OUTPUT_PDF_PATH));
		assertTest();
	}
	
	@Test
	public void convertOnlyForFileParams() throws Exception {
		testee().convert(new File(INPUT_FOP_PATH), new File(OUTPUT_PDF_PATH));
		assertTest();
	}
	
	@Test
	public void convertStreamForStringParams() throws Exception {
		testee().convert(new ByteArrayInputStream(readFile(INPUT_FOP_PATH).getBytes()), OUTPUT_PDF_PATH);
		assertTest();
	}
	
	@Test
	public void convertOnlyForStringParams() throws Exception {
		testee().convert(INPUT_FOP_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 FoToPDF testee() {
		return new FoToPDF(getUserAgent(), true);
	}
}
	

Adressen.fo


<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xlink="http://www.w3.org/1999/xlink">
	<fo:layout-master-set>
		<fo:simple-page-master margin-right="2.5cm" margin-left="2.5cm" margin-bottom="2cm" margin-top="2cm" page-width="21cm" page-height="29.7cm" master-name="DIN-A4">
			<fo:region-body margin-right="2.5cm" margin-left="2cm" margin-bottom="1.8cm" margin-top="1.5cm"/>
			<fo:region-before extent="1.3cm" region-name="header"/>
			<fo:region-after extent="1.5cm" region-name="footer"/>
			<fo:region-start extent="1cm" region-name="left"/>
			<fo:region-end extent="2cm" region-name="right"/>
		</fo:simple-page-master>
	</fo:layout-master-set>
	<fo:page-sequence master-reference="DIN-A4">
		<fo:static-content flow-name="header">
			<fo:block text-align="center" font-size="14pt">
                  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">
			<fo:table border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid" width="100%" table-layout="fixed">
				<fo:table-column column-width="3cm"/>
				<fo:table-column column-width="3cm"/>
				<fo:table-column column-width="4cm"/>
				<fo:table-header>
					<fo:table-row>
						<fo:table-cell background-color="#9bafde" color="#ffffff" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal" text-align="center">Name</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#9bafde" color="#ffffff" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal" text-align="center">Straße</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#9bafde" color="#ffffff" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal" text-align="center">PLZ / Ort</fo:block>
						</fo:table-cell>
					</fo:table-row>
				</fo:table-header>
				<fo:table-body>
					<fo:table-row>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">Max Mustermann</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">Mustertraße 45</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">12345 Musterstadt</fo:block>
						</fo:table-cell>
					</fo:table-row>
					<fo:table-row>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">Maxi Musterfrau</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">Musterweg 36</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">12345 Musterstadt</fo:block>
						</fo:table-cell>
					</fo:table-row>
					<fo:table-row>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">Max und Moritz</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">Zu Hause 1a</fo:block>
						</fo:table-cell>
						<fo:table-cell background-color="#FFFFFF" color="#000000" padding-start="5.15pt" vertical-align="top" padding-top="0.0pt" padding-end="5.15pt" number-columns-spanned="1" height="0.0pt" padding-bottom="0.0pt" border-top=".10pt solid #000000" border-bottom=".10pt solid #000000" border-start-width=" .10pt" border-start-color="#000000" border-start-style="solid" border-end-width=".10pt" border-end-color="#000000" border-end-style="solid">
							<fo:block height="12.0pt" font-family="Times" white-space-collapse="false" font-size="10pt" font-weight="normal">12345 Musterstadt</fo:block>
						</fo:table-cell>
					</fo:table-row>					
				</fo:table-body>
			</fo:table>
			<fo:block id="LastPage"/>
		</fo:flow>
	</fo:page-sequence>
</fo:root>
	

Ergebnis:


Adressbuch.pdf


Top


Example
Sitemap Kontakt Impressum