From 51fe6970ef328ef1eeac2bb17035c0e2eced34be Mon Sep 17 00:00:00 2001 From: Anthony Stirling <77850077+Frooodle@users.noreply.github.com> Date: Sat, 25 Feb 2023 09:53:07 +0000 Subject: [PATCH] jpg and gif support for pdf to image (#59) --- .../SPDF/controller/converters/ConvertImgPDFController.java | 2 +- src/main/java/stirling/software/SPDF/utils/PdfUtils.java | 6 +++--- src/main/resources/templates/convert/pdf-to-img.html | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/controller/converters/ConvertImgPDFController.java b/src/main/java/stirling/software/SPDF/controller/converters/ConvertImgPDFController.java index 6e1c624a..25b58297 100644 --- a/src/main/java/stirling/software/SPDF/controller/converters/ConvertImgPDFController.java +++ b/src/main/java/stirling/software/SPDF/controller/converters/ConvertImgPDFController.java @@ -61,7 +61,7 @@ public class ConvertImgPDFController { boolean singleImage = singleOrMultiple.equals("single"); byte[] result = null; try { - result = PdfUtils.convertFromPdf(pdfBytes, imageFormat.toLowerCase(), colorTypeResult, singleImage, Integer.valueOf(dpi)); + result = PdfUtils.convertFromPdf(pdfBytes, imageFormat.toUpperCase(), colorTypeResult, singleImage, Integer.valueOf(dpi)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 76c56cc3..67629c0c 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -107,7 +107,7 @@ public class PdfUtils { ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (singleImage) { // Write the image to the output stream - ImageIO.write(images.get(0), "PNG", baos); + ImageIO.write(images.get(0), imageType, baos); // Log that the image was successfully written to the byte array logger.info("Image successfully written to byte array"); @@ -117,10 +117,10 @@ public class PdfUtils { for (int i = 0; i < images.size(); i++) { BufferedImage image = images.get(i); try (ByteArrayOutputStream baosImage = new ByteArrayOutputStream()) { - ImageIO.write(image, "PNG", baosImage); + ImageIO.write(image, imageType, baosImage); // Add the image to the zip file - zos.putNextEntry(new ZipEntry(String.format("page_%d.%s", i + 1, "png"))); + zos.putNextEntry(new ZipEntry(String.format("page_%d.%s", i + 1, imageType.toLowerCase()))); zos.write(baosImage.toByteArray()); } } diff --git a/src/main/resources/templates/convert/pdf-to-img.html b/src/main/resources/templates/convert/pdf-to-img.html index ef6c48ee..29307d5d 100644 --- a/src/main/resources/templates/convert/pdf-to-img.html +++ b/src/main/resources/templates/convert/pdf-to-img.html @@ -21,6 +21,8 @@