migrate exportUpdatedPages from bytes to stream (#6201)

Co-authored-by: ConnorYoh <[email protected]>
This commit is contained in:
Anthony Stirling
2026-05-19 23:03:44 +01:00
committed by GitHub
co-authored by ConnorYoh
parent 2c15044dfa
commit 2399da6893
2 changed files with 14 additions and 8 deletions
@@ -184,8 +184,6 @@ public class ConvertPdfJsonController {
validateJobAccess(jobId);
byte[] pdfBytes = pdfJsonConversionService.exportUpdatedPages(jobId, document);
String baseName =
(filename != null && !filename.isBlank())
? FILE_EXTENSION_PATTERN
@@ -197,13 +195,18 @@ public class ConvertPdfJsonController {
.orElse("document");
String docName = baseName.endsWith(".pdf") ? baseName : baseName + ".pdf";
TempFile tempOut = tempFileManager.createManagedTempFile(".pdf");
try {
Files.write(tempOut.getPath(), pdfBytes);
try (OutputStream os = Files.newOutputStream(tempOut.getPath())) {
pdfJsonConversionService.exportUpdatedPages(jobId, document, os);
} catch (Exception e) {
tempOut.close();
throw e;
}
try {
return WebResponseUtils.pdfFileToWebResponse(tempOut, docName);
} catch (Exception e) {
tempOut.close();
throw e;
}
}
@GetMapping(value = "/pdf/text-editor/page/{jobId}/{pageNumber}")
@@ -6490,7 +6490,8 @@ public class PdfJsonConversionService {
objectMapper.writeValue(out, pageFonts);
}
public byte[] exportUpdatedPages(String jobId, PdfJsonDocument updates) throws IOException {
public void exportUpdatedPages(String jobId, PdfJsonDocument updates, OutputStream outputStream)
throws IOException {
if (jobId == null || jobId.isBlank()) {
throw new IllegalArgumentException("jobId is required for incremental export");
}
@@ -6513,7 +6514,8 @@ public class PdfJsonConversionService {
log.debug(
"Incremental export requested with no page updates; returning cached PDF for jobId {}",
jobId);
return cached.getPdfBytes();
outputStream.write(cached.getPdfBytes());
return;
}
try (PDDocument document = pdfDocumentFactory.load(cached.getPdfBytes(), true)) {
@@ -6580,7 +6582,8 @@ public class PdfJsonConversionService {
log.debug(
"Incremental export for jobId {} resulted in no page updates; returning cached PDF",
jobId);
return cached.getPdfBytes();
outputStream.write(cached.getPdfBytes());
return;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@@ -6603,7 +6606,7 @@ public class PdfJsonConversionService {
"Incremental export complete for jobId {} (pages updated: {})",
jobId,
updatedPages.stream().map(i -> i + 1).sorted().toList());
return updatedBytes;
outputStream.write(updatedBytes);
}
}