Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

fix(convert): support fitDocumentToPage option in image to PDF conversion (#7446)

Vivek Maddheshiya37694ed71744
diff --git a/app/common/src/main/java/stirling/software/common/util/PdfUtils.java b/app/common/src/main/java/stirling/software/common/util/PdfUtils.java
index 90cacdf3b8..cb28a68177 100644
--- a/app/common/src/main/java/stirling/software/common/util/PdfUtils.java
+++ b/app/common/src/main/java/stirling/software/common/util/PdfUtils.java
@@ -493,7 +493,7 @@ public class PdfUtils {
pageSize = new PDRectangle(pageSize.getHeight(), pageSize.getWidth());
}
- if ("fitDocumentToImage".equals(fitOption)) {
+ if ("fitDocumentToImage".equals(fitOption) || "fitDocumentToPage".equals(fitOption)) {
pageSize = new PDRectangle(image.getWidth(), image.getHeight());
}
@@ -505,7 +505,9 @@ public class PdfUtils {
try (PDPageContentStream contentStream =
new PDPageContentStream(doc, page, AppendMode.APPEND, true, true)) {
- if ("fillPage".equals(fitOption) || "fitDocumentToImage".equals(fitOption)) {
+ if ("fillPage".equals(fitOption)
+ || "fitDocumentToImage".equals(fitOption)
+ || "fitDocumentToPage".equals(fitOption)) {
contentStream.drawImage(image, 0, 0, pageWidth, pageHeight);
} else if ("maintainAspectRatio".equals(fitOption)) {
float imageAspectRatio = (float) image.getWidth() / (float) image.getHeight();
diff --git a/app/common/src/test/java/stirling/software/common/util/PdfUtilsGapTest.java b/app/common/src/test/java/stirling/software/common/util/PdfUtilsGapTest.java
index b0f5c750ac..74e8963d61 100644
--- a/app/common/src/test/java/stirling/software/common/util/PdfUtilsGapTest.java
+++ b/app/common/src/test/java/stirling/software/common/util/PdfUtilsGapTest.java
@@ -351,6 +351,26 @@ class PdfUtilsGapTest {
}
}
+ @Test
+ @DisplayName("fitDocumentToPage sizes the page to the image and non-empty PDF")
+ void fitDocumentToPage() throws IOException {
+ MockMultipartFile file =
+ new MockMultipartFile(
+ "file",
+ "image.png",
+ MediaType.IMAGE_PNG_VALUE,
+ imageBytes("png", Color.GREEN));
+
+ byte[] pdfOut = runImageToPdf(new MultipartFile[] {file}, "fitDocumentToPage", false);
+
+ try (PDDocument doc = org.apache.pdfbox.Loader.loadPDF(pdfOut)) {
+ assertEquals(1, doc.getNumberOfPages());
+ PDRectangle box = doc.getPage(0).getMediaBox();
+ assertEquals(20f, box.getWidth(), 0.5f);
+ assertEquals(20f, box.getHeight(), 0.5f);
+ }
+ }
+
@Test
@DisplayName("multiple images become multiple pages")
void multipleImages() throws IOException {
@@ -424,6 +444,17 @@ class PdfUtilsGapTest {
}
}
+ @Test
+ @DisplayName("fitDocumentToPage sizes the page to the image bounds")
+ void fitDocumentToPage() throws IOException {
+ try (PDDocument doc = new PDDocument()) {
+ PdfUtils.addImageToDocument(doc, portraitImage(doc), "fitDocumentToPage", false);
+ PDRectangle box = doc.getPage(0).getMediaBox();
+ assertEquals(40f, box.getWidth(), 0.5f);
+ assertEquals(80f, box.getHeight(), 0.5f);
+ }
+ }
+
@Test
@DisplayName("autoRotate with a landscape image swaps to landscape A4")
void autoRotateLandscape() throws IOException {
diff --git a/app/core/src/main/java/stirling/software/SPDF/model/api/converters/ConvertToPdfRequest.java b/app/core/src/main/java/stirling/software/SPDF/model/api/converters/ConvertToPdfRequest.java
index c3b059fe0e..9b2fb90ad4 100644
--- a/app/core/src/main/java/stirling/software/SPDF/model/api/converters/ConvertToPdfRequest.java
+++ b/app/core/src/main/java/stirling/software/SPDF/model/api/converters/ConvertToPdfRequest.java
@@ -20,7 +20,12 @@ public class ConvertToPdfRequest {
description = "Option to determine how the image will fit onto the page",
requiredMode = Schema.RequiredMode.REQUIRED,
defaultValue = "fillPage",
- allowableValues = {"fillPage", "fitDocumentToImage", "maintainAspectRatio"})
+ allowableValues = {
+ "fillPage",
+ "fitDocumentToImage",
+ "fitDocumentToPage",
+ "maintainAspectRatio"
+ })
private String fitOption;
@Schema(
diff --git a/engine/src/stirling/models/tool_models.py b/engine/src/stirling/models/tool_models.py
index ce0d6eb861..24560190f9 100644
--- a/engine/src/stirling/models/tool_models.py
+++ b/engine/src/stirling/models/tool_models.py
@@ -572,6 +572,7 @@ class FitOption(StrEnum):
fill_page = "fillPage"
fit_document_to_image = "fitDocumentToImage"
+ fit_document_to_page = "fitDocumentToPage"
maintain_aspect_ratio = "maintainAspectRatio"
diff --git a/frontend/editor/src/core/types/toolApiTypes.ts b/frontend/editor/src/core/types/toolApiTypes.ts
index 798ca8fd28..7476bc428b 100644
--- a/frontend/editor/src/core/types/toolApiTypes.ts
+++ b/frontend/editor/src/core/types/toolApiTypes.ts
@@ -396,7 +396,11 @@ export interface ConvertToPdfRequest {
/**
* Option to determine how the image will fit onto the page
*/
- fitOption?: "fillPage" | "fitDocumentToImage" | "maintainAspectRatio";
+ fitOption?:
+ | "fillPage"
+ | "fitDocumentToImage"
+ | "fitDocumentToPage"
+ | "maintainAspectRatio";
}
export interface CropPdfForm {
/**