Skip to content

Support document variants with zero code #19

@dannyadair

Description

@dannyadair

A document creating a single PDF does not need any coding.
When a document needs to create multiple PDFs for variants of itself, a bit of coding is currently still required:

If the document project contains a bake.py which defines process_document(), this will be called instead of the standard processing.

For Kiwi PyCon, we create "Material Specs" for sponsors in multiple variants, one for each sponsorship tier.
The project contains a bake.py:

"""Material specs document processor."""

from pdfbaker.document import PDFBakerDocument


def process_document(document: PDFBakerDocument) -> None:
    """Process and generate one material specs document for each tier."""
    document_name = document.name
    document_config = document.config.copy()

    for tier in document_config["tiers"]:
        document.baker.info("Processing tier %s", tier["name"])
        document.config = document_config.copy()
        document.config.update(
            {
                "tier": tier,
                "filename": f"{document_config['base_filename']} - {tier['name']}",
                "pages": tier["pages"],
                "sponsor_level": tier["sponsor_level"],
                "specs_items": tier.get("specs_items", []),
            }
        )
        document.name = f"{document_name}_{tier['name']}"  # Use original name
        document.process()

This can be generalised into pdfbaker itself to have variants with zero code.

Introduce a naming convention for a document's YAML configuration:

  • "documents" list of documents, can be string or new object with "name" and (optionally) "variants"
  • "variants" basically what we have as "sponsorship_tiers": they have their own "pages", "filename" and any other config
  • variant config is merged with (already merged) document config for the template context - one document per variant

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions