Skip to content

Commit 2399da6

Browse files
FrooodleConnorYoh
andauthored
migrate exportUpdatedPages from bytes to stream (Stirling-Tools#6201)
Co-authored-by: ConnorYoh <40631091+ConnorYoh@users.noreply.github.com>
1 parent 2c15044 commit 2399da6

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

app/core/src/main/java/stirling/software/SPDF/controller/api/converters/ConvertPdfJsonController.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ public ResponseEntity<Resource> exportPartialPdf(
184184

185185
validateJobAccess(jobId);
186186

187-
byte[] pdfBytes = pdfJsonConversionService.exportUpdatedPages(jobId, document);
188-
189187
String baseName =
190188
(filename != null && !filename.isBlank())
191189
? FILE_EXTENSION_PATTERN
@@ -197,13 +195,18 @@ public ResponseEntity<Resource> exportPartialPdf(
197195
.orElse("document");
198196
String docName = baseName.endsWith(".pdf") ? baseName : baseName + ".pdf";
199197
TempFile tempOut = tempFileManager.createManagedTempFile(".pdf");
198+
try (OutputStream os = Files.newOutputStream(tempOut.getPath())) {
199+
pdfJsonConversionService.exportUpdatedPages(jobId, document, os);
200+
} catch (Exception e) {
201+
tempOut.close();
202+
throw e;
203+
}
200204
try {
201-
Files.write(tempOut.getPath(), pdfBytes);
205+
return WebResponseUtils.pdfFileToWebResponse(tempOut, docName);
202206
} catch (Exception e) {
203207
tempOut.close();
204208
throw e;
205209
}
206-
return WebResponseUtils.pdfFileToWebResponse(tempOut, docName);
207210
}
208211

209212
@GetMapping(value = "/pdf/text-editor/page/{jobId}/{pageNumber}")

app/core/src/main/java/stirling/software/SPDF/service/PdfJsonConversionService.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6490,7 +6490,8 @@ public void extractPageFonts(String jobId, int pageNumber, OutputStream out)
64906490
objectMapper.writeValue(out, pageFonts);
64916491
}
64926492

6493-
public byte[] exportUpdatedPages(String jobId, PdfJsonDocument updates) throws IOException {
6493+
public void exportUpdatedPages(String jobId, PdfJsonDocument updates, OutputStream outputStream)
6494+
throws IOException {
64946495
if (jobId == null || jobId.isBlank()) {
64956496
throw new IllegalArgumentException("jobId is required for incremental export");
64966497
}
@@ -6513,7 +6514,8 @@ public byte[] exportUpdatedPages(String jobId, PdfJsonDocument updates) throws I
65136514
log.debug(
65146515
"Incremental export requested with no page updates; returning cached PDF for jobId {}",
65156516
jobId);
6516-
return cached.getPdfBytes();
6517+
outputStream.write(cached.getPdfBytes());
6518+
return;
65176519
}
65186520

65196521
try (PDDocument document = pdfDocumentFactory.load(cached.getPdfBytes(), true)) {
@@ -6580,7 +6582,8 @@ public byte[] exportUpdatedPages(String jobId, PdfJsonDocument updates) throws I
65806582
log.debug(
65816583
"Incremental export for jobId {} resulted in no page updates; returning cached PDF",
65826584
jobId);
6583-
return cached.getPdfBytes();
6585+
outputStream.write(cached.getPdfBytes());
6586+
return;
65846587
}
65856588

65866589
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -6603,7 +6606,7 @@ public byte[] exportUpdatedPages(String jobId, PdfJsonDocument updates) throws I
66036606
"Incremental export complete for jobId {} (pages updated: {})",
66046607
jobId,
66056608
updatedPages.stream().map(i -> i + 1).sorted().toList());
6606-
return updatedBytes;
6609+
outputStream.write(updatedBytes);
66076610
}
66086611
}
66096612

0 commit comments

Comments
 (0)