SathvikGanta commited on
Commit
e38af3f
·
verified ·
1 Parent(s): bbf1ed1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -10
app.py CHANGED
@@ -11,24 +11,22 @@ class PDFTransformer:
11
  rect = page.rect
12
  original_width, original_height = rect.width, rect.height
13
 
14
- # Determine the scaling factor to maintain aspect ratio
15
  scale_factor = min(width / original_width, height / original_height)
16
 
17
- # Calculate scaled width and height based on scale factor
18
  scaled_width = original_width * scale_factor
19
  scaled_height = original_height * scale_factor
20
 
21
- # Center the content on the new page dimensions
22
  x_offset = (width - scaled_width) / 2
23
  y_offset = (height - scaled_height) / 2
24
 
25
- # Create a transformation matrix
26
- matrix = fitz.Matrix(scale_factor, scale_factor)
27
-
28
- # Apply transformation to content and reposition
29
- page.set_mediabox(fitz.Rect(0, 0, width, height)) # Set page to target dimensions
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):