Markdown to PDF
Generate reliable, modern Markdown-to-PDF workflows with explicit engine selection, versioned configuration, and deterministic outputs.
Critical
- Prefer
pandocas the baseline converter and show concrete commands. - Explicitly set formats (
--from,--to) and output (--output). - Prefer checked-in defaults files (
--defaults) over long ad-hoc CLI strings. - Choose engine deliberately with
--pdf-engineand explain trade-offs. - For multi-file inputs, enforce explicit file order.
- For untrusted input, include security guardrails (sandboxing + strict parsing choices).
- For reproducibility, include
SOURCE_DATE_EPOCHand deterministic metadata guidance. - For PDF/UA or PDF/A requests, include standards-specific guidance and external validation caveat.
Mandatory Intent Rules
For in-scope requests, never answer with generic prose only. Include the required signals for the detected intent:
- Multi-file merge request: Must mention explicit input ordering. Must mention deterministic output path. Must mention
--resource-pathor file-scope/resource discipline. - Shared local + CI config request: Must recommend a checked-in defaults file and mention
--defaults. - Engine comparison request: Must compare at least 3 engines. Must mention
--pdf-engineexplicitly. - Accessibility/archival request: Must mention PDF/UA or PDF/A path. Must include explicit external validation caveat.
- Reproducibility request: Must mention
SOURCE_DATE_EPOCH. Must mention deterministic input order and metadata/path controls. - Missing-tool automation request: Must provide graceful fallback behaviour (warn/skip or policy-driven fail). Must include remediation command, including
misewhen relevant.
When to Use
- Convert one or more Markdown files to PDF.
- Build a reusable local + CI conversion workflow.
- Compare engines like
lualatex,tectonic,weasyprint,typst, orprince. - Add stable branding via templates/CSS/variables.
- Improve safety for untrusted uploaded Markdown.
- Make outputs reproducible across machines/runs.
- Publish PDFs as CI artifacts.
When NOT to Use
- User asks only for Markdown editing with no PDF output goal.
- User asks unrelated questions (math, geography, general coding).
- Task is non-document automation unrelated to document conversion.
Response Contract
When the request is in scope, structure the answer so it is immediately actionable:
- Provide at least one concrete command.
- State selected engine and why.
- Include deterministic path/config guidance.
- Include security/reproducibility/standards guidance when relevant.
- Keep optional alternatives short and clearly labeled.
Use this compact output structure for in-scope prompts:
Recommended command(orRecommended defaults file)Why this engineDeterminism and CISecurity/standards caveats(only when relevant)
When relevant, include these literal tokens in the answer text:
--defaults--pdf-engineSOURCE_DATE_EPOCH--resource-pathPDF/UAorPDF/Aexternal validationmise
Core Workflow
- Discover capabilities:
pandoc --versionpandoc --list-output-formatspandoc --list-input-formats - Start with a deterministic baseline command.
- Move repeated options to a checked-in defaults file.
- Layer style and branding incrementally.
- Add CI/hook automation with graceful tool-missing behaviour.
- Add reproducibility controls for stable artifacts.
Baseline Commands
Single file with TOC and section numbering:
pandoc --from=markdown --to=pdf README.md \
--output docs/out/README.pdf \
--toc --number-sections
Multiple files with explicit order:
pandoc --from=markdown --to=pdf \
docs/intro.md docs/architecture.md docs/usage.md \
--output docs/out/guide.pdf \
--toc --number-sections
Defaults-File First
Prefer a versioned defaults file in the repo.
Example pdf.defaults.yaml:
from: markdown
to: pdf
pdf-engine: lualatex
table-of-contents: true
number-sections: true
output-file: docs/out/guide.pdf
resource-path:
- .
- docs/assets
Run:
pandoc --defaults pdf.defaults.yaml docs/intro.md docs/architecture.md docs/usage.md
Engine Selection Guidance
Always use --pdf-engine explicitly when giving recommendations.
lualatex: strongest general default for high-quality typography and broad Pandoc support.tectonic: strong CI ergonomics and simplified TeX dependency management.weasyprint: best when CSS-driven layout/web-print parity is required.typst: fast modern typesetting workflows and strong template ergonomics.prince: premium HTML/CSS-to-PDF engine with strong standards features.
Styling and Branding Strategy
Apply styling in this order to reduce brittleness:
- Metadata/variables (title, lang, margins, fonts)
- Defaults file
- Engine-level template/CSS/reference assets
- Minimal custom filters only when strictly necessary
Keep style artifacts versioned and path-stable. Avoid hidden user-local dependencies.
Accessibility and Archival Standards
For PDF/UA or PDF/A requirements:
- LaTeX path: use
lualatexandpdfstandardmetadata/variables where supported. - Typst path: use Typst PDF standard options when requested.
- HTML engines (
weasyprint,prince) can support standards/tagging modes, but capabilities vary by version/config. - State clearly that external conformance validation is mandatory.
Never claim compliance from conversion alone.
Security for Untrusted Markdown
When users upload Markdown:
- Prefer strict parse surface (for example
--from=commonmarkor constrained extensions). - Use
--sandboxfor pandoc IO restrictions. - Treat filters/templates as code: allow-list and review before execution.
- Run conversion in isolated runtime/container for defence in depth.
- Set resource and time limits for conversion jobs.
Reproducibility Controls
Use all of the following when deterministic outputs matter:
- Pin converter + engine versions in CI.
- Set
SOURCE_DATE_EPOCH. - Use fixed input ordering.
- Use stable output paths and checked-in defaults/template assets.
- Set explicit metadata fields (
title,date, identifiers when needed).
Automation Patterns
Git hooks (optional artifact)
If PDF generation is optional and tool is missing:
- warn clearly,
- skip artifact generation,
- continue primary workflow.
Include remediation (install command or mise bootstrap command).
CI artifacts
- Generate to deterministic output path (for example
docs/out/*.pdf). - Upload artifact from that exact path.
- Fail only when PDF is required by policy.
Tool-Missing Fallbacks
When pandoc is unavailable:
- Recommend bootstrap via project toolchain (for this repo,
mise). - Offer containerised fallback if local installation is undesirable.
- Keep behaviour explicit (skip/warn/fail policy).
References
Load these references for deeper, source-grounded detail: