pub struct PdfBuilder { /* private fields */ }Expand description
Fluent builder for generating PDFs.
All settings have sensible defaults — you can generate a PDF with just
PdfBuilder::new().from_markdown(…). Customise anything you want
via the chainable setter methods.
§From Markdown
use pdfsmith::PdfBuilder;
let pdf = PdfBuilder::new()
.from_markdown("# Hello")
.unwrap();§From Markdown with custom CSS
use pdfsmith::PdfBuilder;
let pdf = PdfBuilder::new()
.custom_css("body { font-family: Georgia; font-size: 12pt; }")
.from_markdown("# Styled")
.unwrap();§From JSON
use pdfsmith::PdfBuilder;
let json = serde_json::json!([
{ "type": "heading", "level": 1, "text": "Report" },
{ "type": "paragraph", "text": "Revenue grew **20%**." },
{ "type": "table", "headers": ["Q","Rev"], "rows": [["Q1","$1M"]] }
]);
let pdf = PdfBuilder::new()
.from_json(&json)
.unwrap();Implementations§
Source§impl PdfBuilder
impl PdfBuilder
Sourcepub fn with_config(config: PdfConfig) -> Self
pub fn with_config(config: PdfConfig) -> Self
New builder from an existing PdfConfig.
Sourcepub fn title(self, title: impl Into<String>) -> Self
pub fn title(self, title: impl Into<String>) -> Self
Set the document title (used in <title> and default header).
Sourcepub fn custom_css(self, css: impl Into<String>) -> Self
pub fn custom_css(self, css: impl Into<String>) -> Self
Replace the default CSS with your own stylesheet.
Sourcepub fn extra_css(self, css: impl Into<String>) -> Self
pub fn extra_css(self, css: impl Into<String>) -> Self
Append extra CSS after the base stylesheet.
Sourcepub fn paper_size(self, size: PaperSize) -> Self
pub fn paper_size(self, size: PaperSize) -> Self
Set paper size — A4, Letter, Legal, or Custom { width, height }.
Sourcepub fn margins(self, margins: PageMargins) -> Self
pub fn margins(self, margins: PageMargins) -> Self
Set page margins (in inches).
Sourcepub fn header(self, header: HeaderConfig) -> Self
pub fn header(self, header: HeaderConfig) -> Self
Configure the page header (left / center / right text, or custom HTML).
Configure the page footer (left / center / right text, or custom HTML).
Show or hide header & footer. Default: false.
Sourcepub fn print_background(self, yes: bool) -> Self
pub fn print_background(self, yes: bool) -> Self
Print background colours/images. Default: true.
Sourcepub fn markdown_options(self, opts: MarkdownOptions) -> Self
pub fn markdown_options(self, opts: MarkdownOptions) -> Self
Configure Markdown parsing extensions.
Sourcepub fn chrome_window_size(self, width: u32, height: u32) -> Self
pub fn chrome_window_size(self, width: u32, height: u32) -> Self
Chrome window size. Default: (1280, 900).
Sourcepub fn page_load_wait_secs(self, secs: u64) -> Self
pub fn page_load_wait_secs(self, secs: u64) -> Self
Seconds to wait after page load. Default: 2.
Sourcepub fn heading_numbers(self, yes: bool) -> Self
pub fn heading_numbers(self, yes: bool) -> Self
Enable automatic hierarchical heading numbers (1, 1.1, 1.1.1, …) via CSS counters. Works with all input types.
Sourcepub fn from_json(self, json_doc: &Value) -> Result<Vec<u8>>
pub fn from_json(self, json_doc: &Value) -> Result<Vec<u8>>
Generate a PDF from structured JSON content blocks.
The JSON is converted to Markdown first, then rendered with the
same pipeline as from_markdown.
§JSON Structure
An array of content blocks (or an object with a "content" array):
[
{ "type": "heading", "level": 1, "text": "Title" },
{ "type": "paragraph", "text": "Some **bold** text." },
{ "type": "code", "language": "rust", "text": "fn main() {}" },
{ "type": "list", "ordered": false, "items": ["A", "B", "C"] },
{ "type": "quote", "text": "A wise quote." },
{ "type": "table", "headers": ["X","Y"], "rows": [["1","2"]] },
{ "type": "image", "src": "https://…", "alt": "photo" },
{ "type": "divider" },
{ "type": "html", "text": "<b>raw HTML</b>" }
]Sourcepub fn from_html(self, html: &str) -> Result<Vec<u8>>
pub fn from_html(self, html: &str) -> Result<Vec<u8>>
Generate a PDF from a pre-built HTML string.
The HTML is used as-is — no Markdown conversion, no CSS injection. Header/footer and Chrome options still apply.