Creating a PDF with custom data in MuleSoft

  • November 23, 2020

To generate PDFs in Mule 4, you need valid Base64 content (including styles, fonts and images). This may lead to errors, but Mule allows Java code. You can use iTextpdf and Apache POI JARs. This tech article focuses on Apache POI.

Custom data in MuleSoft1

Let’s create a .docx file using Apache POI and use LibreOffice to convert it to PDF.

1.Add the required dependencies in pom.xml; adding these maven dependencies enables the Java code to recognize imported libraries

 <dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>4.1.0</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>4.1.0</version>

</dependency>

<dependency>

   <groupId>org.apache.commons</groupId>

   <artifactId>commons-collections4</artifactId>

   <version>4.4</version>

</dependency>

<dependency>

    <groupId>org.apache.commons</groupId>

    <artifactId>commons-compress</artifactId>

    <version>1.18</version>

</dependency>

<dependency>

    <groupId>org.apache.poi</groupId>

    <artifactId>poi-ooxml-schemas</artifactId>

    <version>4.1.0</version>

</dependency>

2.Create a package under src/main/java and create a class with Java code to provide the necessary structure to PDF — here’s some sample code:

package com.blogdemo;

import java.io.File;

import java.io.FileOutputStream;

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

paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);

//Set left border to paragraph

paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES);

//Set right border to paragraph

paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES);

//Set top border to paragraph

paragraph.setBorderTop(Borders.BASIC_BLACK_DASHES);

paragraph.setAlignment(ParagraphAlignment.LEFT);

XWPFRun run = paragraph.createRun();

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

document.write(out);

out.close();

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

}

}

3.Use Java Connector (Invoke Static), which will call a static Java method — ApplyingBorderMethod, in this case; the return value from the called method will be placed in the output message

Custom data in MuleSoft2

4.Provide a class name and method name with appropriate arguments to be passed for your method

Custom data in MuleSoft3

We’ve created a .docx file at the location defined in the Java method. Now, we’ll convert it to .pdf. To do this, call a LibreOffice command for conversion from a Windows batch file. We’ll call this batch file from Groovy script.

Let’s continue.

5.Set the filepath and command in variable.

Filepath represents the base path for the document to be converted to PDF and command is the path that will be passed in Groovy script to call the batch file to convert the .docx file to .pdf.

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

Custom data in MuleSoft4

6.Create a Windows batch file that has the commands for LibreOffice to convert the .docx file to .pdf and a path to store the generated .pdf file. This example uses “convertdoctopdf.bat” and there are two 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 to .pdf. (For more information: https://www.systutorials.com/docs/linux/man/1-soffice/)
Custom data in MuleSoft5

7.Add a Groovy script that will call the batch file

8.Add a scripting module (Execute) from Mule Palette after the Transform Message

The Scripting module executes custom logic written in a scripting language.

9.Configure the required libraries to use the Groovy engine

Custom data in MuleSoft6

10.Add the Groovy engine and the Groovy script in the code section

Custom data in MuleSoft7

This example used the following script:

@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);

11.Run the application and you’ll get two files in your configured file location: sampleDocument.docx and sampleDocument.pdf

Custom data in MuleSoft8

NOTE: Make sure to change the filepath based on your needs. If you don’t create a mechanism to change the .docx filename every time the application executes, the application will fail after executing once.

— By Tanya Yadav