UC1_LastPart / modify_pdf.py
SathvikGanta's picture
Update modify_pdf.py
4638a07 verified
import fitz # PyMuPDF
def scale_vector_text(input_pdf, output_pdf, scale_factor=1.2, thickness_factor=1.2):
"""
Scale and adjust thickness of vector-based text in a PDF.
Args:
input_pdf (str): Path to the input PDF.
output_pdf (str): Path to save the modified PDF.
scale_factor (float): Factor to scale the size of text.
thickness_factor (float): Factor to adjust the thickness of text.
"""
doc = fitz.open(input_pdf)
for page in doc:
# Process vector paths for size and thickness adjustments
try:
for shape in page.get_drawings():
for path in shape["items"]:
if "bbox" in path:
# Scale bounding box for size
x0, y0, x1, y1 = path["bbox"]
path["bbox"] = (
x0 * scale_factor,
y0 * scale_factor,
x1 * scale_factor,
y1 * scale_factor,
)
# Adjust stroke thickness
if "line_width" in path:
path["line_width"] *= thickness_factor
except Exception as e:
print(f"Error processing vector paths on page {page.number + 1}: {e}")
doc.save(output_pdf)
print(f"Modified PDF saved to {output_pdf}")