SathvikGanta commited on
Commit
c49c88b
·
verified ·
1 Parent(s): 0083919

Update modify_pdf.py

Browse files
Files changed (1) hide show
  1. modify_pdf.py +35 -35
modify_pdf.py CHANGED
@@ -1,44 +1,44 @@
1
- import pdfplumber
2
- from reportlab.pdfgen import canvas
3
- from reportlab.lib.pagesizes import letter
4
- from PyPDF2 import PdfWriter, PdfReader
5
 
6
  def modify_pdf(input_pdf, output_pdf, scale_text=1.2, line_thickness=1.2):
7
  """
8
- Modify text size and thickness in a PDF, preserving vector data and layers.
 
9
  Args:
10
  input_pdf (str): Path to the input PDF file.
11
  output_pdf (str): Path to save the modified PDF.
12
  scale_text (float): Factor to scale text size.
13
  line_thickness (float): Factor to adjust text thickness.
14
  """
15
- # Step 1: Extract text and vector elements
16
- with pdfplumber.open(input_pdf) as pdf:
17
- pages = pdf.pages
18
- c = canvas.Canvas(output_pdf, pagesize=letter)
19
- for page_number, page in enumerate(pages):
20
- # Extract all text elements
21
- text_objects = page.extract_words()
22
- for text_obj in text_objects:
23
- # Scale text size
24
- x, y, width, height = text_obj["x0"], text_obj["top"], text_obj["x1"], text_obj["bottom"]
25
- new_size = height * scale_text
26
- c.setFont("Helvetica", new_size)
27
- c.drawString(x, y, text_obj["text"])
28
-
29
- # Optionally handle vector data (if available)
30
- # PDF vector paths modification can be added if pdfplumber provides access
31
-
32
- c.save()
33
-
34
- # Step 2: Merge with original PDF for vector layer preservation
35
- original = PdfReader(input_pdf)
36
- modified = PdfReader(output_pdf)
37
- writer = PdfWriter()
38
- for page in original.pages:
39
- writer.add_page(page)
40
- for page in modified.pages:
41
- writer.add_page(page)
42
- with open(output_pdf, "wb") as f:
43
- writer.write(f)
44
- print(f"Saved modified PDF to {output_pdf}")
 
 
 
1
+ import fitz # PyMuPDF
 
 
 
2
 
3
  def modify_pdf(input_pdf, output_pdf, scale_text=1.2, line_thickness=1.2):
4
  """
5
+ Modify text size and thickness in a PDF while preserving vector data and layers.
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_text (float): Factor to scale text size.
11
  line_thickness (float): Factor to adjust text thickness.
12
  """
13
+ doc = fitz.open(input_pdf)
14
+ for page in doc:
15
+ # Process text objects
16
+ try:
17
+ for text_instance in page.search_for(" "): # Use a space as placeholder to find text blocks
18
+ # Extract text properties
19
+ text = page.get_text("text", clip=text_instance)
20
+ bbox = text_instance
21
+ font_size = bbox[3] - bbox[1] # Approximation of text height
22
+
23
+ # Replace text with scaled size
24
+ page.delete_text(text_instance)
25
+ page.insert_text(
26
+ (bbox[0], bbox[1]),
27
+ text,
28
+ fontsize=font_size * scale_text,
29
+ overlay=True
30
+ )
31
+ except Exception as e:
32
+ print(f"Error modifying text size on page {page.number + 1}: {e}")
33
+
34
+ # Process vector paths for thickness
35
+ try:
36
+ for shape in page.get_drawings():
37
+ for path in shape["items"]:
38
+ if "line_width" in path:
39
+ path["line_width"] *= line_thickness
40
+ except Exception as e:
41
+ print(f"Error modifying text thickness on page {page.number + 1}: {e}")
42
+
43
+ doc.save(output_pdf)
44
+ print(f"Modified PDF saved to {output_pdf}")