Creating PDF's With Custom Data Using Mule 4+ Java
While there's no way to generate a PDF in Mule Application, Mule allow you to write a Java code and make use of it in your API.
Join the DZone community and get the full member experience.
Join For FreeHello, readers!
We know that we can create PDFs and Images using Base64 Data, we can generate PDFs only if we have a valid Base64 content which includes, PDF format like Borders, Fonts, Style along with Data. Even if a small character is missed in Base64 content, you won't be able to generate a PDF.
In short, you should have a valid base64 to generate PDFs using Mule 4.
There's no option to generate a PDF by providing Custom Data as input in Mule Application.
But still, Mule helps you to allow you to write a Java code and make use of it in your API.
Requirements
- We need an external Jar: iTextPdf
- Custom Java Code
- Java connector to invoke Java class
Step One
Add below dependency into your pom before writing your Java code. So that you can import the classes while writing Java code
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.1</version>
</dependency>
Step Two
Create a Package under src/main/java and create a Class. Please note that you have to be familiar with Java code. Make sure your code is good enough to have the necessary Structure of PDF like borders, paragraphs, Font Style, etc.
If you are not familiar, no worries, am providing you the Git repo link where I have written a sample code that helps you to dig further and modify your code accordingly.
Step Three
Now use Java connector (Invoke Static ) and provide your class name and method name with appropriate arguments to be passed fo the method you have written.
Note: Methods and arguments are displayed automatically if your Java class is under src/main/java
Step Four
You can pass inputs from POST body like name, role and transform your message accordingly and pass them as arguments to your Java method so that you can create a Custome PDF file.
If you can see the PDF which is generated, I have used Name and role in it.
That's pretty simple, isn’t it?
Why don’t you try out!
Find the code here: https://github.com/sravanl/generatePdfs
Note: Please change your FILE path that is mentioned in Java code to your local path
As said, you can modify according to your need!
Opinions expressed by DZone contributors are their own.
Comments