WIP: Fixes issue - needs refactor
This commit is contained in:
parent
9652f59ae9
commit
fb18d0d04d
1 changed files with 85 additions and 76 deletions
|
@ -1,7 +1,6 @@
|
||||||
package stirling.software.SPDF.controller.api;
|
package stirling.software.SPDF.controller.api;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.*;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
|
@ -10,10 +9,15 @@ import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.pdfbox.io.MemoryUsageSetting;
|
||||||
|
import org.apache.pdfbox.multipdf.PDFMergerUtility;
|
||||||
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;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
@ -90,26 +94,31 @@ private Comparator<MultipartFile> getSortComparator(String sortType) {
|
||||||
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("[.][^.]+$", ""));
|
||||||
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) {
|
|
||||||
if (doc != null) {
|
|
||||||
doc.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
HttpHeaders headers = new HttpHeaders();
|
||||||
|
headers.setContentType(MediaType.APPLICATION_PDF);
|
||||||
|
|
||||||
|
// Here you have to set the actual filename of your pdf
|
||||||
|
headers.setContentDispositionFormData(mergedDoc.getDestinationFileName(), mergedDoc.getDestinationFileName());
|
||||||
|
headers.setCacheControl("must-revalidate, post-check=0, pre-check=0");
|
||||||
|
return new ResponseEntity<>(docOutputstream.toByteArray(), headers, HttpStatus.OK);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
logger.error("Error in merge pdf process", ex);
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue