Generating custom Pdf in Mulesoft using Java code

Gursimran Singh Ranhotra
Another Integration Blog
3 min readSep 12, 2022

We саn сreаte РDFs аnd Imаges using Bаse64 Dаtа. We саn generаte РDFs оnly if we hаve а vаlid Bаse64 соntent including format like Bоrders, Fоnts, Style аlоng with its Dаtа. Fоr it, we shоuld hаve а vаlid Bаse64 tо generаte РDFs using Mule 4.

We can make use of Java code to generate pdf in MuleSoft using custom data. We can also create our own custom connector to do so.

In this tutorial, we will be focusing on the way to create a custom pdf using Java code. There are a few jars in Java that we can use to implement it. Some of them which can be used to achieve this purpose are iTextpdf and Apache POI.

In this article, I will focus on creating pdf files using iText Jar. You must be familiar with the fundamentals of Java coding.

1. Create a new package and inside it create a new java class in src/main/java in your project-

2. Please include the following Java code in created file-

package com.pdf;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class jsontoPdf{
public static void jsontopdf(String filename,String content) {
Document document = new Document();
try
{
File file = new File(filename);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
document.add(new Paragraph(content));
document.close();
writer.close();
} catch (DocumentException e)
{
e.printStackTrace();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}

Note-

The package name we are using here is- com.pdf
Java ClassName- jsontoPdf
Java method name- jsontopdf

We can modify the className, methodName, and code according to our needs.

The public method used here is-

public static void jsontopdf (String fileName, String content)

This method accepts two parameters-

1. The fileName (The name of pdf that we want to create)
2. The content of pdf.

3. Add the dependency in pom.xml

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.2</version>
</dependency>

(This is the dependency, I am using, however, you can add any version according to your needs from the Maven repository.)

4. In the dataweave, you just need to write code to invoke the Java method and a pdf would be generated.

%dw 2.0
import java!com::pdf::jsontoPdf
output application/java
— -
{
pdf :jsontoPdf::jsontopdf(“C:\Users\Desktop\\test.pdf”,payload)
}

Note-

The package used- com.pdf
Java ClassName- jsontoPdf
Java method name- jsontopdf.
FileName: test.pdf

Syntax used-

className::methodName(“Path\\fileName”,content)

Thus, you can modify your code accordingly.

5. Create your code in the studio, deploy, and test it.

As we can see, a pdf with the same name and payload is created.

Thanks for reading!
Please feel free to comment and follow me for more helpful articles and insights in the future.

--

--