Spaces:
Sleeping
Sleeping
Update modify_pdf.py
Browse files- modify_pdf.py +35 -35
modify_pdf.py
CHANGED
|
@@ -1,44 +1,44 @@
|
|
| 1 |
-
import
|
| 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
|
|
|
|
| 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 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
| 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}")
|