Generate PDF From XML Or Java Object Using Apache FOP
What is FOP(Formatting Objects Processor)?
Formatting Objects Processor (FOP, also known as Apache FOP) is a Java application that converts XSL Formatting Objects (XSL-FO) files to PDF or other printable formats.
Formatting Objects Processor (FOP, also known as Apache FOP) is a Java application that converts XSL Formatting Objects (XSL-FO) files to PDF or other printable formats.
Find more details on below link:
https://xmlgraphics.apache.org/fop/Find this project on Github
https://github.com/javacodenet/ApacheFopDemopom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.javacodenet</groupId> | |
<artifactId>apachefopdemo</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>xml-apis</groupId> | |
<artifactId>xml-apis</artifactId> | |
<version>2.0.0</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.xml</groupId> | |
<artifactId>jaxb-api</artifactId> | |
<version>2.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.apache.xmlgraphics</groupId> | |
<artifactId>fop</artifactId> | |
<version>1.0</version> | |
</dependency> | |
<dependency> | |
<groupId>avalon-framework</groupId> | |
<artifactId>avalon-framework</artifactId> | |
<version>4.1.3</version> | |
</dependency> | |
<dependency> | |
<groupId>commons-io</groupId> | |
<artifactId>commons-io</artifactId> | |
<version>2.0.1</version> | |
</dependency> | |
<dependency> | |
<groupId>commons-logging</groupId> | |
<artifactId>commons-logging</artifactId> | |
<version>1.0.3</version> | |
</dependency> | |
</dependencies> | |
</project> |
ApacheFOPDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.fop.apps.*; | |
import javax.xml.bind.JAXBContext; | |
import javax.xml.bind.JAXBException; | |
import javax.xml.bind.Marshaller; | |
import javax.xml.transform.Result; | |
import javax.xml.transform.Transformer; | |
import javax.xml.transform.TransformerException; | |
import javax.xml.transform.TransformerFactory; | |
import javax.xml.transform.sax.SAXResult; | |
import javax.xml.transform.stream.StreamSource; | |
import java.io.*; | |
import java.util.Arrays; | |
public class ApacheFOPDemo { | |
public static void main(String[] args) { | |
try { | |
generatePDFFromXml(); | |
generatePDFFromJavaObject(); | |
} catch (FOPException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (TransformerException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void generatePDFFromXml() | |
throws IOException, FOPException, TransformerException { | |
// the XML file which provides the input | |
StreamSource xmlStreamSource = new StreamSource(new File("book.xml")); | |
generatePDF(xmlStreamSource); | |
} | |
private static void generatePDFFromJavaObject() throws Exception { | |
Books books = new Books(); | |
Book book1 = getBook("A Time To Kill", 1989); | |
Book book2 = getBook("The Film", 1991); | |
Book book3 = getBook("The Client", 1993); | |
books.setBookList(Arrays.asList(book1, book2, book3)); | |
books.setAuthor("John Grisham"); | |
ByteArrayOutputStream xmlSource = getXMLSource(books); | |
StreamSource streamSource = | |
new StreamSource(new ByteArrayInputStream(xmlSource.toByteArray())); | |
generatePDF(streamSource); | |
} | |
private static Book getBook(String name, int year) { | |
Book book = new Book(); | |
book.setName(name); | |
book.setYear(year); | |
return book; | |
} | |
private static ByteArrayOutputStream getXMLSource(Books books) throws Exception { | |
JAXBContext context; | |
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); | |
try { | |
context = JAXBContext.newInstance(Books.class); | |
Marshaller marshaller = context.createMarshaller(); | |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); | |
marshaller.marshal(books, outStream); | |
} catch (JAXBException e) { | |
e.printStackTrace(); | |
} | |
return outStream; | |
} | |
private static void generatePDF(StreamSource streamSource) | |
throws FOPException, TransformerException, IOException { | |
File xsltFile = new File("template.xsl"); | |
// create an instance of fop factory | |
FopFactory fopFactory = FopFactory.newInstance(); | |
// a user agent is needed for transformation | |
FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); | |
// Setup output | |
OutputStream out = new java.io.FileOutputStream("book.pdf"); | |
try { | |
// Construct fop with desired output format | |
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); | |
// Setup XSLT | |
TransformerFactory factory = TransformerFactory.newInstance(); | |
Transformer transformer = factory.newTransformer(new StreamSource(xsltFile)); | |
// Resulting SAX events (the generated FO) must be piped through to FOP | |
Result res = new SAXResult(fop.getDefaultHandler()); | |
// Start XSLT transformation and FOP processing | |
// That's where the XML is first transformed to XSL-FO and then | |
// PDF is created | |
transformer.transform(streamSource, res); | |
} finally { | |
out.close(); | |
} | |
} | |
} |
Books.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.xml.bind.annotation.XmlElement; | |
import javax.xml.bind.annotation.XmlElementWrapper; | |
import javax.xml.bind.annotation.XmlRootElement; | |
import java.util.List; | |
@XmlRootElement(name="books") | |
public class Books { | |
public Books() { | |
} | |
private String author; | |
private List<Book> bookList; | |
@XmlElement(name = "authorName") | |
public String getAuthor() { | |
return author; | |
} | |
public void setAuthor(String author) { | |
this.author = author; | |
} | |
@XmlElement(name = "book") | |
public List<Book> getBookList() { | |
return bookList; | |
} | |
public void setBookList(List<Book> bookList) { | |
this.bookList = bookList; | |
} | |
} |
Book.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Book { | |
private String name; | |
private int year; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getYear() { | |
return year; | |
} | |
public void setYear(int year) { | |
this.year = year; | |
} | |
} |
template.xsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
xmlns:fo="http://www.w3.org/1999/XSL/Format" exclude-result-prefixes="fo"> | |
<xsl:template match="books"> | |
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> | |
<fo:layout-master-set> | |
<fo:simple-page-master master-name="A4" page-height="29.7cm" page-width="21cm" margin-top="2cm" | |
margin-bottom="2cm" margin-left="2cm" margin-right="2cm"> | |
<fo:region-body/> | |
</fo:simple-page-master> | |
</fo:layout-master-set> | |
<fo:page-sequence master-reference="A4"> | |
<fo:flow flow-name="xsl-region-body"> | |
<fo:block font-size="16pt" font-weight="bold" space-after="5mm">Author Name: | |
<xsl:value-of select="authorName"/> | |
</fo:block> | |
<fo:block font-size="10pt"> | |
<fo:table table-layout="fixed" width="100%" border-collapse="separate"> | |
<fo:table-column column-width="4cm"/> | |
<fo:table-column column-width="4cm"/> | |
<fo:table-body> | |
<xsl:apply-templates select="book"/> | |
</fo:table-body> | |
</fo:table> | |
</fo:block> | |
</fo:flow> | |
</fo:page-sequence> | |
</fo:root> | |
</xsl:template> | |
<xsl:template match="book"> | |
<fo:table-row> | |
<xsl:attribute name="font-weight">bold</xsl:attribute> | |
<fo:table-cell> | |
<fo:block> | |
<xsl:value-of select="name"/> | |
</fo:block> | |
</fo:table-cell> | |
<fo:table-cell> | |
<fo:block> | |
<xsl:value-of select="year"/> | |
</fo:block> | |
</fo:table-cell> | |
</fo:table-row> | |
</xsl:template> | |
</xsl:stylesheet> |
book.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<books> | |
<authorName>John Grisham</authorName> | |
<book> | |
<name>A Time To Kill</name> | |
<year>1989</year> | |
</book> | |
<book> | |
<name>The Film</name> | |
<year>1991</year> | |
</book> | |
<book> | |
<name>The Client</name> | |
<year>1993</year> | |
</book> | |
</books> |
Comments
Post a Comment