Skip to content

PdfBuilder Fluent API

PdfBuilder は PDF 作成のための流暢な設定 API を提供します。 メソッドをチェーンしてページサイズ、マージン、フォントサイズ、メタデータなどを設定し, その後 from_* メソッドを呼び出して PDF を生成します。

簡単な例

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

完全な API リファレンス

コンストラクタ

PdfBuilder::new() -> PdfBuilder

デフォルト設定で新しいビルダーを作成します:

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

メタデータメソッド

.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")

レイアウトメソッド

.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) カスタム幅 x 高さ(ポイント)

.margin(margin) – Set Uniform Margins

4つのマージンすべてを同じポイント値に設定します。

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)

タイポグラフィメソッド

.font_size(size) – Set Default Font Size

PdfBuilder::new().font_size(11.0)

.line_height(height) – Set Line Height Multiplier

行間を制御します。1.5はフォントサイズの1.5倍です。

PdfBuilder::new().line_height(1.6)

生成メソッド

各生成メソッドはビルダーを消費して 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

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

応用例

フル設定チェーン

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

カスタムページサイズ

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 での同等処理

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.

出力メソッド

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

Method Language 説明
.save(path) Python, Rust PDFをファイルに書き込む
.to_bytes() (Python) Python 生のPDFバイトを取得
.into_bytes() (Rust) Rust Pdfを消費して返す Vec<u8>
.toBytes() (JS) JavaScript PDFを取得 Uint8Array

関連ページ