mirror of
				https://github.com/Frooodle/Stirling-PDF.git
				synced 2025-11-01 01:21:18 +01:00 
			
		
		
		
	Merge branch 'main' into main
This commit is contained in:
		
						commit
						8938e86223
					
				@ -1,15 +1,9 @@
 | 
				
			|||||||
package stirling.software.SPDF.controller.api;
 | 
					package stirling.software.SPDF.controller.api;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.IOException;
 | 
					import io.swagger.v3.oas.annotations.Operation;
 | 
				
			||||||
import java.io.InputStream;
 | 
					import io.swagger.v3.oas.annotations.tags.Tag;
 | 
				
			||||||
import java.nio.file.Files;
 | 
					import org.apache.pdfbox.io.MemoryUsageSetting;
 | 
				
			||||||
import java.nio.file.Paths;
 | 
					import org.apache.pdfbox.multipdf.PDFMergerUtility;
 | 
				
			||||||
import java.nio.file.attribute.BasicFileAttributes;
 | 
					 | 
				
			||||||
import java.util.ArrayList;
 | 
					 | 
				
			||||||
import java.util.Arrays;
 | 
					 | 
				
			||||||
import java.util.Comparator;
 | 
					 | 
				
			||||||
import java.util.List;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
import org.apache.pdfbox.pdmodel.PDDocument;
 | 
					import org.apache.pdfbox.pdmodel.PDDocument;
 | 
				
			||||||
import org.apache.pdfbox.pdmodel.PDPage;
 | 
					import org.apache.pdfbox.pdmodel.PDPage;
 | 
				
			||||||
import org.slf4j.Logger;
 | 
					import org.slf4j.Logger;
 | 
				
			||||||
@ -20,12 +14,19 @@ import org.springframework.web.bind.annotation.PostMapping;
 | 
				
			|||||||
import org.springframework.web.bind.annotation.RequestMapping;
 | 
					import org.springframework.web.bind.annotation.RequestMapping;
 | 
				
			||||||
import org.springframework.web.bind.annotation.RestController;
 | 
					import org.springframework.web.bind.annotation.RestController;
 | 
				
			||||||
import org.springframework.web.multipart.MultipartFile;
 | 
					import org.springframework.web.multipart.MultipartFile;
 | 
				
			||||||
 | 
					 | 
				
			||||||
import io.swagger.v3.oas.annotations.Operation;
 | 
					 | 
				
			||||||
import io.swagger.v3.oas.annotations.tags.Tag;
 | 
					 | 
				
			||||||
import stirling.software.SPDF.model.api.general.MergePdfsRequest;
 | 
					import stirling.software.SPDF.model.api.general.MergePdfsRequest;
 | 
				
			||||||
import stirling.software.SPDF.utils.WebResponseUtils;
 | 
					import stirling.software.SPDF.utils.WebResponseUtils;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.io.ByteArrayInputStream;
 | 
				
			||||||
 | 
					import java.io.ByteArrayOutputStream;
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.nio.file.Files;
 | 
				
			||||||
 | 
					import java.nio.file.Paths;
 | 
				
			||||||
 | 
					import java.nio.file.attribute.BasicFileAttributes;
 | 
				
			||||||
 | 
					import java.util.Arrays;
 | 
				
			||||||
 | 
					import java.util.Comparator;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@RequestMapping("/api/v1/general")
 | 
					@RequestMapping("/api/v1/general")
 | 
				
			||||||
@Tag(name = "General", description = "General APIs")
 | 
					@Tag(name = "General", description = "General APIs")
 | 
				
			||||||
