Skip to content

PdfBuilder Fluent API

PdfBuilder provides a fluent configuration API for PDF creation. Chain methods to set page size, margins, font size, metadata, and more, then call a from_* method to generate the PDF.

Binding coverage. The fluent PdfBuilder / WasmPdfBuilder is exposed in Python, Rust, and WASM. The Go and C# bindings expose the shortcut entry points (FromMarkdown, FromHtml, FromImage / Pdf.FromMarkdown, Pdf.FromHtml, Pdf.FromText) but not the builder configuration surface. For custom margins or page sizes from Go or C#, call the Rust CLI with flags, or wrap a Rust helper that exposes the builder over FFI.

Quick Example

Python

from pdf_oxide import Pdf

# Python uses optional keyword arguments on Pdf static methods
pdf = Pdf.from_markdown(
    "# Report\n\nContent here.",
    title="Quarterly Report",
    author="Finance Team"
)
pdf.save("report.pdf")

WASM

import { WasmPdf } from "pdf-oxide-wasm";

// JavaScript uses optional title and author parameters
const pdf = WasmPdf.fromMarkdown(
  "# Report\n\nContent here.",
  "Quarterly Report",
  "Finance Team"
);
writeFileSync("report.pdf", pdf.toBytes());

Rust

use pdf_oxide::api::PdfBuilder;
use pdf_oxide::writer::PageSize;

let pdf = PdfBuilder::new()
    .title("Quarterly Report")
    .author("Finance Team")
    .page_size(PageSize::A4)
    .margin(54.0)
    .font_size(11.0)
    .line_height(1.5)
    .from_markdown("# Report\n\nContent here.")?;

pdf.save("report.pdf")?;

Full API Reference

Constructor

PdfBuilder::new() -> PdfBuilder

Creates a new builder with default configuration:

Setting Default
Page size Letter (8.5" x 11")
All margins 72pt (1 inch)
Font size 12pt
Line height 1.5

Metadata Methods

.title(title) – Set Document Title

PdfBuilder::new().title("My Document")

.author(author) – Set Document Author

PdfBuilder::new().author("Jane Smith")

.subject(subject) – Set Document Subject

PdfBuilder::new().subject("Annual Performance Review")

.keywords(keywords) – Set Document Keywords

PdfBuilder::new().keywords("report, annual, 2025")

Layout Methods

.page_size(size) – Set Page Size

use pdf_oxide::writer::PageSize;

PdfBuilder::new().page_size(PageSize::A4)

PageSize enum:

Variant Dimensions
PageSize::Letter 612 x 792 pt (8.5" x 11")
PageSize::A4 595 x 842 pt (210 x 297 mm)
PageSize::Legal 612 x 1008 pt (8.5" x 14")
PageSize::A3 842 x 1190 pt (297 x 420 mm)
PageSize::Custom(w, h) Custom width x height in points

.margin(margin) – Set Uniform Margins

Sets all four margins (left, right, top, bottom) to the same value in points.

PdfBuilder::new().margin(54.0)  // 0.75 inch margins

.margins(left, right, top, bottom) – Set Individual Margins

PdfBuilder::new().margins(72.0, 72.0, 54.0, 54.0)

Typography Methods

.font_size(size) – Set Default Font Size

PdfBuilder::new().font_size(11.0)

.line_height(height) – Set Line Height Multiplier

Controls spacing between lines. A value of 1.5 means 1.5 times the font size.

PdfBuilder::new().line_height(1.6)

Generation Methods

Each generation method consumes the builder and returns a Pdf object.

.from_markdown(content) – Generate from Markdown

let pdf = PdfBuilder::new()
    .title("Notes")
    .from_markdown("# Notes\n\n- Item one\n- Item two")?;

.from_html(content) – Generate from HTML

let pdf = PdfBuilder::new()
    .title("Report")
    .from_html("<h1>Report</h1><p>Summary of findings.</p>")?;

.from_text(content) – Generate from Plain Text

let pdf = PdfBuilder::new()
    .font_size(10.0)
    .from_text("Line 1\nLine 2\nLine 3")?;

.from_image(path) – Generate from Single Image

let pdf = PdfBuilder::new()
    .page_size(PageSize::A4)
    .from_image("photo.jpg")?;

.from_image_bytes(data) – Generate from Image Bytes

let data = std::fs::read("chart.png")?;
let pdf = PdfBuilder::new().from_image_bytes(&data)?;

.from_images(paths) – Generate from Multiple Images

let pdf = PdfBuilder::new()
    .title("Photo Album")
    .from_images(&["img1.jpg", "img2.jpg", "img3.jpg"])?;

.from_qrcode(data) – Generate QR Code PDF

Requires the barcodes feature.

let pdf = PdfBuilder::new()
    .title("QR Code")
    .from_qrcode("https://example.com")?;

.from_barcode(barcode_type, data) – Generate Barcode PDF

Requires the barcodes feature.

use pdf_oxide::writer::barcode::BarcodeType;

let pdf = PdfBuilder::new()
    .from_barcode(BarcodeType::Code128, "ABC-12345")?;

Advanced Examples

Full Configuration Chain

use pdf_oxide::api::PdfBuilder;
use pdf_oxide::writer::PageSize;

let content = r#"
# Technical Specification

## Overview

This document defines the interface contract for the v2 API.

## Endpoints

- `GET /api/v2/users` - List users
- `POST /api/v2/users` - Create user
- `GET /api/v2/users/:id` - Get user by ID

## Authentication

All endpoints require a Bearer token in the Authorization header.
"#;

let pdf = PdfBuilder::new()
    .title("API Specification v2")
    .author("Platform Team")
    .subject("REST API Technical Specification")
    .keywords("api, rest, specification, v2")
    .page_size(PageSize::A4)
    .margins(72.0, 72.0, 54.0, 72.0)
    .font_size(11.0)
    .line_height(1.5)
    .from_markdown(content)?;

pdf.save("api_spec.pdf")?;

Custom Page Size

use pdf_oxide::api::PdfBuilder;
use pdf_oxide::writer::PageSize;

// Create a half-letter page (5.5" x 8.5")
let pdf = PdfBuilder::new()
    .page_size(PageSize::Custom(396.0, 612.0))
    .margin(36.0)
    .font_size(10.0)
    .from_markdown("# Pocket Guide\n\nCompact reference card.")?;

pdf.save("pocket_guide.pdf")?;

JavaScript Equivalent

In JavaScript/WASM, WasmPdf static methods accept optional title and author parameters, providing the same functionality as PdfBuilder metadata:

import { WasmPdf } from "pdf-oxide-wasm";

const pdf = WasmPdf.fromMarkdown("# Notes\n\n- Item one\n- Item two", "Notes");
const html = WasmPdf.fromHtml("<h1>Report</h1><p>Summary.</p>", "Report", "Author");
const text = WasmPdf.fromText("Line 1\nLine 2", "Plain Text Doc");
const img = WasmPdf.fromImageBytes(imageData);

Page size, margins, font size, and line height configuration are not available in the WASM API — use the default layout settings.

Output Methods

Once you have a Pdf / WasmPdf object, use these methods to output it:

Method Language Description
.save(path) Python, Rust Write the PDF to a file
.to_bytes() (Python) Python Get the raw PDF bytes
.into_bytes() (Rust) Rust Consume the Pdf and return Vec<u8>
.toBytes() (JS) JavaScript Get the PDF as Uint8Array