Skip to content

New Feature: Add configurable HTML hook before PDF generation#128

Open
jdillard wants to merge 3 commits intouseblocks:mainfrom
jdillard:html-hook
Open

New Feature: Add configurable HTML hook before PDF generation#128
jdillard wants to merge 3 commits intouseblocks:mainfrom
jdillard:html-hook

Conversation

@jdillard
Copy link

@jdillard jdillard commented Jan 28, 2026

Adds a configuration option so users can run a custom Python script to modify the HTML (via BeautifulSoup) before PDF generation (e.g. remove, add, or change HTML elements).

Usage

# conf.py
simplepdf_html_hook = "./hooks/pdf_hook.py"

# hooks/pdf_hook.py — must define a function named html_hook
from bs4 import BeautifulSoup

def html_hook(soup, app):
    """soup: BeautifulSoup of the index HTML; app: Sphinx application. Return modified soup."""
    # e.g. remove nav, add watermark
    return soup

docs preview: https://sphinx-simplepdf--128.org.readthedocs.build/en/128/configuration.html#simplepdf-html-hook

Changes

  • Added simplepdf_html_hook config option, a path to a Python script
    • The script must define a function named html_hook(soup, app) that returns a BeautifulSoup object.
  • Added _load_html_hook() and _execute_html_hook() methods to the builder
  • Refactored _toctree_fix() to work with BeautifulSoup objects directly (used in the same pipeline as the hook)
  • Added configuration documentation and updated changelog

@jdillard jdillard marked this pull request as draft January 28, 2026 20:05
@jdillard jdillard changed the title Add hook to manipulate HTML Add configurable HTML hook before PDF generation Mar 18, 2026
@jdillard jdillard marked this pull request as ready for review March 18, 2026 01:13
@jdillard jdillard changed the title Add configurable HTML hook before PDF generation New Feature: Add configurable HTML hook before PDF generation Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant