Skip to content

Instantly share code, notes, and snippets.

@Rgghgh
Created May 10, 2024 07:29
Show Gist options
  • Select an option

  • Save Rgghgh/93b82b9a0bf724138f2ccac5ae00be3c to your computer and use it in GitHub Desktop.

Select an option

Save Rgghgh/93b82b9a0bf724138f2ccac5ae00be3c to your computer and use it in GitHub Desktop.
PDF overlay merger (file 1 is the base, file 2 is the overlay)
import fitz # Import PyMuPDF
def overlay_notes(original_pdf_path, notes_pdf_path, output_pdf_path):
# Open the original and notes documents
original_pdf = fitz.open(original_pdf_path)
notes_pdf = fitz.open(notes_pdf_path)
# Iterate through the pages and overlay notes onto the original
for page_number in range(len(original_pdf)):
original_page = original_pdf[page_number]
if page_number < len(notes_pdf): # Check if there's a corresponding notes page
notes_page = notes_pdf[page_number]
# Create a pixmap of the notes page with an alpha channel (transparency)
pixmap = notes_page.get_pixmap(alpha=True)
# Insert the pixmap onto the original page
original_page.insert_image(original_page.rect, pixmap=pixmap)
# Save the modified document to a new file
original_pdf.save(output_pdf_path)
# File paths
original_pdf_path = '1.pdf'
notes_pdf_path = '2.pdf'
output_pdf_path = 'merged_output.pdf'
# Function call to overlay the notes
overlay_notes(original_pdf_path, notes_pdf_path, output_pdf_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment