mirror of
https://github.com/arsvendg/Stirling-PDF.git
synced 2026-07-16 19:33:11 +02:00
fix(api): address potential backend resource leaks and improve frontend accessibility (#5678)
This commit is contained in:
@@ -574,34 +574,39 @@ public class PdfUtils {
|
|||||||
boolean everyPage)
|
boolean everyPage)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
|
||||||
PDDocument document = pdfDocumentFactory.load(pdfBytes);
|
try (PDDocument document = pdfDocumentFactory.load(pdfBytes)) {
|
||||||
|
// Get the first page of the PDF
|
||||||
// Get the first page of the PDF
|
int pages = document.getNumberOfPages();
|
||||||
int pages = document.getNumberOfPages();
|
for (int i = 0; i < pages; i++) {
|
||||||
for (int i = 0; i < pages; i++) {
|
PDPage page = document.getPage(i);
|
||||||
PDPage page = document.getPage(i);
|
try (PDPageContentStream contentStream =
|
||||||
try (PDPageContentStream contentStream =
|
new PDPageContentStream(
|
||||||
new PDPageContentStream(
|
document,
|
||||||
document, page, PDPageContentStream.AppendMode.APPEND, true, true)) {
|
page,
|
||||||
// Create an image object from the image bytes
|
PDPageContentStream.AppendMode.APPEND,
|
||||||
PDImageXObject image = PDImageXObject.createFromByteArray(document, imageBytes, "");
|
true,
|
||||||
// Draw the image onto the page at the specified x and y coordinates
|
true)) {
|
||||||
contentStream.drawImage(image, x, y);
|
// Create an image object from the image bytes
|
||||||
log.info("Image successfully overlaid onto PDF");
|
PDImageXObject image =
|
||||||
if (!everyPage && i == 0) {
|
PDImageXObject.createFromByteArray(document, imageBytes, "");
|
||||||
break;
|
// Draw the image onto the page at the specified x and y coordinates
|
||||||
|
contentStream.drawImage(image, x, y);
|
||||||
|
log.info("Image successfully overlaid onto PDF");
|
||||||
|
if (!everyPage && i == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
// Log an error message if there is an issue overlaying the image onto the PDF
|
||||||
|
log.error("Error overlaying image onto PDF", e);
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
// Log an error message if there is an issue overlaying the image onto the PDF
|
|
||||||
log.error("Error overlaying image onto PDF", e);
|
|
||||||
throw e;
|
|
||||||
}
|
}
|
||||||
|
// Create a ByteArrayOutputStream to save the PDF to
|
||||||
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
|
document.save(baos);
|
||||||
|
log.info("PDF successfully saved to byte array");
|
||||||
|
return baos.toByteArray();
|
||||||
}
|
}
|
||||||
// Create a ByteArrayOutputStream to save the PDF to
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
document.save(baos);
|
|
||||||
log.info("PDF successfully saved to byte array");
|
|
||||||
return baos.toByteArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean containsTextInFile(PDDocument pdfDocument, String text, String pagesToCheck)
|
public boolean containsTextInFile(PDDocument pdfDocument, String text, String pagesToCheck)
|
||||||
|
|||||||
+56
-54
@@ -101,66 +101,68 @@ public class SplitPdfBySectionsController {
|
|||||||
return WebResponseUtils.baosToWebResponse(baos, filename + ".pdf");
|
return WebResponseUtils.baosToWebResponse(baos, filename + ".pdf");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TempFile zipTempFile = new TempFile(tempFileManager, ".zip");
|
try (TempFile zipTempFile = new TempFile(tempFileManager, ".zip")) {
|
||||||
try (ZipOutputStream zipOut =
|
try (ZipOutputStream zipOut =
|
||||||
new ZipOutputStream(Files.newOutputStream(zipTempFile.getPath()))) {
|
new ZipOutputStream(Files.newOutputStream(zipTempFile.getPath()))) {
|
||||||
for (int pageIndex = 0;
|
for (int pageIndex = 0;
|
||||||
pageIndex < sourceDocument.getNumberOfPages();
|
pageIndex < sourceDocument.getNumberOfPages();
|
||||||
pageIndex++) {
|
pageIndex++) {
|
||||||
int pageNum = pageIndex + 1;
|
int pageNum = pageIndex + 1;
|
||||||
if (pagesToSplit.contains(pageIndex)) {
|
if (pagesToSplit.contains(pageIndex)) {
|
||||||
for (int i = 0; i < horiz; i++) {
|
for (int i = 0; i < horiz; i++) {
|
||||||
for (int j = 0; j < verti; j++) {
|
for (int j = 0; j < verti; j++) {
|
||||||
try (PDDocument subDoc =
|
try (PDDocument subDoc =
|
||||||
pdfDocumentFactory.createNewDocument()) {
|
pdfDocumentFactory.createNewDocument()) {
|
||||||
LayerUtility subLayerUtility = new LayerUtility(subDoc);
|
LayerUtility subLayerUtility = new LayerUtility(subDoc);
|
||||||
addSingleSectionToTarget(
|
addSingleSectionToTarget(
|
||||||
sourceDocument,
|
sourceDocument,
|
||||||
pageIndex,
|
pageIndex,
|
||||||
subDoc,
|
subDoc,
|
||||||
subLayerUtility,
|
subLayerUtility,
|
||||||
i,
|
i,
|
||||||
j,
|
j,
|
||||||
horiz,
|
horiz,
|
||||||
verti);
|
verti);
|
||||||
int sectionNum = i * verti + j + 1;
|
int sectionNum = i * verti + j + 1;
|
||||||
String entryName =
|
String entryName =
|
||||||
filename
|
filename
|
||||||
+ "_"
|
+ "_"
|
||||||
+ pageNum
|
+ pageNum
|
||||||
+ "_"
|
+ "_"
|
||||||
+ sectionNum
|
+ sectionNum
|
||||||
+ ".pdf";
|
+ ".pdf";
|
||||||
saveDocToZip(subDoc, zipOut, entryName);
|
saveDocToZip(subDoc, zipOut, entryName);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error(
|
log.error(
|
||||||
"Error creating section {} for page {}",
|
"Error creating section {} for page {}",
|
||||||
(i * verti + j + 1),
|
(i * verti + j + 1),
|
||||||
pageNum,
|
pageNum,
|
||||||
e);
|
e);
|
||||||
throw e;
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
} else {
|
try (PDDocument subDoc = pdfDocumentFactory.createNewDocument()) {
|
||||||
try (PDDocument subDoc = pdfDocumentFactory.createNewDocument()) {
|
LayerUtility subLayerUtility = new LayerUtility(subDoc);
|
||||||
LayerUtility subLayerUtility = new LayerUtility(subDoc);
|
addPageToTarget(
|
||||||
addPageToTarget(sourceDocument, pageIndex, subDoc, subLayerUtility);
|
sourceDocument, pageIndex, subDoc, subLayerUtility);
|
||||||
String entryName = filename + "_" + pageNum + "_1.pdf";
|
String entryName = filename + "_" + pageNum + "_1.pdf";
|
||||||
saveDocToZip(subDoc, zipOut, entryName);
|
saveDocToZip(subDoc, zipOut, entryName);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Error processing unsplit page {}", pageNum, e);
|
log.error("Error processing unsplit page {}", pageNum, e);
|
||||||
throw e;
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error("Error creating ZIP file with split PDF sections", e);
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
byte[] zipBytes = Files.readAllBytes(zipTempFile.getPath());
|
||||||
log.error("Error creating ZIP file with split PDF sections", e);
|
return WebResponseUtils.bytesToWebResponse(
|
||||||
throw e;
|
zipBytes, filename + ".zip", MediaType.APPLICATION_OCTET_STREAM);
|
||||||
}
|
}
|
||||||
byte[] zipBytes = Files.readAllBytes(zipTempFile.getPath());
|
|
||||||
return WebResponseUtils.bytesToWebResponse(
|
|
||||||
zipBytes, filename + ".zip", MediaType.APPLICATION_OCTET_STREAM);
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("Error splitting PDF file: {}", file.getOriginalFilename(), e);
|
log.error("Error splitting PDF file: {}", file.getOriginalFilename(), e);
|
||||||
|
|||||||
+32
-32
@@ -55,45 +55,45 @@ public class OverlayImageController {
|
|||||||
|
|
||||||
boolean isSvg = SvgOverlayUtil.isSvgImage(imageBytes);
|
boolean isSvg = SvgOverlayUtil.isSvgImage(imageBytes);
|
||||||
|
|
||||||
PDDocument document = pdfDocumentFactory.load(pdfBytes);
|
try (PDDocument document = pdfDocumentFactory.load(pdfBytes)) {
|
||||||
|
int pages = document.getNumberOfPages();
|
||||||
|
for (int i = 0; i < pages; i++) {
|
||||||
|
PDPage page = document.getPage(i);
|
||||||
|
|
||||||
int pages = document.getNumberOfPages();
|
if (isSvg) {
|
||||||
for (int i = 0; i < pages; i++) {
|
SvgOverlayUtil.overlaySvgOnPage(document, page, imageBytes, x, y);
|
||||||
PDPage page = document.getPage(i);
|
} else {
|
||||||
|
try (PDPageContentStream contentStream =
|
||||||
|
new PDPageContentStream(
|
||||||
|
document,
|
||||||
|
page,
|
||||||
|
PDPageContentStream.AppendMode.APPEND,
|
||||||
|
true,
|
||||||
|
true)) {
|
||||||
|
PDImageXObject image =
|
||||||
|
PDImageXObject.createFromByteArray(document, imageBytes, "");
|
||||||
|
contentStream.drawImage(image, x, y);
|
||||||
|
log.info("Image successfully overlaid onto PDF page {}", i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isSvg) {
|
if (!everyPage && i == 0) {
|
||||||
SvgOverlayUtil.overlaySvgOnPage(document, page, imageBytes, x, y);
|
break;
|
||||||
} else {
|
|
||||||
try (PDPageContentStream contentStream =
|
|
||||||
new PDPageContentStream(
|
|
||||||
document,
|
|
||||||
page,
|
|
||||||
PDPageContentStream.AppendMode.APPEND,
|
|
||||||
true,
|
|
||||||
true)) {
|
|
||||||
PDImageXObject image =
|
|
||||||
PDImageXObject.createFromByteArray(document, imageBytes, "");
|
|
||||||
contentStream.drawImage(image, x, y);
|
|
||||||
log.info("Image successfully overlaid onto PDF page {}", i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!everyPage && i == 0) {
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
break;
|
document.save(baos);
|
||||||
}
|
|
||||||
|
byte[] result = baos.toByteArray();
|
||||||
|
log.info("PDF with overlaid image successfully created");
|
||||||
|
|
||||||
|
return WebResponseUtils.bytesToWebResponse(
|
||||||
|
result,
|
||||||
|
GeneralUtils.generateFilename(
|
||||||
|
pdfFile.getOriginalFilename(), "_overlayed.pdf"));
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
document.save(baos);
|
|
||||||
document.close();
|
|
||||||
|
|
||||||
byte[] result = baos.toByteArray();
|
|
||||||
log.info("PDF with overlaid image successfully created");
|
|
||||||
|
|
||||||
return WebResponseUtils.bytesToWebResponse(
|
|
||||||
result,
|
|
||||||
GeneralUtils.generateFilename(pdfFile.getOriginalFilename(), "_overlayed.pdf"));
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Failed to add image to PDF", e);
|
log.error("Failed to add image to PDF", e);
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ export default function HomePage() {
|
|||||||
<div className="mobile-toggle">
|
<div className="mobile-toggle">
|
||||||
<div className="mobile-header">
|
<div className="mobile-header">
|
||||||
<div className="mobile-brand">
|
<div className="mobile-brand">
|
||||||
<img src={brandIconSrc} alt="" className="mobile-brand-icon" />
|
<img src={brandIconSrc} alt="" aria-hidden="true" className="mobile-brand-icon" />
|
||||||
<img src={brandTextSrc} alt={brandAltText} className="mobile-brand-text" />
|
<img src={brandTextSrc} alt={brandAltText} className="mobile-brand-text" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user