From 3f50979d3e6f7d01585bcdb8207260140c50c7c3 Mon Sep 17 00:00:00 2001 From: sbplat <71648843+sbplat@users.noreply.github.com> Date: Sun, 24 Dec 2023 12:52:55 -0500 Subject: [PATCH] fix: use JPEGFactory for jpeg --- src/main/java/stirling/software/SPDF/utils/PdfUtils.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 1b0275b2..e4e577b8 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -266,7 +266,10 @@ public class PdfUtils { } else { BufferedImage image = ImageIO.read(file.getInputStream()); BufferedImage convertedImage = ImageProcessingUtils.convertColorType(image, colorType); - PDImageXObject pdImage = LosslessFactory.createFromImage(doc, convertedImage); + // Use JPEGFactory if it's JPEG since JPEG is lossy + PDImageXObject pdImage = (contentType != null && contentType.equals("image/jpeg")) + ? JPEGFactory.createFromImage(doc, convertedImage) + : LosslessFactory.createFromImage(doc, convertedImage); addImageToDocument(doc, pdImage, fitOption, autoRotate); } }