# dhvanibhesaniya/pdfsmith architecture

> Architecture diagram of the GitHub repository dhvanibhesaniya/pdfsmith, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dhvanibhesaniya/pdfsmith
- Repository: https://github.com/dhvanibhesaniya/pdfsmith

## Overview

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: [`src/lib.rs`](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/lib.rs) (PdfBuilder), [`src/renderer/html.rs`](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/renderer/html.rs) (HTML doc), [`src/parser/mod.rs`](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/parser/mod.rs) (Parser mod), [`src/renderer/mod.rs`](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/renderer/mod.rs) (Renderer mod)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_src["src"]
  node_lib["PdfBuilder<br/>public API<br/>[lib.rs]"]
  node_config["Config<br/>settings<br/>[config.rs]"]
  node_error(("PdfError<br/>error model<br/>[error.rs]"))
  node_css["Default CSS<br/>style base<br/>[css.rs]"]
end

subgraph group_parser["parser"]
  node_md_parser["Markdown<br/>parser<br/>[markdown.rs]"]
  node_json_parser["JSON<br/>parser<br/>[json.rs]"]
  node_parser_mod["Parser mod<br/>routing<br/>[mod.rs]"]
end

subgraph group_renderer["renderer"]
  node_html_renderer["HTML doc<br/>assembly<br/>[html.rs]"]
  node_template["Templates<br/>templating<br/>[template.rs]"]
  node_chrome["Chrome PDF<br/>backend<br/>[chrome.rs]"]
  node_renderer_mod["Renderer mod<br/>orchestration<br/>[mod.rs]"]
end

subgraph group_usage["usage"]
  node_from_markdown["Markdown example<br/>[from_markdown.rs]"]
  node_from_json["JSON example<br/>[from_json.rs]"]
  node_full_document["Full doc example<br/>[full_document.rs]"]
  node_custom_css["CSS example<br/>[custom_css.rs]"]
  node_basic_test["Basic test<br/>[basic.rs]"]
  node_json_test["JSON test<br/>[json.rs]"]
end

node_lib -->|"re-exports"| node_config
node_lib -->|"re-exports"| node_error
node_lib -->|"routes input"| node_parser_mod
node_lib -->|"renders via"| node_renderer_mod
node_parser_mod -->|"markdown"| node_md_parser
node_parser_mod -->|"json"| node_json_parser
node_json_parser -->|"normalizes to HTML"| node_md_parser
node_md_parser -->|"HTML output"| node_html_renderer
node_html_renderer -->|"applies"| node_css
node_html_renderer -->|"reads"| node_config
node_html_renderer -->|"injects"| node_template
node_template -->|"uses"| node_config
node_renderer_mod -->|"builds"| node_html_renderer
node_renderer_mod -->|"prints"| node_chrome
node_chrome -->|"maps failures"| node_error
node_lib -->|"returns"| node_error
node_from_markdown -.->|"uses"| node_lib
node_from_json -.->|"uses"| node_lib
node_full_document -.->|"uses"| node_lib
node_custom_css -.->|"uses"| node_lib
node_basic_test -.->|"verifies"| node_lib
node_json_test -.->|"verifies"| node_lib

click node_lib "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/lib.rs"
click node_config "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/config.rs"
click node_error "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/error.rs"
click node_css "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/css.rs"
click node_md_parser "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/parser/markdown.rs"
click node_json_parser "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/parser/json.rs"
click node_parser_mod "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/parser/mod.rs"
click node_html_renderer "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/renderer/html.rs"
click node_template "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/renderer/template.rs"
click node_chrome "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/renderer/chrome.rs"
click node_renderer_mod "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/renderer/mod.rs"
click node_from_markdown "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/examples/from_markdown.rs"
click node_from_json "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/examples/from_json.rs"
click node_full_document "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/examples/full_document.rs"
click node_custom_css "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/examples/custom_css.rs"
click node_basic_test "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/tests/basic.rs"
click node_json_test "https://github.com/dhvanibhesaniya/pdfsmith/blob/main/tests/json.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_lib,node_config,node_error,node_css toneBlue
class node_md_parser,node_json_parser,node_parser_mod toneAmber
class node_html_renderer,node_template,node_chrome,node_renderer_mod toneMint
class node_from_markdown,node_from_json,node_full_document,node_custom_css,node_basic_test,node_json_test toneRose
```

## Components

### src

Library implementation

- [PdfBuilder](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/lib.rs): public API · Main builder entry point and re-exports · `src/lib.rs`
- [Config](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/config.rs): settings · Document and rendering options · `src/config.rs`
- [PdfError](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/error.rs): error model · Library-wide error type · `src/error.rs`
- [Default CSS](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/css.rs): style base · Built-in stylesheet and theme defaults · `src/css.rs`

### parser

Input transforms

- [Markdown](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/parser/markdown.rs): parser · Markdown to HTML transform · `src/parser/markdown.rs`
- [JSON](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/parser/json.rs): parser · Structured content to Markdown transform · `src/parser/json.rs`
- [Parser mod](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/parser/mod.rs): routing · Parser module wiring · `src/parser/mod.rs`

### renderer

HTML and PDF rendering

- [HTML doc](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/renderer/html.rs): assembly · Wraps content into a full HTML document · `src/renderer/html.rs`
- [Templates](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/renderer/template.rs): templating · Header and footer HTML templates · `src/renderer/template.rs`
- [Chrome PDF](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/renderer/chrome.rs): backend · Headless Chrome print-to-PDF runtime · `src/renderer/chrome.rs`
- [Renderer mod](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/src/renderer/mod.rs): orchestration · Renderer module wiring · `src/renderer/mod.rs`

### usage

Examples and tests

- [Markdown example](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/examples/from_markdown.rs): example · Basic Markdown usage · `examples/from_markdown.rs`
- [JSON example](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/examples/from_json.rs): example · Structured JSON usage · `examples/from_json.rs`
- [Full doc example](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/examples/full_document.rs): example · End-to-end document assembly · `examples/full_document.rs`
- [CSS example](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/examples/custom_css.rs): example · Custom styling override · `examples/custom_css.rs`
- [Basic test](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/tests/basic.rs): test · Core path validation · `tests/basic.rs`
- [JSON test](https://github.com/dhvanibhesaniya/pdfsmith/blob/main/tests/json.rs): test · JSON path validation · `tests/json.rs`

## Connections

- 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
