Skip to content

PdfBuilder 플루언트 API

PdfBuilder는 PDF 생성을 위한 플루언트(fluent) 구성 API를 제공합니다. 메서드를 체이닝하여 페이지 크기, 여백, 글꼴 크기, 메타데이터 등을 설정한 다음 from_* 메서드를 호출하여 PDF를 생성합니다.

바인딩 지원 범위. 플루언트 PdfBuilder / WasmPdfBuilderPython, Rust, WASM에서 제공됩니다. GoC# 바인딩은 단축 진입점(FromMarkdown, FromHtml, FromImage / Pdf.FromMarkdown, Pdf.FromHtml, Pdf.FromText)을 제공하지만 빌더 구성 인터페이스는 제공하지 않습니다. Go나 C#에서 사용자 정의 여백이나 페이지 크기를 사용하려면 플래그를 지정하여 Rust CLI를 호출하거나, 빌더를 FFI로 노출하는 Rust 헬퍼를 래핑하세요.

빠른 예제

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

기본 구성으로 새 빌더를 생성합니다:

설정 기본값
페이지 크기 Letter (8.5" x 11")
모든 여백 72pt (1인치)
글꼴 크기 12pt
줄 높이 1.5

메타데이터 메서드

.title(title) – 문서 제목 설정

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

.author(author) – 문서 작성자 설정

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

.subject(subject) – 문서 주제 설정

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

.keywords(keywords) – 문서 키워드 설정

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

레이아웃 메서드

.page_size(size) – 페이지 크기 설정

use pdf_oxide::writer::PageSize;

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

PageSize 열거형:

변형 크기
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) – 균일한 여백 설정

네 개의 여백(왼쪽, 오른쪽, 위, 아래)을 모두 동일한 값(포인트 단위)으로 설정합니다.

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

.margins(left, right, top, bottom) – 개별 여백 설정

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

타이포그래피 메서드

.font_size(size) – 기본 글꼴 크기 설정

PdfBuilder::new().font_size(11.0)

.line_height(height) – 줄 높이 배수 설정

줄 간격을 제어합니다. 값이 1.5이면 글꼴 크기의 1.5배를 의미합니다.

PdfBuilder::new().line_height(1.6)

생성 메서드

각 생성 메서드는 빌더를 소비하고 Pdf 객체를 반환합니다.

.from_markdown(content) – Markdown에서 생성

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

.from_html(content) – HTML에서 생성

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

.from_text(content) – 일반 텍스트에서 생성

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

.from_image(path) – 단일 이미지에서 생성

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

.from_image_bytes(data) – 이미지 바이트에서 생성

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

.from_images(paths) – 여러 이미지에서 생성

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

.from_qrcode(data) – QR 코드 PDF 생성

barcodes 기능이 필요합니다.

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

.from_barcode(barcode_type, data) – 바코드 PDF 생성

barcodes 기능이 필요합니다.

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 정적 메서드는 선택적 titleauthor 매개변수를 받아 PdfBuilder 메타데이터와 동일한 기능을 제공합니다:

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

페이지 크기, 여백, 글꼴 크기, 줄 높이 구성은 WASM API에서 사용할 수 없습니다. 기본 레이아웃 설정을 사용하세요.

출력 메서드

Pdf / WasmPdf 객체를 얻은 후에는 다음 메서드를 사용하여 출력합니다:

메서드 언어 설명
.save(path) Python, Rust PDF를 파일에 씁니다
.to_bytes() (Python) Python 원시 PDF 바이트를 가져옵니다
.into_bytes() (Rust) Rust Pdf를 소비하고 Vec<u8>을 반환합니다
.toBytes() (JS) JavaScript PDF를 Uint8Array로 가져옵니다

관련 페이지