Skip to content

Commit

Permalink
Merge pull request #366 from Frooodle/image
Browse files Browse the repository at this point in the history
image to pdf change and cert atempt fix
  • Loading branch information
Frooodle authored Sep 17, 2023
2 parents a1f388e + 1be3046 commit 05ebf3a
Show file tree
Hide file tree
Showing 24 changed files with 161 additions and 37 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = 'stirling.software'
version = '0.14.1'
version = '0.14.2'
sourceCompatibility = '17'

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public ResponseEntity<Resource> convertToImage(@ModelAttribute ConvertToImageReq
description = "This endpoint converts one or more images to a PDF file. Users can specify whether to stretch the images to fit the PDF page, and whether to automatically rotate the images. Input:Image Output:PDF Type:SISO?")
public ResponseEntity<byte[]> convertToPdf(@ModelAttribute ConvertToPdfRequest request) throws IOException {
MultipartFile[] file = request.getFileInput();
boolean stretchToFit = request.isStretchToFit();
String fitOption = request.getFitOption();
String colorType = request.getColorType();
boolean autoRotate = request.isAutoRotate();

// Convert the file to PDF and get the resulting bytes
byte[] bytes = PdfUtils.imageToPdf(file, stretchToFit, autoRotate, colorType);
byte[] bytes = PdfUtils.imageToPdf(file, fitOption, autoRotate, colorType);
return WebResponseUtils.bytesToWebResponse(bytes, file[0].getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_converted.pdf");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ public class ConvertToPdfRequest {
@Schema(description = "The input images to be converted to a PDF file")
private MultipartFile[] fileInput;

@Schema(description = "Whether to stretch the images to fit the PDF page or maintain the aspect ratio", example = "false")
private boolean stretchToFit;
@Schema(description = "Option to determine how the image will fit onto the page",
allowableValues = { "fillPage", "fitDocumentToImage", "maintainAspectRatio" })
private String fitOption;



@Schema(description = "The color type of the output image(s)", allowableValues = {"color", "greyscale", "blackwhite"})
private String colorType;
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/stirling/software/SPDF/utils/PdfUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public static byte[] convertFromPdf(byte[] inputStream, String imageType, ImageT
throw e;
}
}
public static byte[] imageToPdf(MultipartFile[] files, boolean stretchToFit, boolean autoRotate, String colorType) throws IOException {
public static byte[] imageToPdf(MultipartFile[] files, String fitOption, boolean autoRotate, String colorType) throws IOException {
try (PDDocument doc = new PDDocument()) {
for (MultipartFile file : files) {
String contentType = file.getContentType();
Expand All @@ -261,7 +261,7 @@ public static byte[] imageToPdf(MultipartFile[] files, boolean stretchToFit, boo
BufferedImage pageImage = reader.read(i);
BufferedImage convertedImage = ImageProcessingUtils.convertColorType(pageImage, colorType);
PDImageXObject pdImage = LosslessFactory.createFromImage(doc, convertedImage);
addImageToDocument(doc, pdImage, stretchToFit, autoRotate);
addImageToDocument(doc, pdImage, fitOption, autoRotate);
}
} else {
File imageFile = Files.createTempFile("image", ".png").toFile();
Expand All @@ -279,7 +279,7 @@ public static byte[] imageToPdf(MultipartFile[] files, boolean stretchToFit, boo
} else {
pdImage = LosslessFactory.createFromImage(doc, convertedImage);
}
addImageToDocument(doc, pdImage, stretchToFit, autoRotate);
addImageToDocument(doc, pdImage, fitOption, autoRotate);
} catch (IOException e) {
logger.error("Error writing image to file: {}", imageFile.getAbsolutePath(), e);
throw e;
Expand All @@ -295,22 +295,30 @@ public static byte[] imageToPdf(MultipartFile[] files, boolean stretchToFit, boo
}
}

private static void addImageToDocument(PDDocument doc, PDImageXObject image, boolean stretchToFit, boolean autoRotate) throws IOException {
private static void addImageToDocument(PDDocument doc, PDImageXObject image, String fitOption, boolean autoRotate) throws IOException {
boolean imageIsLandscape = image.getWidth() > image.getHeight();
PDRectangle pageSize = PDRectangle.A4;

System.out.println(fitOption);

if (autoRotate && imageIsLandscape) {
pageSize = new PDRectangle(pageSize.getHeight(), pageSize.getWidth());
}

if ("fitDocumentToImage".equals(fitOption)) {
pageSize = new PDRectangle(image.getWidth(), image.getHeight());
}

PDPage page = new PDPage(pageSize);
doc.addPage(page);

float pageWidth = page.getMediaBox().getWidth();
float pageHeight = page.getMediaBox().getHeight();

try (PDPageContentStream contentStream = new PDPageContentStream(doc, page)) {
if (stretchToFit) {
if ("fillPage".equals(fitOption) || "fitDocumentToImage".equals(fitOption)) {
contentStream.drawImage(image, 0, 0, pageWidth, pageHeight);
} else {
} else if ("maintainAspectRatio".equals(fitOption)) {
float imageAspectRatio = (float) image.getWidth() / (float) image.getHeight();
float pageAspectRatio = pageWidth / pageHeight;

Expand All @@ -331,6 +339,7 @@ private static void addImageToDocument(PDDocument doc, PDImageXObject image, boo
}
}


public static byte[] overlayImage(byte[] pdfBytes, byte[] imageBytes, float x, float y, boolean everyPage) throws IOException {

PDDocument document = PDDocument.load(new ByteArrayInputStream(pdfBytes));
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_ar_AR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Split
imageToPDF.title=صورة إلى PDF
imageToPDF.header=صورة إلى PDF
imageToPDF.submit=تحول
imageToPDF.selectText.1=\u062A\u0645\u062F\u062F \u0644\u0644\u0645\u0644\u0627\u0621\u0645\u0629
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=\u062F\u0648\u0631\u0627\u0646 PDF \u062A\u0644\u0642\u0627\u0626\u064A\u064B\u0627
imageToPDF.selectText.3=\u0627\u0644\u0645\u0646\u0637\u0642 \u0627\u0644\u0645\u062A\u0639\u062F\u062F \u0644\u0644\u0645\u0644\u0641\u0627\u062A (\u0645\u0641\u0639\u0651\u0644 \u0641\u0642\u0637 \u0625\u0630\u0627 \u0643\u0646\u062A \u062A\u0639\u0645\u0644 \u0645\u0639 \u0635\u0648\u0631 \u0645\u062A\u0639\u062F\u062F\u0629)
imageToPDF.selectText.4=\u062F\u0645\u062C \u0641\u064A \u0645\u0644\u0641 PDF \u0648\u0627\u062D\u062F
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_ca_CA.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Divideix
imageToPDF.title=Imatge a PDF
imageToPDF.header=Imatge a PDF
imageToPDF.submit=Converteix
imageToPDF.selectText.1=Estirar per adaptar
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=Auto rota PDF
imageToPDF.selectText.3=Lògica de diversos fitxers (només està activada si es treballa amb diverses imatges)
imageToPDF.selectText.4=Combina en un únic PDF
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_de_DE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Aufteilen
imageToPDF.title=Bild zu PDF
imageToPDF.header=Bild zu PDF
imageToPDF.submit=Umwandeln
imageToPDF.selectText.1=Auf Seite strecken
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=PDF automatisch drehen
imageToPDF.selectText.3=Mehrere Dateien verarbeiten (nur aktiv, wenn Sie mit mehreren Bildern arbeiten)
imageToPDF.selectText.4=In ein einziges PDF zusammenführen
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/messages_en_GB.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,10 @@ split.submit=Split
imageToPDF.title=Image to PDF
imageToPDF.header=Image to PDF
imageToPDF.submit=Convert
imageToPDF.selectText.1=Stretch to fit
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=Auto rotate PDF
imageToPDF.selectText.3=Multi file logic (Only enabled if working with multiple images)
imageToPDF.selectText.4=Merge into single PDF
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Split
imageToPDF.title=Image to PDF
imageToPDF.header=Image to PDF
imageToPDF.submit=Convert
imageToPDF.selectText.1=Stretch to fit
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=Auto rotate PDF
imageToPDF.selectText.3=Multi file logic (Only enabled if working with multiple images)
imageToPDF.selectText.4=Merge into single PDF
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/messages_es_ES.properties
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ home.MarkdownToPDF.title=Markdown a PDF
home.MarkdownToPDF.desc=Convierte cualquier archivo Markdown a PDF
MarkdownToPDF.tags=margen,contenido web,transformación,convertir


home.getPdfInfo.title=Obtener toda la información en PDF
home.getPdfInfo.desc=Obtiene toda la información posible de archivos PDF
getPdfInfo.tags=información,datos,stats,estadísticas
Expand Down Expand Up @@ -664,7 +665,13 @@ split.submit=Dividir
imageToPDF.title=Imagen a PDF
imageToPDF.header=Imagen a PDF
imageToPDF.submit=Convertir
imageToPDF.selectText.1=Estirar para ajustar
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=Rotación automática del PDF
imageToPDF.selectText.3=Lógica de archivos múltiples (únicamente activado si funciona con multiples imágenes)
imageToPDF.selectText.4=Unir en un único archivo PDF
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_eu_ES.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Zatitu
imageToPDF.title=Irudia PDF bihurtu
imageToPDF.header=Irudia PDF bihurtu
imageToPDF.submit=Bihurtu
imageToPDF.selectText.1=Zabaldu doitzeko
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=PDFaren errotazio automatikoa
imageToPDF.selectText.3=Fitxategi askoren logika (gaituta bakarrik zenbait irudirekin ari denean)
imageToPDF.selectText.4=Elkartu PDF bakar batean
Expand Down
10 changes: 8 additions & 2 deletions src/main/resources/messages_fr_FR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Diviser
imageToPDF.title=Image en PDF
imageToPDF.header=Image en PDF
imageToPDF.submit=Convertir
imageToPDF.selectText.1=Étirer pour adapter
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=Rotation automatique du PDF
imageToPDF.selectText.3=Logique multi-fichiers (uniquement activée si vous travaillez avec plusieurs images)
imageToPDF.selectText.4=Fusionner en un seul PDF
Expand Down Expand Up @@ -749,7 +755,7 @@ removePassword.submit=Supprimer


#changeMetadata
changeMetadata.title=Modifier les métadonnées
changeMetadata.title=Titre
changeMetadata.header=Modifier les métadonnées
changeMetadata.selectText.1=Veuillez modifier les variables que vous souhaitez modifier.
changeMetadata.selectText.2=Supprimer toutes les métadonnées
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_it_IT.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Dividi
imageToPDF.title=Immagine a PDF
imageToPDF.header=Immagine a PDF
imageToPDF.submit=Converti
imageToPDF.selectText.1=Allarga per riempire
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=Ruota automaticamente PDF
imageToPDF.selectText.3=Logica multi-file (funziona solo se ci sono più immagini)
imageToPDF.selectText.4=Unisci in un unico PDF
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_ja_JP.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=分割
imageToPDF.title=画像をPDFに変換
imageToPDF.header=画像をPDFに変換
imageToPDF.submit=変換
imageToPDF.selectText.1=フィットするように引き伸ばす
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=PDFの自動回転
imageToPDF.selectText.3=マルチファイルの処理 (複数の画像を操作する場合に有効になります)
imageToPDF.selectText.4=1つのPDFに結合
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_ko_KR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=분할
imageToPDF.title=이미지를 PDF로 변환
imageToPDF.header=이미지를 PDF로 변환
imageToPDF.submit=변환하기
imageToPDF.selectText.1=맞춤 크기로 늘리기
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=PDF 자동 회전
imageToPDF.selectText.3=다중 파일 로직 (여러 이미지로 작업하는 경우에만 활성화됨)
imageToPDF.selectText.4=단일 PDF로 병합
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_nl_NL.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Splitsen
imageToPDF.title=Afbeelding naar PDF
imageToPDF.header=Afbeelding naar PDF
imageToPDF.submit=Omzetten
imageToPDF.selectText.1=Uitrekken om te passen
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=PDF automatisch draaien
imageToPDF.selectText.3=Meervoudige bestandslogica (Alleen ingeschakeld bij werken met meerdere afbeeldingen)
imageToPDF.selectText.4=Voeg samen in één PDF
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_pl_PL.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Podziel
imageToPDF.title=Obraz na PDF
imageToPDF.header=Obraz na PDF
imageToPDF.submit=Konwertuj
imageToPDF.selectText.1=Rozciągnij, aby dopasować
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=Automatyczne obracanie PDF
imageToPDF.selectText.3=Logika wielu plików (dostępna tylko w przypadku pracy z wieloma obrazami)
imageToPDF.selectText.4=Połącz w jeden dokument PDF
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Dividir
imageToPDF.title=Imagem para PDF
imageToPDF.header=Converter Imagem para PDF
imageToPDF.submit=Converter
imageToPDF.selectText.1=Esticar para Ajustar
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=Girar Automaticamente
imageToPDF.selectText.3=Lógica de Vários Arquivos (Ativada apenas ao trabalhar com várias imagens)
imageToPDF.selectText.4=Mesclar em um Único PDF
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_ro_RO.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Împarte
imageToPDF.title=Imagine în PDF
imageToPDF.header=Imagine în PDF
imageToPDF.submit=Convertă
imageToPDF.selectText.1=Redimensionare pentru a se potrivi
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=Rotire automată a PDF-ului
imageToPDF.selectText.3=Logica pentru mai multe fișiere (activată numai dacă se lucrează cu mai multe imagini)
imageToPDF.selectText.4=Unifică într-un singur PDF
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_ru_RU.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Разделить
imageToPDF.title=Изображение в PDF
imageToPDF.header=Изображение в PDF
imageToPDF.submit=Конвертировать
imageToPDF.selectText.1=Растянуть, чтобы соответствовать
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=Автоматический поворот PDF
imageToPDF.selectText.3=Многофайловая логика (включена только при работе с несколькими изображениями)
imageToPDF.selectText.4=Объединить в один PDF
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_sv_SE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=Dela
imageToPDF.title=Bild till PDF
imageToPDF.header=Bild till PDF
imageToPDF.submit=Konvertera
imageToPDF.selectText.1=Sträck för att passa
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=Rotera PDF automatiskt
imageToPDF.selectText.3=Multifillogik (Endast aktiverad om man arbetar med flera bilder)
imageToPDF.selectText.4=Slå samman till en enda PDF
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/messages_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,13 @@ split.submit=拆分
imageToPDF.title=图片转PDF
imageToPDF.header=图像转为PDF
imageToPDF.submit=转换
imageToPDF.selectText.1=拉伸至适合的尺寸
##########################
### TODO: Translate ###
##########################
imageToPDF.selectLabel=Image Fit Options
imageToPDF.fillPage=Fill Page
imageToPDF.fitDocumentToImage=Fit Page to Image
imageToPDF.maintainAspectRatio=Maintain Aspect Ratios
imageToPDF.selectText.2=自动旋转PDF
imageToPDF.selectText.3=多文件逻辑(仅在处理多个图像时启用)
imageToPDF.selectText.4=合并成一个PDF文件
Expand Down
Loading

0 comments on commit 05ebf3a

Please sign in to comment.