Skip to main content

PdfBuilder

Struct PdfBuilder 

Source
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

Source

pub fn new() -> Self

New builder with default configuration.

Source

pub fn with_config(config: PdfConfig) -> Self

New builder from an existing PdfConfig.

Source

pub fn config(self, config: PdfConfig) -> Self

Replace the entire configuration.

Source

pub fn title(self, title: impl Into<String>) -> Self

Set the document title (used in <title> and default header).

Source

pub fn custom_css(self, css: impl Into<String>) -> Self

Replace the default CSS with your own stylesheet.

Source

pub fn extra_css(self, css: impl Into<String>) -> Self

Append extra CSS after the base stylesheet.

Source

pub fn paper_size(self, size: PaperSize) -> Self

Set paper size — A4, Letter, Legal, or Custom { width, height }.

Source

pub fn margins(self, margins: PageMargins) -> Self

Set page margins (in inches).

Source

pub fn landscape(self, yes: bool) -> Self

Enable landscape orientation.

Source

pub fn header(self, header: HeaderConfig) -> Self

Configure the page header (left / center / right text, or custom HTML).

Source

pub fn footer(self, footer: FooterConfig) -> Self

Configure the page footer (left / center / right text, or custom HTML).

Show or hide header & footer. Default: false.

Source

pub fn print_background(self, yes: bool) -> Self

Print background colours/images. Default: true.

Source

pub fn markdown_options(self, opts: MarkdownOptions) -> Self

Configure Markdown parsing extensions.

Source

pub fn chrome_window_size(self, width: u32, height: u32) -> Self

Chrome window size. Default: (1280, 900).

Source

pub fn page_load_wait_secs(self, secs: u64) -> Self

Seconds to wait after page load. Default: 2.

Source

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.

Source

pub fn from_markdown(self, md: &str) -> Result<Vec<u8>>

Generate a PDF from a Markdown string.

Source

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>" }
]
Source

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.

Source

pub fn from_markdown_file(self, path: impl AsRef<Path>) -> Result<Vec<u8>>

Generate a PDF from a Markdown file on disk.

Source

pub fn from_json_file(self, path: impl AsRef<Path>) -> Result<Vec<u8>>

Generate a PDF from a JSON file on disk.

Trait Implementations§

Source§

impl Default for PdfBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V