Creating PDF with Custom Data in MuleSoft

Author: Tanya Yadav

To generate PDFs in Mule 4, we need to have valid Base64 content (including Styles, Fonts, Images, etc.). This may lead to a lot of errors but since Mule allows us to use java code, let’s use it to generate pdf and make use of it in MuleSoft. 

Jars that can be used to achieve this purpose are iTextpdf and Apache POI. Here, we will be focusing on Apache POI.

We will create a .docx file using Apache POI and use LibreOffice to convert it to PDF. Let’s see the steps to achieve our objective:


1. Add required dependencies in pom.xml. By adding these maven dependencies, java code will be able to recognize imported libraries. 


























2. Create a package under src/main/java and create a class. In this class, we will have java code to provide necessary structure to PDF. My sample code:

package com.blogdemo;



import org.apache.poi.xwpf.usermodel.Borders;

import org.apache.poi.xwpf.usermodel.XWPFDocument;

import org.apache.poi.xwpf.usermodel.XWPFParagraph;

import org.apache.poi.xwpf.usermodel.XWPFRun;

public class ApplyingBorder extends XWPFDocument{

public static void ApplyingBorderMethod() throws Exception {

//Blank Document

XWPFDocument document = new XWPFDocument();

//Write the Document in file system

FileOutputStream out = new FileOutputStream(new File(“D:\\op\\sampleDocument.docx”));

//create paragraph

XWPFParagraph paragraph = document.createParagraph();

//Set bottom border to paragraph


//Set left border to paragraph


//Set right border to paragraph


//Set top border to paragraph



XWPFRun run = paragraph.createRun();

run.setText(“Hi, This is the document created to demonstrate how to create pdf using Mulesoft”);



System.out.println(“sampleDocument.docx written successully”);



3. Now, use Java Connector (Invoke Static) which will call static Java method (ApplyingBorderMethod() is our case), and the return value from the called method will be placed in the output message

4. And provide class name and method name with appropriate arguments to be passed for your method.

We have created a .docx file at the location defined in the java method. Now, we will convert it to .pdf. To achieve this, we can call LibreOffice command for conversion from Windows batch file. And I’m going to call this batch file from Groovy script. Let’s see the next steps:

5. In this step, we will set filepath and command in variable. Filepath represents the base path for the document which is to be converted to pdf and command is the one which will be passed in groovy script to call batch file to convert .docx file to .pdf

In my case, “sampleDocument” is the name of docx file that will be created by java code in application and basepath is “D:/”

6. Next step is to create a Windows batch file which has the commands for LibreOffice to convert docx file to pdf and a path to store the generated .pdf file. I named this file as “convertdoctopdf.bat”. There are 2 commands in this file, 

  1. The first command is to change the working directory to where soffice is installed
  2. The second command is the soffice command to convert from Docx file to .pdf. For more info, refer to

7. It’s time to add a groovy script which will call our batch file. Add Scripting module (Execute) from Mule Palette after transform message. Scripting module executes custom logic written in a scripting language

8. configure the required libraries to use Groovy engine.

9. Add Engine: Groovy and add groovy script in code section.

Script that I used for the purpose:

@GrabConfig(systemClassLoader = true)

String x= vars.command

String batfile = “\\convertdoctopdf.bat”

String filepath = vars.filepath

println(“HERE IS THE VALUE OF X ” + vars.command);

def proc = (“cmd /c ” +filepath+batfile + ” ” +x).execute();

proc.waitForProcessOutput(System.out, System.err);

9. BAM! Run your application and you will get two files in your configured file location: sampleDocument.docx and sampleDocument.pdf.

Note: Make sure to change filepath according to your need. If you don’t create a mechanism to change the filename of .docx file every time the application executes, the application will fail after executing once.

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.