Merge A number of PDFs in MuleSoft
Suppose there’s a situation the place you need to merge a number of PDFs into one PDF and ship the merged PDF as a response again to the supply system or retailer the merged PDF in a file location. On this article, you’ll learn to do that. There isn’t a such connector in MuleSoft that you should use to merge the PDFs. You’ll have to use the Java library org.apache.pdfbox.multipdf to merge the PDFs. It incorporates all obligatory Java libraries for merging a number of PDFs into one.
Implementation
Step 1
Add the dependency as proven under in pom.xml:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<model>2.0.1</model>
</dependency>
Step 2
Create a Java class MergeMultiplePDF
below src/principal/java and create two static strategies mergeAndStorePDF
and mergePDFs
:
package deal com.mulesoft.mergePDF;
import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
public class MergeMultiplePDF {
/*
Merge PDF and retailer it in an area listing
listPdfFileNames= ["src/main/resources/input/PDF1.pdf", "src/main/resources/input/PDF2.pdf"]
mergedPdfFileName= "src/principal/assets/output/mergedPDF.pdf"
*/
public static void mergeAndStorePDF(String[] listPdfFileNames, String mergedPdfFileName) throws IOException {
/* Create and initialize object of PDFMergerUtility */
PDFMergerUtility obj = new PDFMergerUtility();
/* Set Vacation spot File within the PDFMergerUtility Object */
obj.setDestinationFileName(mergedPdfFileName);
/* Iterate via the record of PDF filenames */
for (String file : listPdfFileNames) {
/* Add Every PDF information within the PDFMergerUtility Object */
obj.addSource(new File(file));
}
/* Now the PDFMergerUtility Object has all of the PDFs. Use mergeDocuments technique to merge the PDFs */
obj.mergeDocuments(null);
/* This may retailer the merged PDF within the vacation spot path handed as argument */
}
/*
Merge PDFs and return the merged PDF as a byte array
base64PDF= Record of base64 encoded PDF strings
Eg. ["PDF1 base64 encoded string", "PDF2 base64 encoded string"]
*/
public static byte[] mergePDFs(String[] base64PDF) throws Exception {
/* Create and initialize ByteArrayOutputStream to return the merged PDF as a byte array */
strive (ByteArrayOutputStream vacation spot = new ByteArrayOutputStream()) {
/* Create and initialize object of PDFMergerUtility */
PDFMergerUtility obj = new PDFMergerUtility();
/* Set Vacation spot Stream because the ByteArrayOutputStream object within the PDFMergerUtility Object */
obj.setDestinationStream(vacation spot);
/* Iterate via the record of Base64 encoded PDF strings */
for (String pdf : base64PDF) {
/* Initialize ByteArrayInputStream object and retailer every PDF as bytes */
ByteArrayInputStream bais = new ByteArrayInputStream(pdf.getBytes());
/* Add every base64 decoded PDF within the PDFMergerUtility Object */
obj.addSource(Base64.getDecoder().wrap(bais));
}
/* Now the PDFMergerUtility Object has all of the PDFs. Use mergeDocuments technique to merge the PDFs */
obj.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
/* Return the mergedPDF as a byte array utilizing the ByteArrayOutputStream object */
return vacation spot.toByteArray();
} catch (IOException e) {
throw new Exception("Error occurred whereas merging pdf information", e);
}
}
}
mergeAndStorePDF
– This static technique accepts two arguments (listPdfFileNames
andmergedPdfFileName
) and has no return sort. Learn the feedback within the Java class to know every step.mergePDFs
– This static technique accepts one argument (base64PDF
) and has the return sort as a byte array. This byte array is definitely the merged PDF file in byte array format. Learn the feedback within the Java class to know every step.
Step 3
Drag and drop an SFTP Record Connector and add the SFTP credentials within the SFTP config. Add the Listing Path and Filename Sample as {*.pdf}
. This may retrieve all of the PDF information from the trail talked about within the property file.
Step 4
Subsequent, initialize a variable base64PDF
as a clean array []
. This variable will include all base64 encoded PDF strings. Add a selection element after that to examine if the payload is empty (i.e., the SFTP Record connector picked up any information or not). If no information are retrieved then simply log a message No information discovered. In any other case, use a For-Every
element to iterate via all of the PDF information retrieved.
Step 5
Subsequent, learn every PDF as proven above, encode it, and retailer it as a base64 encoded string within the variable base64PDF
. Encode the PDF utilizing toBase64()
technique of dw::core::Binaries library
as proven under.
Step 6
Subsequent, name the static technique mergePDFs
of sophistication MergeMultiplePDF
. Cross the record of base64 encoded PDF strings (saved within the variable base64PDF
) as an argument to the tactic mergePDFs
. The mergePDFs
technique will return the concatenated PDF in byte array format. Encode that byte array to base64 encoded string and retailer it in a variable.
Now, assemble the response payload of MimeType multipart/form-data
. Decode the base64 encoded merged PDF and go it within the content material subject. Set the Content material-Kind
as software/pdf
as proven above.
Step 7
Set the MimeType as software/pdf. This may ship the merged PDF as a pdf as a response. You can too retailer this payload in an SFTP output listing.
Enter PDFs
PDF1
PDF2
Output PDF
Merged PDF
The merged PDF can have a complete of all of the pages that every PDF has.
Conclusion
That is how one can merge a number of PDF information into one PDF and get the merged PDF as a response or retailer the merged PDF in a file location.
Thanks for studying the article and in case you have any questions please be at liberty to jot down it down within the feedback part.