diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 1b0275b263d..e4e577b8eeb 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 static byte[] imageToPdf(MultipartFile[] files, String fitOption, boolean } 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); } }