1
0
mirror of https://github.com/Stirling-Tools/Stirling-PDF.git synced 2024-10-02 09:10:12 +02:00

Merge pull request #366 from Frooodle/image

image to pdf change and cert atempt fix
This commit is contained in:
Anthony Stirling 2023-09-17 21:21:19 +01:00 committed by GitHub
commit 05ebf3a6b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 161 additions and 37 deletions

View File

@ -8,7 +8,7 @@ plugins {
}
group = 'stirling.software'
version = '0.14.1'
version = '0.14.2'
sourceCompatibility = '17'
repositories {

View File

@ -79,12 +79,12 @@ public class ConvertImgPDFController {
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");
}

View File

@ -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;

View File

@ -248,7 +248,7 @@ public class PdfUtils {
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();
@ -261,7 +261,7 @@ public class PdfUtils {
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();
@ -279,7 +279,7 @@ public class PdfUtils {
} 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;
@ -295,12 +295,20 @@ public class PdfUtils {
}
}
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);
@ -308,9 +316,9 @@ public class PdfUtils {
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;
@ -331,6 +339,7 @@ public class PdfUtils {
}
}
public static byte[] overlayImage(byte[] pdfBytes, byte[] imageBytes, float x, float y, boolean everyPage) throws IOException {
PDDocument document = PDDocument.load(new ByteArrayInputStream(pdfBytes));

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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
@ -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

View File

@ -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

View File

@ -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
@ -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

View File

@ -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

View File

@ -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に結合

View File

@ -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로 병합

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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文件

View File

@ -21,10 +21,16 @@
<div th:replace="~{fragments/common :: fileSelector(name='fileInput', multiple=false, accept='image/*', inputText=#{imgPrompt})}"></div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="stretchToFit" id="stretchToFit">
<label class="ms-3" for="stretchToFit" th:text=#{imageToPDF.selectText.1}></label>
</div>
<div class="mb-3">
<label for="fitOption" th:text="#{imageToPDF.selectLabel}">Fit Options</label>
<select class="form-control" id="fitOption" name="fitOption">
<option value="fillPage" th:text="#{imageToPDF.fillPage}">Fill Page</option>
<option value="fitDocumentToImage" th:text="#{imageToPDF.fitDocumentToImage}">Fit Document to Image</option>
<option value="maintainAspectRatio" th:text="#{imageToPDF.maintainAspectRatio}">Maintain Aspect Ratio</option>
</select>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" name="autoRotate" id="autoRotate">
<label class="ms-3" for="autoRotate" th:text=#{imageToPDF.selectText.2}></label>

View File

@ -35,19 +35,19 @@
<div class="mb-3" id="p12Group" style="display: none;">
<label th:text="#{certSign.selectP12}"></label>
<div
th:replace="~{fragments/common :: fileSelector(name='p12', notRequired=true, multiple=false, accept='.p12,.pfx')}"></div>
th:replace="~{fragments/common :: fileSelector(name='p12File', notRequired=true, multiple=false, accept='.p12,.pfx')}"></div>
</div>
<div id="pemGroup" style="display: none;">
<div class="mb-3">
<label th:text="#{certSign.selectKey}"></label>
<div
th:replace="~{fragments/common :: fileSelector(name='key', multiple=false, notRequired=true, accept='.pem,.der')}"></div>
th:replace="~{fragments/common :: fileSelector(name='privateKeyFile', multiple=false, notRequired=true, accept='.pem,.der')}"></div>
</div>
<div class="mb-3">
<label th:text="#{certSign.selectCert}"></label>
<div
th:replace="~{fragments/common :: fileSelector(name='cert', multiple=false, notRequired=true, accept='.pem,.der')}"></div>
th:replace="~{fragments/common :: fileSelector(name='certFile', multiple=false, notRequired=true, accept='.pem,.der')}"></div>
</div>
</div>