How dhvanibhesaniya/pdfsmith works
1. Purpose and outer boundary
pdfsmith is a Rust library crate, not an application. Its public surface is a builder-style API that turns one of three input forms—Markdown, structured JSON, or raw HTML—into PDF bytes. The output boundary is always Vec<u8>; the caller is responsible for writing the bytes to disk, sending them over HTTP, etc.
2. Public API layer
src/lib.rs is the main entry point. It re-exports the builder and the configuration/error types so consumers can configure the pipeline fluently. The architecture is centered on PdfBuilder, which selects the input source and applies rendering options.
3. Configuration subsystem
src/config.rs holds the document and rendering settings: paper size, margins, landscape mode, header/footer settings, CSS choice, heading numbering, Markdown extension toggles, Chrome viewport size, and page-load delay. This config layer is the main control plane for the entire pipeline.
4. Input parsing subsystem
The src/parser/ module contains two distinct transforms:
- markdown.rs converts Markdown into HTML using comrak.
- json.rs converts structured JSON content blocks into Markdown first, then into the same downstream HTML path.
This means Markdown and JSON converge early and share the same rendering path.
5. Raw HTML bypass path
Raw HTML is a separate input mode that skips Markdown/JSON conversion. It still goes through document wrapping and Chrome rendering, but the body content is passed through as-is. This is an important architectural boundary because it preserves full caller control over the HTML markup.
6. Document assembly subsystem
src/renderer/html.rs is responsible for wrapping content into a complete HTML document. It combines the body with the resolved stylesheet, document title, and page-level settings. This layer is where the library turns a content fragment into something headless Chrome can print.
7. Header/footer templating
src/renderer/template.rs builds the HTML for headers and footers. The config supports simple left/center/right text or fully custom HTML, and the templates can use Chrome page placeholders such as current page and total pages. This is a distinct rendering concern from the main document body.
8. PDF generation backend
src/renderer/chrome.rs is the final execution stage. It launches headless Chrome/Chromium via the headless_chrome-style workflow, loads the generated HTML, applies print options, waits for assets if needed, and exports PDF bytes. Chrome is therefore the runtime rendering engine and an external system dependency.
9. Styling model
src/css.rs exposes the default stylesheet, and the config supports either replacing it entirely or appending extra rules. This makes CSS a first-class architecture boundary: the library is opinionated by default, but the caller can fully override or extend the print theme.
10. Error boundary
src/error.rs centralizes failures into a library-specific error type. The main categories are Chrome launch/render issues, I/O errors, JSON shape errors, image download failures, and generic fallback errors. This keeps the public API consistent across the different input and rendering branches.
11. Example and test coverage
The examples/ directory shows the major usage modes: minimal Markdown, JSON documents, custom CSS, headers/footers, images, and full reports. The tests/ directory validates the basic and JSON paths. These files are useful as architectural references because they demonstrate the intended combinations of input, config, and rendering.
12. End-to-end data flow
The core pipeline is: input source → parser/transform → HTML assembly with CSS/templates/config → headless Chrome print → PDF bytes. Markdown and JSON share the same downstream renderer; raw HTML joins the pipeline later. The major external dependency boundary is the Chrome/Chromium runtime, which is required on the host machine or container.
Main entry points: lib.rs PdfBuilder · html.rs HTML doc · parser/mod.rs Parser mod · renderer/mod.rs Renderer mod
Components 17
src
Library implementation
- PdfBuilder · Main builder entry point and re-exports
src/lib.rs - Config · Document and rendering options
src/config.rs - PdfError · Library-wide error type
src/error.rs - Default CSS · Built-in stylesheet and theme defaults
src/css.rs
parser
Input transforms
- Markdown · Markdown to HTML transform
src/parser/markdown.rs - JSON · Structured content to Markdown transform
src/parser/json.rs - Parser mod · Parser module wiring
src/parser/mod.rs
renderer
HTML and PDF rendering
- HTML doc · Wraps content into a full HTML document
src/renderer/html.rs - Templates · Header and footer HTML templates
src/renderer/template.rs - Chrome PDF · Headless Chrome print-to-PDF runtime
src/renderer/chrome.rs - Renderer mod · Renderer module wiring
src/renderer/mod.rs
usage
Examples and tests
- Markdown example · Basic Markdown usage
examples/from_markdown.rs - JSON example · Structured JSON usage
examples/from_json.rs - Full doc example · End-to-end document assembly
examples/full_document.rs - CSS example · Custom styling override
examples/custom_css.rs - Basic test · Core path validation
tests/basic.rs - JSON test · JSON path validation
tests/json.rs
Connections 22
- PdfBuilder → Config: re-exports · Public configuration surface
- PdfBuilder → PdfError: re-exports · Public error surface
- PdfBuilder → Parser mod: routes input · Selects markdown, JSON, or raw HTML
- PdfBuilder → Renderer mod: renders via · Invokes document assembly and PDF backend
- Parser mod → Markdown: markdown · Markdown transform path
- Parser mod → JSON: json · JSON transform path
- JSON → Markdown: normalizes to HTML · JSON content is converted through Markdown into the shared rendering path
- Markdown → HTML doc: HTML output · Produces HTML fragments for document assembly
- HTML doc → Default CSS: applies · Resolves default or overridden stylesheet
- HTML doc → Config: reads · Uses page setup and document settings
- HTML doc → Templates: injects · Adds header and footer templates
- Templates → Config: uses · Reads header/footer configuration and placeholders
- Renderer mod → HTML doc: builds · Creates printable HTML document
- Renderer mod → Chrome PDF: prints · Sends HTML to headless Chrome for PDF export
- Chrome PDF → PdfError: maps failures · Converts backend and I/O failures into library errors
- PdfBuilder → PdfError: returns · Unified failure type for the public API
- Markdown example → PdfBuilder: uses · Example consumer path
- JSON example → PdfBuilder: uses · Example consumer path
- Full doc example → PdfBuilder: uses · Example consumer path
- CSS example → PdfBuilder: uses · Example consumer path
- Basic test → PdfBuilder: verifies · Core behavior coverage
- JSON test → PdfBuilder: verifies · JSON behavior coverage