{"slug": "script-to-add-a-title-page-to-pdfs", "title": "Script to Add a Title Page to PDFs", "summary": "The article presents a Python script that automatically adds a title page to PDF documents, specifically designed for use with PDFs generated from ChatGPT conversations. The script uses the `fpdf2` library to create a temporary title page with a title and optional subtitle, then merges it with the original PDF using the `pdftk` command-line tool. It includes a command-line interface for specifying the input PDF, output file, title text, and an optional subtitle.", "body_md": "Script to Add a Title Page to PDFs\nI'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.\ntitle4pdf.py\n:\nimport os\nimport tempfile\nimport subprocess\nfrom fpdf import FPDF\ndef add_title_page(input_pdf, output_pdf, title, subtitle=None):\n# Create temporary title page PDF\nwith tempfile.NamedTemporaryFile(suffix=\".pdf\", delete=False) as temp_pdf:\n# Create title page with fpdf2\npdf = FPDF()\npdf.add_page()\npdf.set_font(\"Helvetica\", \"B\", 24)\n# Calculate centered position for title\npdf.cell(0, 40, text=\"\", new_x=\"LMARGIN\", new_y=\"NEXT\") # Add vertical spacing\npdf.multi_cell(0, 10, text=title, align=\"C\")\n# Add subtitle if provided\nif subtitle:\npdf.set_font(\"Helvetica\", \"\", 16)\npdf.ln(10) # Space between title and subtitle\npdf.multi_cell(0, 10, text=subtitle, align=\"C\")\n# Save temporary title page\npdf.output(temp_pdf.name)\ntry:\n# Merge PDFs using pdftk\nsubprocess.run([\n\"pdftk\",\ntemp_pdf.name,\ninput_pdf,\n\"cat\",\n\"output\",\noutput_pdf\n], check=True)\nfinally:\n# Clean up temporary file\nos.remove(temp_pdf.name)\nif __name__ == \"__main__\":\nimport argparse\nparser = argparse.ArgumentParser(description=\"Add title page to PDF\")\nparser.add_argument(\"input\", help=\"Input PDF file\")\nparser.add_argument(\"title\", help=\"Title text\")\nparser.add_argument(\"output\", help=\"Output PDF file\")\nparser.add_argument(\"--subtitle\", help=\"Subtitle text (optional)\")\nargs = parser.parse_args()\nadd_title_page(args.input, args.output, args.title, args.subtitle)", "url": "https://wpnews.pro/news/script-to-add-a-title-page-to-pdfs", "canonical_source": "https://nand2mario.github.io/posts/2025/add_title_page_to_pdf/", "published_at": "2025-03-10 00:00:00+00:00", "updated_at": "2026-05-23 15:54:44.908216+00:00", "lang": "en", "topics": ["developer-tools", "open-source"], "entities": ["ChatGPT", "Chrome", "Python", "FPDF"], "alternates": {"html": "https://wpnews.pro/news/script-to-add-a-title-page-to-pdfs", "markdown": "https://wpnews.pro/news/script-to-add-a-title-page-to-pdfs.md", "text": "https://wpnews.pro/news/script-to-add-a-title-page-to-pdfs.txt", "jsonld": "https://wpnews.pro/news/script-to-add-a-title-page-to-pdfs.jsonld"}}