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 | 기본값 |
|---|---|
| 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
네 개의 여백(왼쪽, 오른쪽, 위, 아래) 모두를 포인트 단위의 동일한 값으로 설정합니다.
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 기본값 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("비고")
.from_markdown("# 비고\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 동등 기능
JavaScript/WASM에서 WasmPdf 정적 메서드는 선택적 title 및 author 매개변수를 받아 PdfBuilder 메타데이터와 동일한 기능을 제공합니다:
import { WasmPdf } from "pdf-oxide-wasm";
const pdf = WasmPdf.fromMarkdown("# 비고\n\n- Item one\n- Item two", "비고");
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);
페이지 크기, 여백, 폰트 크기, 줄 높이 구성은 WASM API에서 사용할 수 없습니다 – 기본 레이아웃 설정을 사용하세요.
출력 메서드
Pdf / WasmPdf 객체를 얻은 후 다음 메서드를 사용하여 출력합니다:
| 메서드 | Language | 설명 |
|---|---|---|
.save(path) |
Python, Rust | PDF를 파일에 저장 |
.to_bytes() (Python) |
Python | 원시 PDF 바이트 가져오기 |
.into_bytes() (Rust) |
Rust | Pdf를 소비하고 Vec<u8> 반환 |
.toBytes() (JS) |
JavaScript | Uint8Array로 PDF 가져오기 |
관련 페이지
- Create from Markdown – Markdown to PDF
- Create from HTML – HTML to PDF
- Create from Images – Images to PDF
- DocumentBuilder Low-Level API – Programmatic page construction
- QR Codes and Barcodes – Barcode generation