javamulesoftmule4

Merge multiple PDF files using Java and Mulesoft


The Challenge: Merging PDFs As integration Medium, our tasks often involve consolidating information scattered across multiple documents. Merging PDF files is a common requirement that can be elegantly handled within MuleSoft.

Merge multiple PDF files using Java and Mule 4. We will leverage Apache PDFBox for the PDF merging functionality and integrate this with a Mule 4 flow to expose an HTTP endpoint that accepts multiple PDF files and returns a merged PDF.


Solution

  • Implementation with MuleSoft:

    1. Add Apache PDFBox Dependencies:

    A Java library for working with PDF documents. Open the pom.xml file of your Mule project and add the following dependencies to include Apache PDFBox.

    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>2.0.20</version>
    </dependency>
    

    2. Creating the Java Class for Merging PDFs:

    Implement the mergePDFs method in the PDFMerger class using PDFBox utilities. This method accepts a list of PDF byte arrays, merges them, and returns the combined PDF as a byte array.

    package com.mule.api;
    
    import org.apache.pdfbox.multipdf.PDFMergerUtility;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.util.List;
    
    public class MergePdfFile {
        public static byte[] mergePdfs(List<byte[]> pdfs)throws IOException {       
            PDFMergerUtility pdfMerger = new PDFMergerUtility();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            for (byte[] pdf : pdfs) {
                pdfMerger.addSource(new ByteArrayInputStream(pdf));           
            }
            pdfMerger.setDestinationStream(outputStream);
            pdfMerger.mergeDocuments(null);
            return outputStream.toByteArray();
        }
    }
    

    3. Configuring the Mule 4 Flow:

    Use a Java component to call the mergePDFs method from the PDFMerger class, passing the extracted PDF files to it. Set the payload to the merged PDF and configure the response to return it with the correct MIME type (application/pdf).

    <flow name="post:\v1\merge-pdf:multipart\form-data:experience-merge-pdf-api-config">       
        <java:invoke-static method="mergePdfs(java.util.List)" doc:name="Invoke static mergePdfs" doc:id="fd7f982b-265f-468c-ab3e-7034472626d5" class="com.mule.api.MergePdfFile" target="mergedPdfFile">
            <java:args><![CDATA[#[{
        pdfs: payload.parts..content
    }]]]></java:args>
        </java:invoke-static>
        <set-payload value="#[vars.mergedPdfFile]" doc:name="Set mergedPdfFile" doc:id="d4451400-47c2-4ca4-8dec-d06dd2f37432" mimeType="application/pdf" /> 
    </flow>
    

    Testing Document API’s:

    To test the Merge PDF API, we'll send a sample PDF files (sampleFile1.pdf and sampleFile2.pdf) to the Merge PDF API. Upon successful execution, the file should be merged into a single pdf file. enter image description here

    I hope this article helps.

    Cheers Mulesoft!!