How to merge pdf in spring boot Java

How to merge pdf in spring boot Java

PDF files in a Spring Boot application, you can use a library like Apache PDFBox or iText. Below is an example using Apache PDFBox to merge PDFs in a Spring Boot application.

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.27</version>  <!-- Check for the latest version -->
</dependency>

Create a Service to Merge PDFs: Create a service class that uses PDFBox to merge PDF files.

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

@Service
public class PdfMergeService {

    public void mergePdfs(File[] inputPdfs, File outputPdf) throws IOException {
        // Create PDFMergerUtility object
        PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
        
        // Add all the input PDF files to the merger
        for (File pdf : inputPdfs) {
            pdfMergerUtility.addSource(pdf);
        }
        
        // Set the output PDF file
        pdfMergerUtility.setDestinationFileName(outputPdf.getAbsolutePath());
        
        // Merge the PDFs
        pdfMergerUtility.mergeDocuments(null);
    }
}

Create a Controller: Expose an endpoint to handle the merging of PDF files.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/pdf")
public class PdfController {

    @Autowired
    private PdfMergeService pdfMergeService;

    @PostMapping("/merge")
    public String mergePdfs(@RequestParam("files") MultipartFile[] files) {
        try {
            // Convert uploaded files to File objects
            File[] inputPdfs = new File[files.length];
            for (int i = 0; i < files.length; i++) {
                inputPdfs[i] = new File(System.getProperty("user.dir") + "/uploaded_pdf_" + i + ".pdf");
                files[i].transferTo(inputPdfs[i]);
            }

            // Output PDF file
            File outputPdf = new File(System.getProperty("user.dir") + "/merged_output.pdf");

            // Merge the PDFs
            pdfMergeService.mergePdfs(inputPdfs, outputPdf);
            
            // Return success message with the location of the merged PDF
            return "PDFs merged successfully. Output file: " + outputPdf.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
            return "Failed to merge PDFs: " + e.getMessage();
        }
    }
}
  • POST: http://localhost:8080/pdf/merge
  • Form Data: files (add multiple PDF files)
    {
      "message": "PDFs merged successfully. Output file: /path/to/merged_output.pdf"
    }
    

Leave a Reply

Your email address will not be published. Required fields are marked *