34 lines
958 B
Python
34 lines
958 B
Python
import fitz # PyMuPDF
|
|
from pathlib import Path
|
|
from utils.text_metrics import get_text_width
|
|
|
|
def add_date(input_path, output_path, month_year, font_path, position=("right", 30), font_size=22):
|
|
doc = fitz.open(input_path)
|
|
font_path = str(Path(font_path).resolve())
|
|
|
|
for page in doc:
|
|
page_width = page.rect.width
|
|
|
|
# Register font and get its name
|
|
page.insert_font(fontname="custom", fontfile=font_path)
|
|
font_name = "custom"
|
|
|
|
# Measure exact pixel width
|
|
text_width = get_text_width(month_year, font_path, font_size)
|
|
|
|
# Calculate X position
|
|
x = page_width - text_width - 40 if position[0] == "right" else position[0]
|
|
y = position[1]
|
|
|
|
# Insert the text
|
|
page.insert_text(
|
|
(x, y),
|
|
month_year,
|
|
fontname=font_name,
|
|
fontsize=font_size,
|
|
color=(0, 0, 0),
|
|
)
|
|
|
|
doc.save(output_path)
|
|
doc.close()
|