How to create XSL-FO (.fo) file in Spring Boot and convert it to PDF (Part 1)
In this article, I will show you how to create an XSL-FO (.fo) file (in Spring boot) and how to convert it to a PDF
Overview
An XSL-FO file can be written using .xsl and .fo extensions. Today, we will create an XSL-FO file using the .fo extension. This file is used to convert data written with specific template rules into a PDF.
Let’s start doing that now…
Create .fo file
Under the “resources” folder, create a new file by selecting “New” and naming it with the “.fo” extension.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<!-- Layout master set definition -->
<fo:layout-master-set>
<fo:simple-page-master master-name="simple">
<fo:region-body margin="1in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<!-- Page sequence using the simple master layout -->
<fo:page-sequence master-reference="simple">
<fo:flow flow-name="xsl-region-body">
<!-- Block containing inline styled text -->
<fo:block>
<fo:inline font-weight="bold">Text</fo:inline>
<fo:inline font-style="italic">Text2</fo:inline>
</fo:block>
<!-- Table with two columns and one row -->
<fo:table border="0.5pt solid black" table-layout="fixed" width="100%">
<fo:table-column column-width="50%"/>
<fo:table-column column-width="50%"/>
<fo:table-body>
<fo:table-row>
<fo:table-cell border="0.5pt solid black" padding="5pt">
<fo:block>Cell1</fo:block>
</fo:table-cell>
<fo:table-cell border="0.5pt solid black" padding="5pt">
<fo:block>Cell2</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</fo:root>
Note!
Here, the file extension (.fo or .xsl) doesn’t make a difference in Spring Boot usage. I’ve tested this — you can use either. The real difference lies in their content, which you’ll see in the next part. XSL-FO files are divided into 2 different content.
- Full XSL-FO Document
- XSLT-based Transformation and Styling
Don’t worry, I will talk about these in upcoming sections. Let’s continue with this for now.
How to convert a .fo file to PDF.
First, we need a library for the conversion process (Apache FOP).
Ok. Good question. What is Apache FOP?
Apache FOP
Apache FOP (Formatting Objects Processor) is an open-source Java library for generating PDF and other output formats from XML documents. FOP processes XSL-FO (Extensible Stylesheet Language Formatting Objects) documents and converts them into different formats such as PDF, PostScript, PCL, AFP, SVG, XML (Areas Tree representation), and Print.
for gradle
implementation 'org.apache.xmlgraphics:fop:2.8'
for maven
<dependency>
<groupId>org.apache.xmlgraphics</groupId>
<artifactId>fop</artifactId>
<version>2.8</version>
</dependency>
You can use Apache FOP to convert XSL-FO to PDF. The following code snippet converts the previously generated .fo file to PDF.
import lombok.SneakyThrows;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.FileOutputStream;
public class FoFileToPdf {
@SneakyThrows
public static void main(String[] args) {
File xsltFile = new File("src/main/resources/files/doc.fo");
String desktopPath = System.getProperty("user.home") + File.separator + "Desktop";
File desktopDir = new File(desktopPath);
if (!desktopDir.exists()) {
desktopDir.mkdirs();
}
File pdfFile = new File(desktopDir, "test.pdf");
FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, new FileOutputStream(pdfFile));
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new StreamSource(xsltFile), new SAXResult(fop.getDefaultHandler()));
}
}
And that’s it, now you have converted your .fo file to PDF. You can write any content you want inside the .fo file.
Part 2 — PDF generate as dynamically