@ -34,7 +35,7 @@ public class MergeController {
 | 
				
			|||||||
    private static final Logger logger = LoggerFactory.getLogger(MergeController.class);
 | 
					    private static final Logger logger = LoggerFactory.getLogger(MergeController.class);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private PDDocument mergeDocuments(List<PDDocument> documents) throws IOException {
 | 
					    private PDDocument mergeDocuments(List<PDDocument> documents) throws IOException {
 | 
				
			||||||
        PDDocument mergedDoc = new PDDocument();
 | 
					        PDDocument mergedDoc = new PDDocument();
 | 
				
			||||||
        for (PDDocument doc : documents) {
 | 
					        for (PDDocument doc : documents) {
 | 
				
			||||||
            for (PDPage page : doc.getPages()) {
 | 
					            for (PDPage page : doc.getPages()) {
 | 
				
			||||||
@ -42,9 +43,9 @@ private PDDocument mergeDocuments(List<PDDocument> documents) throws IOException
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return mergedDoc;
 | 
					        return mergedDoc;
 | 
				
			||||||
}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private Comparator<MultipartFile> getSortComparator(String sortType) {
 | 
					    private Comparator<MultipartFile> getSortComparator(String sortType) {
 | 
				
			||||||
        switch (sortType) {
 | 
					        switch (sortType) {
 | 
				
			||||||
            case "byFileName":
 | 
					            case "byFileName":
 | 
				
			||||||
                return Comparator.comparing(MultipartFile::getOriginalFilename);
 | 
					                return Comparator.comparing(MultipartFile::getOriginalFilename);
 | 
				
			||||||
@ -83,33 +84,31 @@ private Comparator<MultipartFile> getSortComparator(String sortType) {
 | 
				
			|||||||
            default:
 | 
					            default:
 | 
				
			||||||
                return (file1, file2) -> 0;  // Default is the order provided
 | 
					                return (file1, file2) -> 0;  // Default is the order provided
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
}
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@PostMapping(consumes = "multipart/form-data", value = "/merge-pdfs")
 | 
					    @PostMapping(consumes = "multipart/form-data", value = "/merge-pdfs")
 | 
				
			||||||
@Operation(summary = "Merge multiple PDF files into one",
 | 
					    @Operation(summary = "Merge multiple PDF files into one",
 | 
				
			||||||
            description = "This endpoint merges multiple PDF files into a single PDF file. The merged file will contain all pages from the input files in the order they were provided. Input:PDF Output:PDF Type:MISO")
 | 
					            description = "This endpoint merges multiple PDF files into a single PDF file. The merged file will contain all pages from the input files in the order they were provided. Input:PDF Output:PDF Type:MISO")
 | 
				
			||||||
public ResponseEntity<byte[]> mergePdfs(@ModelAttribute MergePdfsRequest form) throws IOException {
 | 
					    public ResponseEntity<byte[]> mergePdfs(@ModelAttribute MergePdfsRequest form) throws IOException {
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
            MultipartFile[] files = form.getFileInput();
 | 
					            MultipartFile[] files = form.getFileInput();
 | 
				
			||||||
            Arrays.sort(files, getSortComparator(form.getSortType()));
 | 
					            Arrays.sort(files, getSortComparator(form.getSortType()));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    List<PDDocument> documents = new ArrayList<>();
 | 
					            PDFMergerUtility mergedDoc = new PDFMergerUtility();
 | 
				
			||||||
 | 
					            ByteArrayOutputStream docOutputstream = new ByteArrayOutputStream();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            for (MultipartFile file : files) {
 | 
					            for (MultipartFile file : files) {
 | 
				
			||||||
        try (InputStream is = file.getInputStream()) {
 | 
					                mergedDoc.addSource(new ByteArrayInputStream(file.getBytes()));
 | 
				
			||||||
            documents.add(PDDocument.load(is));
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try (PDDocument mergedDoc = mergeDocuments(documents)) {
 | 
					            mergedDoc.setDestinationFileName(files[0].getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_merged.pdf");
 | 
				
			||||||
        ResponseEntity<byte[]> response = WebResponseUtils.pdfDocToWebResponse(mergedDoc, files[0].getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_merged.pdf");
 | 
					            mergedDoc.setDestinationStream(docOutputstream);
 | 
				
			||||||
        return response;
 | 
					            mergedDoc.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
 | 
				
			||||||
    } finally {
 | 
					
 | 
				
			||||||
        for (PDDocument doc : documents) {
 | 
					            return WebResponseUtils.bytesToWebResponse(docOutputstream.toByteArray(), mergedDoc.getDestinationFileName());
 | 
				
			||||||
            if (doc != null) {
 | 
					        } catch (Exception ex) {
 | 
				
			||||||
                doc.close();
 | 
					            logger.error("Error in merge pdf process", ex);
 | 
				
			||||||
            }
 | 
					           throw ex;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user