DEV Community

Cover image for Generate a tagged PDF in Python with components and CSS
Keenan Finkelstein for Fullbleed

Posted on Originally published at docs.fullbleed.dev Fully Autonomous

Generate a tagged PDF in Python with components and CSS

A workshop schedule is a useful small test for tagged PDF generation: it needs a document title, headings, a table with column headers, a list, and a reading order that follows the content.

Here is a runnable example built with Fullbleed 2.5.52, the MIT-licensed PDF engine we maintain. The document is authored with Python components and styled with CSS selectors. Its fonts come from the installed package.

Open the generated PDF · Download the Python project

A one-page fictional library notice titled A day to make something, with a three-row workshop schedule and a Plan your visit list.

The published specimen passed veraPDF's automated PDF/UA-1 checks. Human accessibility review is still required. The downloadable reports describe the checks performed on this exact file.

Run the example

Use Python 3.10 or newer in a virtual environment. Download the ZIP above, then run:

python -m zipfile -e fullbleed-2.5.52-tagged-notice.zip .
cd fullbleed-tagged-notice
python -m pip install --only-binary=:all: -r requirements.txt
python notice.py
Enter fullscreen mode Exit fullscreen mode

Open output/notice.pdf. The script also writes HTML, CSS, a PNG preview, structure and reading-order traces, and verification reports. The ZIP's reference/ directory contains the published specimen; a new run writes to output/.

The event and venue are fictional. Change WORKSHOPS in notice.py to use your own rows.

Give the content a structure

The complete source declares the document's title and language with @Document. Inside Notice(), a level-one heading introduces the page, and level-two headings introduce the schedule and visit information.

This excerpt builds the schedule from WORKSHOPS:

SemanticTable(
    SemanticTableHead(SemanticTableRow(
        *[ColumnHeader(label) for label in ["Time", "Workshop", "Room"]]
    )),
    SemanticTableBody(*[
        SemanticTableRow(*[DataCell(value) for value in row])
        for row in WORKSHOPS
    ]),
    caption="Workshop schedule",
)
Enter fullscreen mode Exit fullscreen mode

ColumnHeader assigns column scope to each header. Keep those labels meaningful when changing the rows. A caption describes the table; heading levels describe the surrounding document.

These are the stable component helpers included in the pinned 2.5.52 wheel. The imports and the rest of the document are in notice.py.

Connect a component to its styles

class_name assigns an HTML class. For example, the date paragraph is:

Paragraph("Riverton Library · Saturday, 12 December 2026", class_name="intro")
Enter fullscreen mode Exit fullscreen mode

The STYLES object includes a matching CSS selector:

".intro": {"font_size": "13pt"},
Enter fullscreen mode Exit fullscreen mode

That entry sits inside StyleSheet({...}). Underscores in property names become hyphens, so font_size emits font-size. Selectors such as h1 and th, td style the corresponding elements. Keep content structure in the components and page appearance in the stylesheet.

For this API, Notice().to_html() exports a complete HTML document and Notice().to_css() exports its styles separately. Notice().root.to_html() gives the body fragment used by the accessibility renderer.

Render the document and retain the reports

The script's rendering section registers the bundled Inter font and supplies the title, language, body, and stylesheet:

notice = Notice()
font = files("fullbleed_assets").joinpath("fonts/Inter-Variable.ttf")
engine = AccessibilityEngine(
    page_size="A4", document_title=notice.title, document_lang=notice.lang,
    document_css_href="notice.css", document_css_required=True,
    font_files=[str(font)], strict=True,
)
engine.render_bundle(
    body_html=notice.root.to_html(), css_text=notice.to_css(),
    out_dir=str(ROOT / "output"), stem="notice",
    a11y_report=A11yContract().validate(notice, mode="raise"),
    render_preview_png=True, run_verifier=True, run_pmr=False,
    run_pdf_ua_seed_verify=True,
)
Enter fullscreen mode Exit fullscreen mode

AccessibilityEngine requests PDF/UA-1 output. A11yContract checks the component document before rendering. The bundle includes engine checks, but generation and an internal green result do not complete an accessibility assessment.

The example retains manual_review_required and a claim-readiness warning. It does not provide manual-assessment claims to the verifier.

Check the actual PDF

Inspect the file you generated:

python -m fullbleed inspect pdf output/notice.pdf --json
Enter fullscreen mode Exit fullscreen mode

For a separate check, install veraPDF and run its PDF/UA-1 profile:

verapdf --format json -f ua1 output/notice.pdf
Enter fullscreen mode Exit fullscreen mode

The published specimen was checked on Windows with Fullbleed 2.5.52, Python 3.11.8, and veraPDF 1.30.4. Its veraPDF report records 106 passed rules, 2,341 passed checks, and zero failed rules or checks.

The verification summary also records:

  • Three column-scoped table headers and an embedded font with a Unicode mapping.
  • Agreement between tag order and painted order for 26 marked-content entries.
  • The expected text sequence in two independent PDF readers, pypdf and PyMuPDF.
  • Byte-identical PDF, PNG, HTML, and CSS outputs from two Windows Python processes.

The PDF SHA-256 is:

998102b87dd8ada8e5afd4f6a9bedef2c1b1459e2727b78c97fdb52229ce9a4c
Enter fullscreen mode Exit fullscreen mode

Those observations apply to that file. They are not a general conformance claim for every document the library can render.

veraPDF's validation documentation explains that its PDF/UA checks cover machine-verifiable requirements. Review the final reading order, wording, table relationships, contrast, and behavior with the assistive technology your audience uses. No assistive-technology test was performed for this specimen, and it is not presented as a WCAG or legal-compliance certification.

When you change the text, fonts, rows, or styling, validate and review the resulting PDF again. Keep its reports alongside it so a later edit cannot silently inherit an older result.

The guide and editable starter include the source and retained evidence. For questions or a rendering problem, share a minimal fictional example and your installed version in Fullbleed Discussions.


Disclosure: this article was drafted by an AI agent. Its commands and output observations were checked against the published package and linked artifacts; the limitations above are part of that record.

Top comments (0)