Script to Add a Title Page to PDFs I've recently found myself frequently using the "ChatGPT to PDF" Chrome extension to convert ChatGPT conversations into PDF documents. The Deep Research discussions in particular contain valuable info worth preserving in ebook format. However they lack proper title pages. So here's a quick Python script to add a simple title page to PDF documents. title4pdf.py :
import os
import tempfile
import subprocess
from fpdf import FPDF
def add_title_page(input_pdf, output_pdf, title, subtitle=None):
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", "B", 24)
pdf.cell(0, 40, text="", new_x="LMARGIN", new_y="NEXT") # Add vertical spacing
pdf.multi_cell(0, 10, text=title, align="C")
if subtitle:
pdf.set_font("Helvetica", "", 16)
pdf.ln(10) # Space between title and subtitle
pdf.multi_cell(0, 10, text=subtitle, align="C")
pdf.output(temp_pdf.name)
try: subprocess.run([ "pdftk",
temp_pdf.name,
input_pdf,
"cat", "output", output_pdf ], check=True) finally:
os.remove(temp_pdf.name)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Add title page to PDF")
parser.add_argument("input", help="Input PDF file")
parser.add_argument("title", help="Title text")
parser.add_argument("output", help="Output PDF file")
parser.add_argument("--subtitle", help="Subtitle text (optional)")
args = parser.parse_args()
add_title_page(args.input, args.output, args.title, args.subtitle)