2023-02-03 21:26:35 +01:00
|
|
|
package stirling.software.SPDF.controller;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
|
import org.apache.pdfbox.pdmodel.PDPage;
|
|
|
|
import org.apache.pdfbox.pdmodel.PDPageTree;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.core.io.InputStreamResource;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.ui.Model;
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
2023-03-28 00:24:48 +02:00
|
|
|
import stirling.software.SPDF.utils.PdfUtils;
|
|
|
|
|
2023-02-03 21:26:35 +01:00
|
|
|
@Controller
|
|
|
|
public class MergeController {
|
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(MergeController.class);
|
2023-02-03 21:26:35 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
@GetMapping("/merge-pdfs")
|
|
|
|
public String hello(Model model) {
|
|
|
|
model.addAttribute("currentPage", "merge-pdfs");
|
|
|
|
return "merge-pdfs";
|
|
|
|
}
|
2023-02-03 21:26:35 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
@PostMapping("/merge-pdfs")
|
2023-03-28 00:24:48 +02:00
|
|
|
public ResponseEntity<byte[]> mergePdfs(@RequestParam("fileInput") MultipartFile[] files) throws IOException {
|
2023-02-11 15:27:15 +01:00
|
|
|
// Read the input PDF files into PDDocument objects
|
|
|
|
List<PDDocument> documents = new ArrayList<>();
|
2023-02-03 21:26:35 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
// Loop through the files array and read each file into a PDDocument
|
|
|
|
for (MultipartFile file : files) {
|
|
|
|
documents.add(PDDocument.load(file.getInputStream()));
|
|
|
|
}
|
2023-02-03 21:26:35 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
PDDocument mergedDoc = mergeDocuments(documents);
|
2023-02-03 21:26:35 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
// Return the merged PDF as a response
|
2023-03-28 00:24:48 +02:00
|
|
|
return PdfUtils.pdfDocToWebResponse(mergedDoc, files[0].getOriginalFilename().replaceFirst("[.][^.]+$", "")+ "_merged.pdf");
|
2023-02-11 15:27:15 +01:00
|
|
|
}
|
2023-02-03 21:26:35 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
private PDDocument mergeDocuments(List<PDDocument> documents) throws IOException {
|
|
|
|
// Create a new empty document
|
|
|
|
PDDocument mergedDoc = new PDDocument();
|
2023-02-03 21:26:35 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
// Iterate over the list of documents and add their pages to the merged document
|
|
|
|
for (PDDocument doc : documents) {
|
|
|
|
// Get all pages from the current document
|
|
|
|
PDPageTree pages = doc.getPages();
|
|
|
|
// Iterate over the pages and add them to the merged document
|
|
|
|
for (PDPage page : pages) {
|
|
|
|
mergedDoc.addPage(page);
|
|
|
|
}
|
|
|
|
}
|
2023-02-03 21:26:35 +01:00
|
|
|
|
2023-02-11 15:27:15 +01:00
|
|
|
// Return the merged document
|
|
|
|
return mergedDoc;
|
|
|
|
}
|
2023-02-03 21:26:35 +01:00
|
|
|
|
|
|
|
}
|