Generating Error Document As Pdf From Standard Error HTML Template In Mule

Author: Payal Jain

This blog describes how to style our PDF documents with images(logos), CSS, and other properties and features used in HTML.

In most Mule projects, we handle errors using standard parameters like errorSubject, errorObject, errorMessage, errorDescription, errorDetailedDescription. We can design a basic HTML template that consists of these fields and can be reused across applications.

  • In the Mule project, under /src/main/resources, create templates and a file under the same as errorTemplate.html-
<!DOCTYPE html>
<p><span><img src="${imgSrc}" width="550" height="100"/></span></p>
<br> <br></br></br>
  <br> <br></br></br>
<b>Error Message</b>
  <br> <br></br></br>
<b>Error Description</b>
  <br> <br></br></br>
<b>Error Detailed Description</b>
  <br> <br></br></br>
  <p style="color:grey; font-size: 16px; font-weight:bold"><span>CONFIDENTIALITY NOTICE</span></p>
  <p style="color:grey; font-size: 10px">
  <br> <br></br></br>
  <p style="color:grey; font-size: 10px">

Open the above errorTemplate.html file with the browser and it should render the default HTML. From the Mule flow, the values and the image shall be replaced in $ values and the font shall suit the needed PDF doc. 

  • This is a basic HTML code with basic font styles with content in body tags, <p> tags for paragraphs,<br> for line breaks. The values with $ are to be replaced with data from Mule flow.
  • Add Java module from Exchange to the Mule palette.
  • In pom.xml we need to add the required dependencies. Here, we shall use Apache FreeMarker and OpenHtmlToPdf dependency.
  • Apache FreeMarker- It is a template engine: a Java library to generate text output based on templates and changing data. In the template, we are focusing on how to present the data, and outside the template, we are focusing on what data to present.
  • OpenHtmlToPdf- It is a pure-Java library for rendering a reasonable subset of well-formed XML/XHTML using CSS for layout and formatting, outputting to PDF.
  • Under /src/main/java, create a package and add 3 java classes-,, and
  • In Used to encapsulate data, we add setters and getters for data values to be replaced in our HTML code.
  • In It renders the HTML.
  • In It contains the static methods invoked by the flows that generate the PDFs from the HTML.

The flow build-up of the code can be summarized in the following steps:

  1. The PDF is rendered with the help of the PdfRendererBuilder from the OpenHtmlToPdf API.
  2.  After instantiating it, we register the HTML content with the method withHtmlContent. 
  3. Further, we register a stream where to put the PDF data with the toStream method.
  4. We create the PDF by calling the run method. 
  5. We get the bytes of the PDF by calling toByteArray. This byte array we can then finally write it into a file or upload it to some storage.
  • Create the Mule flow with the HTTP listener as the endpoint that builds the data object with the template values and the image to be passed in the Pdf. 
  • In HTTP Listener, configure path with /errorDoc.pdf
  • In Transform Message, pass the error data as needed.
  • Use the new operation from the Java module to create an object on which a method is later invoked. This provides an instanced object that can then call one of its methods.
  • For logos in pdf, we need to pass Image as Base64 value in the SetVariable component:
  • Set the invoke operation to call one of the object’s methods:

Run the application in the browser to view the pdf as a downloaded pdf file:

For a use case, when we require our endpoint for testing our application like http://localhost:8081/errorDoc.pdf to be different from the PDF file- like errorDoc.pdf-

We can configure Content-Disposition in HTTP header in the HTTP Listener:

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.