Skip to content

Commit

Permalink
possible fixes for overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
Frooodle committed Dec 17, 2023
1 parent 39345bb commit 5774a22
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'stirling.software'
version = '0.17.0'
version = '0.17.1'
sourceCompatibility = '17'

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ public ResponseEntity<byte[]> overlayPdfs(@ModelAttribute OverlayPdfsRequest req
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
overlay.overlay(overlayGuide).save(outputStream);
byte[] data = outputStream.toByteArray();

return WebResponseUtils.bytesToWebResponse(data, "overlaid.pdf", MediaType.APPLICATION_PDF);
String outputFilename = baseFile.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_overlayed.pdf"; // Remove file extension and append .pdf

return WebResponseUtils.bytesToWebResponse(data, outputFilename, MediaType.APPLICATION_PDF);
}
} finally {
for (File overlayPdfFile : overlayPdfFiles) {
Expand Down Expand Up @@ -84,17 +85,20 @@ private Map<Integer, String> prepareOverlayGuide(int basePageCount, File[] overl
}

private void sequentialOverlay(Map<Integer, String> overlayGuide, File[] overlayFiles, int basePageCount) throws IOException {
int currentPage = 1;
for (File overlayFile : overlayFiles) {
try (PDDocument overlayPdf = PDDocument.load(overlayFile)) {
for (int i = 0; i < overlayPdf.getNumberOfPages(); i++) {
if (currentPage > basePageCount) break;
overlayGuide.put(currentPage++, overlayFile.getAbsolutePath());
}
if (overlayFiles.length != 1 || basePageCount != PDDocument.load(overlayFiles[0]).getNumberOfPages()) {
throw new IllegalArgumentException("Overlay file count and base page count must match for sequential overlay.");
}

File overlayFile = overlayFiles[0];
try (PDDocument overlayPdf = PDDocument.load(overlayFile)) {
for (int i = 1; i <= overlayPdf.getNumberOfPages(); i++) {
if (i > basePageCount) break;
overlayGuide.put(i, overlayFile.getAbsolutePath());
}
}
}


private void interleavedOverlay(Map<Integer, String> overlayGuide, File[] overlayFiles, int basePageCount) throws IOException {
for (int i = 0; i < basePageCount; i++) {
File overlayFile = overlayFiles[i % overlayFiles.length];
Expand Down

0 comments on commit 5774a22

Please sign in to comment.