Skip to content

PdfBuilder Fluent API

PdfBuilder fornece uma API de configuração fluente para criação de PDF. Chain methods to set página size, margins, font size, metadata, and more, then call a from_* método to generate the PDF.

Exemplo Rápido

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")?;

Referência Completa da API

Construtor

PdfBuilder::new() -> PdfBuilder

Cria um novo builder com configuração padrão:

Setting Padrão
Tamanho da página Letter (8.5" x 11")
Todas as margens 72pt (1 polegada)
Tamanho da fonte 12pt
Line height 1.5

Metadados 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 Padrão 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

Cada método de geração consome o builder e retorna um objeto Pdf.

.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

Requer the barcodes feature.

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

.from_barcode(barcode_type, data) – Generate Barcode PDF

Requer the barcodes feature.

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

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

Exemplos Avançados

Full Configuration Chain

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

let content = r#"
# Technical Specification

## Overview

Este documento define o contrato de interface para a API v2.

## 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")?;

Equivalente JavaScript

In JavaScript/WASM, WasmPdf método estaticos 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 não disponível in the WASM API — use the default layout settings.

Metodos de Saida

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

Method Language Descrição
.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 Obter o PDF as Uint8Array

Páginas Relacionadas