From 1efefcfcb8d1be88abe8a8837d3cf3d360617b3f Mon Sep 17 00:00:00 2001 From: sbplat <71648843+sbplat@users.noreply.github.com> Date: Sat, 23 Dec 2023 20:27:04 -0500 Subject: [PATCH] feat: add support for svg+webp images to convert to pdf --- build.gradle | 23 +++++++++++++--- .../software/SPDF/utils/PdfUtils.java | 26 +++---------------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/build.gradle b/build.gradle index d2a0359d..66de762e 100644 --- a/build.gradle +++ b/build.gradle @@ -80,12 +80,27 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test:3.1.6' + // Batik + implementation 'org.apache.xmlgraphics:batik-all:1.17' + // TwelveMonkeys + implementation 'com.twelvemonkeys.imageio:imageio-batik:3.10.1' + implementation 'com.twelvemonkeys.imageio:imageio-bmp:3.10.1' + // implementation 'com.twelvemonkeys.imageio:imageio-hdr:3.10.1' + // implementation 'com.twelvemonkeys.imageio:imageio-icns:3.10.1' + // implementation 'com.twelvemonkeys.imageio:imageio-iff:3.10.1' + implementation 'com.twelvemonkeys.imageio:imageio-jpeg:3.10.1' + // implementation 'com.twelvemonkeys.imageio:imageio-pcx:3.10.1' + // implementation 'com.twelvemonkeys.imageio:imageio-pict:3.10.1' + // implementation 'com.twelvemonkeys.imageio:imageio-pnm:3.10.1' + // implementation 'com.twelvemonkeys.imageio:imageio-psd:3.10.1' + // implementation 'com.twelvemonkeys.imageio:imageio-sgi:3.10.1' + // implementation 'com.twelvemonkeys.imageio:imageio-tga:3.10.1' + // implementation 'com.twelvemonkeys.imageio:imageio-thumbsdb:3.10.1' + implementation 'com.twelvemonkeys.imageio:imageio-tiff:3.10.1' + implementation 'com.twelvemonkeys.imageio:imageio-webp:3.10.1' + // implementation 'com.twelvemonkeys.imageio:imageio-xwd:3.10.1' - // https://mvnrepository.com/artifact/org.apache.pdfbox/jbig2-imageio - implementation group: 'org.apache.pdfbox', name: 'jbig2-imageio', version: '3.0.4' - implementation 'com.github.jai-imageio:jai-imageio-core:1.4.0' - implementation 'com.github.jai-imageio:jai-imageio-jpeg2000:1.3.0' implementation 'commons-io:commons-io:2.15.1' implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0' diff --git a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java index 2f57fe10..1b0275b2 100644 --- a/src/main/java/stirling/software/SPDF/utils/PdfUtils.java +++ b/src/main/java/stirling/software/SPDF/utils/PdfUtils.java @@ -264,28 +264,10 @@ public class PdfUtils { addImageToDocument(doc, pdImage, fitOption, autoRotate); } } else { - File imageFile = Files.createTempFile("image", ".png").toFile(); - try (FileOutputStream fos = new FileOutputStream(imageFile); InputStream input = file.getInputStream()) { - byte[] buffer = new byte[1024]; - int len; - while ((len = input.read(buffer)) != -1) { - fos.write(buffer, 0, len); - } - BufferedImage image = ImageIO.read(imageFile); - BufferedImage convertedImage = ImageProcessingUtils.convertColorType(image, colorType); - PDImageXObject pdImage; - if (contentType != null && (contentType.equals("image/jpeg"))) { - pdImage = JPEGFactory.createFromImage(doc, convertedImage); - } else { - pdImage = LosslessFactory.createFromImage(doc, convertedImage); - } - addImageToDocument(doc, pdImage, fitOption, autoRotate); - } catch (IOException e) { - logger.error("Error writing image to file: {}", imageFile.getAbsolutePath(), e); - throw e; - } finally { - imageFile.delete(); - } + BufferedImage image = ImageIO.read(file.getInputStream()); + BufferedImage convertedImage = ImageProcessingUtils.convertColorType(image, colorType); + PDImageXObject pdImage = LosslessFactory.createFromImage(doc, convertedImage); + addImageToDocument(doc, pdImage, fitOption, autoRotate); } } ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();