SathvikGanta commited on
Commit
4638a07
·
verified ·
1 Parent(s): e62685b

Update modify_pdf.py

Browse files
Files changed (1) hide show
  1. modify_pdf.py +11 -29
modify_pdf.py CHANGED
@@ -1,24 +1,24 @@
1
  import fitz # PyMuPDF
2
 
3
- def transform_pdf(input_pdf, output_pdf, scale_factor=1.2):
4
  """
5
- Emulate the CorelDRAW Pick Tool by scaling all elements in a PDF.
6
-
7
  Args:
8
- input_pdf (str): Path to the input PDF file.
9
  output_pdf (str): Path to save the modified PDF.
10
- scale_factor (float): Factor to scale all elements (width, height, and thickness).
 
11
  """
12
  doc = fitz.open(input_pdf)
13
 
14
  for page in doc:
15
- # Scale all drawable elements
16
  try:
17
- # Scale vector paths
18
  for shape in page.get_drawings():
19
  for path in shape["items"]:
20
- # Scale the bounding box of the shape
21
  if "bbox" in path:
 
22
  x0, y0, x1, y1 = path["bbox"]
23
  path["bbox"] = (
24
  x0 * scale_factor,
@@ -26,29 +26,11 @@ def transform_pdf(input_pdf, output_pdf, scale_factor=1.2):
26
  x1 * scale_factor,
27
  y1 * scale_factor,
28
  )
29
- # Adjust line thickness
30
  if "line_width" in path:
31
- path["line_width"] *= scale_factor
32
-
33
- # Scale text bounding boxes
34
- for text_instance in page.search_for(" "): # Use placeholder for locating text
35
- bbox = text_instance
36
- x0, y0, x1, y1 = bbox
37
- new_width = (x1 - x0) * scale_factor
38
- new_height = (y1 - y0) * scale_factor
39
- new_x0, new_y0 = x0 * scale_factor, y0 * scale_factor
40
-
41
- # Replace text with scaled dimensions
42
- page.delete_text(text_instance)
43
- text = page.get_text("text", clip=text_instance)
44
- page.insert_text(
45
- (new_x0, new_y0),
46
- text,
47
- fontsize=new_height, # Approximating font size with height
48
- overlay=True,
49
- )
50
  except Exception as e:
51
- print(f"Error scaling elements on page {page.number + 1}: {e}")
52
 
53
  doc.save(output_pdf)
54
  print(f"Modified PDF saved to {output_pdf}")
 
1
  import fitz # PyMuPDF
2
 
3
+ def scale_vector_text(input_pdf, output_pdf, scale_factor=1.2, thickness_factor=1.2):
4
  """
5
+ Scale and adjust thickness of vector-based text in a PDF.
6
+
7
  Args:
8
+ input_pdf (str): Path to the input PDF.
9
  output_pdf (str): Path to save the modified PDF.
10
+ scale_factor (float): Factor to scale the size of text.
11
+ thickness_factor (float): Factor to adjust the thickness of text.
12
  """
13
  doc = fitz.open(input_pdf)
14
 
15
  for page in doc:
16
+ # Process vector paths for size and thickness adjustments
17
  try:
 
18
  for shape in page.get_drawings():
19
  for path in shape["items"]:
 
20
  if "bbox" in path:
21
+ # Scale bounding box for size
22
  x0, y0, x1, y1 = path["bbox"]
23
  path["bbox"] = (
24
  x0 * scale_factor,
 
26
  x1 * scale_factor,
27
  y1 * scale_factor,
28
  )
29
+ # Adjust stroke thickness
30
  if "line_width" in path:
31
+ path["line_width"] *= thickness_factor
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  except Exception as e:
33
+ print(f"Error processing vector paths on page {page.number + 1}: {e}")
34
 
35
  doc.save(output_pdf)
36
  print(f"Modified PDF saved to {output_pdf}")