Using the power of Apache FOP in a .NET world using Azure Functions
--
Every developer knows the struggle of generating PDF files. Even though in the .NET world there are some pretty good packages to do this, it basically comes down to coding the PDF line by line. Since this is a time consuming and monotonous task, producing a lot of low value code we decided to do this differently
A tool like Altova Stylevision allow you to visually design a document and The Apache FOP Project takes the XML containing the data and an XSLT file containing the ‘design’ . That sounds a lot friendlier, but how to integrate this JAVA component into a .NET world? We used Azure Functions.
The problem
Apache FOP is a JAVA component and that doesn’t really play nice with .NET code. There is no nuget package to use Apache FOP inside your .NET solution.
In our first attempt we actually used a VM and ran the Apache FOP executable in isolation with a command like
using (var process = new Process())
{
// A lot of code to invoke a system process running the .JAR file
// and read back the generated .PDF file
}
It works, but then we actually had to maintain an actual VM (with all the stuff like patching etc.) plus it’s pretty expensive to keep a VM running to only run a ‘small’ JAVA component.
The serverless approach
Since we’re .NET developers we know our way around Azure and we knew Azure Functions had JAVA support in preview. Azure Functions allow you to run some code in the cloud without having to worry about the underlying infrastructure. It’s called serverless, but it’s more like some-random-server. The server gets instantly provisioned by Azure when your function (which is a piece of code) is triggered by an event (a HTTP GET call for instance). Less worrying about infrastructure and focus more on adding value with code!
My most recent JAVA coding experience was over 10 years ago so I wondered if we were able to pull it off in a descent timely matter. I had never used Maven nor had I an IDE for JAVA (we ended up just using Notepad++ ). Luckily I already had some experience with Azure Functions and JAVA looks a lot like C# 😉.
What we installed..
- Java SDK
- Maven
- Azure CLI
What we did…
- Used the excellent tutorial on creating your first Azure Function in JAVA using Maven. This Azure Function is triggered by an HTTP call.
- Read the body of the POST request to this function as a POJO (Plain Old JAVA Object) so we end up with a simple REST service taking the XSLT and the XML as JSON properties and giving the raw PDF bytes as a response.
- Since we’re in a JAVA world there is an Apache FOP JAVA package available in Maven. We added the Apache FOP package dependency into the
pom.xml
that was generated by the Maven Azure Function example. - Invoke Apache FOP through the package (good code examples can be found here) and return the resulting PDF bytes into the response using:
java.io.ByteArrayOutputStream
After a few hours of trial and error we had our generic Apache FOP REST service! I’m sure there are many optimizations to come but we were already happy with the result!