#!/usr/bin/env python3 import sys import os # Add the virtual environment to the path sys.path.insert(0, '/home/rpm/claude/ryanmalloy.com/static-files/documents/pdf_env/lib/python3.12/site-packages') import fitz # PyMuPDF import urllib.request def edit_pdf_text_precise(output_path): """Edit PDF text using PyMuPDF to correct the Intouch Solutions end date.""" print("Downloading original PDF from static server...") urllib.request.urlretrieve("https://static.ryanmalloy.com/documents/Ryan_Malloy_Resume.pdf", "original_resume.pdf") # Open the PDF doc = fitz.open("original_resume.pdf") print(f"PDF has {len(doc)} pages") text_found = False # Process each page for page_num in range(len(doc)): page = doc.load_page(page_num) print(f"Processing page {page_num + 1}") # Get text instances to find and replace text_instances = page.search_for("02/2001 - 11/2002") if text_instances: print(f"Found target text on page {page_num + 1}") text_found = True # For each instance found for inst in text_instances: print(f"Found text at position: {inst}") # Get the text blocks to understand formatting better blocks = page.get_text("dict") # Find the right block and span with proper formatting replacement_font_size = 10 # default replacement_font = "helv" # default for block in blocks["blocks"]: if "lines" in block: for line in block["lines"]: for span in line["spans"]: span_bbox = fitz.Rect(span["bbox"]) # Check if this span intersects with our target text if span_bbox.intersects(inst): replacement_font_size = span["size"] replacement_font = span["font"] print(f"Using font: {replacement_font}, size: {replacement_font_size}") break # Create a white rectangle to cover the old text white_rect = fitz.Rect(inst.x0, inst.y0, inst.x1, inst.y1) page.draw_rect(white_rect, color=None, fill=(1, 1, 1), width=0) # Insert the corrected text at the same position insertion_point = fitz.Point(inst.x0, inst.y1 - 2) # Slightly above baseline page.insert_text( insertion_point, "02/2001 - 11/2003", fontsize=replacement_font_size, fontname=replacement_font, color=(0, 0, 0) # Black text ) if not text_found: print("Target text '02/2001 - 11/2002' was not found in the PDF") # Save the modified PDF doc.save(output_path) doc.close() print(f"Modified PDF saved to: {output_path}") if __name__ == "__main__": edit_pdf_text_precise("Ryan_Malloy_Resume_corrected_precise.pdf")