Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,24 +11,22 @@ class PDFTransformer:
|
|
| 11 |
rect = page.rect
|
| 12 |
original_width, original_height = rect.width, rect.height
|
| 13 |
|
| 14 |
-
#
|
| 15 |
scale_factor = min(width / original_width, height / original_height)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
scaled_width = original_width * scale_factor
|
| 19 |
scaled_height = original_height * scale_factor
|
| 20 |
|
| 21 |
-
#
|
| 22 |
x_offset = (width - scaled_width) / 2
|
| 23 |
y_offset = (height - scaled_height) / 2
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
page.set_cropbox(fitz.Rect(0, 0, width, height)) # Ensure cropbox matches the new size
|
| 31 |
-
page.apply_transform(matrix) # Scale the content
|
| 32 |
page.set_cropbox(fitz.Rect(x_offset, y_offset, x_offset + scaled_width, y_offset + scaled_height))
|
| 33 |
|
| 34 |
def save_to_pdf(self, output_path):
|
|
|
|
| 11 |
rect = page.rect
|
| 12 |
original_width, original_height = rect.width, rect.height
|
| 13 |
|
| 14 |
+
# Calculate the scaling factor to keep the aspect ratio
|
| 15 |
scale_factor = min(width / original_width, height / original_height)
|
| 16 |
|
| 17 |
+
# New dimensions based on scale factor
|
| 18 |
scaled_width = original_width * scale_factor
|
| 19 |
scaled_height = original_height * scale_factor
|
| 20 |
|
| 21 |
+
# Calculate offsets to center the content on the new page size
|
| 22 |
x_offset = (width - scaled_width) / 2
|
| 23 |
y_offset = (height - scaled_height) / 2
|
| 24 |
|
| 25 |
+
# Set the mediabox and cropbox to the target dimensions
|
| 26 |
+
page.set_mediabox(fitz.Rect(0, 0, width, height))
|
| 27 |
+
page.set_cropbox(fitz.Rect(0, 0, width, height))
|
| 28 |
+
|
| 29 |
+
# Adjust cropbox to center scaled content
|
|
|
|
|
|
|
| 30 |
page.set_cropbox(fitz.Rect(x_offset, y_offset, x_offset + scaled_width, y_offset + scaled_height))
|
| 31 |
|
| 32 |
def save_to_pdf(self, output_path):
|