HTML から作成
2 つのエントリーポイントが用意されています。
Pdf::from_html(content)— 基本的な構造を持つ HTML(見出し、段落、リスト、コード、太字/斜体)。スタイル指定はなし。すべてのバインディングで利用可能。Pdf::from_html_css(html, css, font_bytes)— v0.3.37 で導入された純 Rust 製の HTML+CSS パイプライン全体。自作の CSS エンジン(L3 + L4 セレクターのサブセット、カスケード、calc()/var()、@page/@media print)、Taffy ベースの block / flex / grid レイアウト、UAX #14 改行処理、rustybuzz による RTL シェイピング、::before/::after、page-break-*、<a href>→ リンク注釈、<img>data-URI →/XObject、マルチフォントカスケードを備えます。MPL 依存はゼロ。すべてのバインディングで利用可能。
クイック例
Python
from pdf_oxide import Pdf
pdf = Pdf.from_html("<h1>Hello</h1><p>World</p>")
pdf.save("out.pdf")
WASM
import { WasmPdf } from "pdf-oxide-wasm";
import { writeFileSync } from "fs";
const pdf = WasmPdf.fromHtml("<h1>Hello</h1><p>World</p>");
writeFileSync("out.pdf", pdf.toBytes());
Rust
use pdf_oxide::api::Pdf;
let pdf = Pdf::from_html("<h1>Hello</h1><p>World</p>")?;
pdf.save("out.pdf")?;
Go
package main
import (
"log"
pdfoxide "github.com/yfedoseev/pdf_oxide/go"
)
func main() {
pdf, err := pdfoxide.FromHtml("<h1>Hello</h1><p>World</p>")
if err != nil { log.Fatal(err) }
defer pdf.Close()
if err := pdf.Save("out.pdf"); err != nil { log.Fatal(err) }
}
C#
using PdfOxide;
using var pdf = Pdf.FromHtml("<h1>Hello</h1><p>World</p>");
pdf.Save("out.pdf");
Java
import fyi.oxide.pdf.Pdf;
import java.nio.file.Path;
try (Pdf pdf = Pdf.fromHtml("<h1>Hello</h1><p>World</p>")) {
pdf.saveTo(Path.of("out.pdf"));
}
PHP
use PdfOxide\Pdf;
$pdf = Pdf::fromHtml('<h1>Hello</h1><p>World</p>');
file_put_contents('out.pdf', $pdf->save());
Ruby
require 'pdf_oxide'
PdfOxide::Pdf.from_html('<h1>Hello</h1><p>World</p>') { |pdf| pdf.save('out.pdf') }
C++
#include <pdf_oxide/pdf_oxide.hpp>
auto pdf = pdf_oxide::Pdf::from_html("<h1>Hello</h1><p>World</p>");
pdf.save("out.pdf");
Swift
import PdfOxide
let pdf = try Pdf.fromHtml("<h1>Hello</h1><p>World</p>")
try pdf.save("out.pdf")
Kotlin
import fyi.oxide.pdf.Pdf
Pdf.fromHtml("<h1>Hello</h1><p>World</p>").use { it.saveTo(java.nio.file.Path.of("out.pdf")) }
Dart
import 'package:pdf_oxide/pdf_oxide.dart';
final pdf = Pdf.fromHtml('<h1>Hello</h1><p>World</p>');
pdf.save('out.pdf');
R
library(pdfoxide)
pdf <- pdf_from_html("<h1>Hello</h1><p>World</p>")
pdf_save(pdf, "out.pdf")
Julia
using PdfOxide
pdf = from_html("<h1>Hello</h1><p>World</p>")
save(pdf, "out.pdf")
Zig
const pdf_oxide = @import("pdf_oxide");
var pdf = try pdf_oxide.Pdf.fromHtml("<h1>Hello</h1><p>World</p>");
try pdf.save("out.pdf");
Scala
import fyi.oxide.pdf.Pdf
import scala.util.Using
Using.resource(Pdf.fromHtml("<h1>Hello</h1><p>World</p>"))(_.saveTo(java.nio.file.Path.of("out.pdf")))
Clojure
(require '[pdf-oxide.core :as pdf])
(let [p (pdf/from-html "<h1>Hello</h1><p>World</p>")]
(.saveTo p (java.nio.file.Path/of "out.pdf" (into-array String []))))
Objective-C
#import "POXPdfOxide.h"
NSError *err = nil;
POXPdf *pdf = [POXPdf fromHtml:@"<h1>Hello</h1><p>World</p>" error:&err];
[pdf saveToPath:@"out.pdf" error:&err];
Elixir
{:ok, pdf} = PdfOxide.from_html("<h1>Hello</h1><p>World</p>")
PdfOxide.save(pdf, "out.pdf")
HTML + CSS パイプライン (v0.3.37)
Pdf::from_html_css(html, css, font_bytes) は HTML、CSS スタイルシート、TTF/OTF フォントのバイト列を受け取り、ページ分割された PDF を返します。extract_text がバイト単位で完全に往復するため、生成された PDF は既存のテストインフラに組み込めます。
Rust:
use pdf_oxide::api::Pdf;
let font = std::fs::read("DejaVuSans.ttf")?;
let pdf = Pdf::from_html_css(
"<h1>Hello</h1><p>World</p>",
"h1 { color: blue; font-size: 24pt } p { line-height: 1.5 }",
font,
)?;
pdf.save("out.pdf")?;
Python:
from pdf_oxide import Pdf
with open("DejaVuSans.ttf", "rb") as f:
font = f.read()
pdf = Pdf.from_html_css(
"<h1>Hello</h1><p>World</p>",
"h1 { color: blue; font-size: 24pt }",
font,
)
pdf.save("out.pdf")
Node / TypeScript:
import { Pdf } from "pdf-oxide";
import { readFileSync } from "fs";
const font = readFileSync("DejaVuSans.ttf");
const pdf = Pdf.fromHtmlCss(
"<h1>Hello</h1><p>World</p>",
"h1 { color: blue; font-size: 24pt }",
font,
);
pdf.save("out.pdf");
Go:
font, _ := os.ReadFile("DejaVuSans.ttf")
pdf, err := pdfoxide.FromHtmlCss(
"<h1>Hello</h1><p>World</p>",
"h1 { color: blue; font-size: 24pt }",
font,
)
if err != nil { log.Fatal(err) }
defer pdf.Close()
_ = pdf.Save("out.pdf")
C#:
var font = File.ReadAllBytes("DejaVuSans.ttf");
using var pdf = Pdf.FromHtmlCss(
"<h1>Hello</h1><p>World</p>",
"h1 { color: blue; font-size: 24pt }",
font);
pdf.Save("out.pdf");
C++:
#include <pdf_oxide/pdf_oxide.hpp>
#include <fstream>
std::ifstream in("DejaVuSans.ttf", std::ios::binary);
std::string font((std::istreambuf_iterator<char>(in)), {});
auto pdf = pdf_oxide::Pdf::from_html_css(
"<h1>Hello</h1><p>World</p>",
"h1 { color: blue; font-size: 24pt }",
std::vector<uint8_t>(font.begin(), font.end()));
pdf.save("out.pdf");
Swift:
import PdfOxide
import Foundation
let font = [UInt8](try Data(contentsOf: URL(fileURLWithPath: "DejaVuSans.ttf")))
let pdf = try Pdf.fromHtmlCss(
html: "<h1>Hello</h1><p>World</p>",
css: "h1 { color: blue; font-size: 24pt }",
fontBytes: font)
try pdf.save("out.pdf")
Dart:
import 'dart:io';
import 'package:pdf_oxide/pdf_oxide.dart';
final font = File('DejaVuSans.ttf').readAsBytesSync();
final pdf = Pdf.fromHtmlCss(
'<h1>Hello</h1><p>World</p>',
'h1 { color: blue; font-size: 24pt }',
font);
pdf.save('out.pdf');
R:
library(pdfoxide)
font <- readBin("DejaVuSans.ttf", "raw", file.info("DejaVuSans.ttf")$size)
pdf <- pdf_from_html_css(
"<h1>Hello</h1><p>World</p>",
"h1 { color: blue; font-size: 24pt }",
font)
pdf_save(pdf, "out.pdf")
Julia:
using PdfOxide
font = read("DejaVuSans.ttf")
pdf = from_html_css(
"<h1>Hello</h1><p>World</p>",
"h1 { color: blue; font-size: 24pt }",
font)
save(pdf, "out.pdf")
Zig:
const pdf_oxide = @import("pdf_oxide");
const std = @import("std");
const font = try std.fs.cwd().readFileAlloc(std.heap.page_allocator, "DejaVuSans.ttf", 1 << 24);
var pdf = try pdf_oxide.Pdf.fromHtmlCss(
"<h1>Hello</h1><p>World</p>",
"h1 { color: blue; font-size: 24pt }",
font);
try pdf.save("out.pdf");
Objective-C:
#import "POXPdfOxide.h"
NSError *err = nil;
NSData *font = [NSData dataWithContentsOfFile:@"DejaVuSans.ttf"];
POXPdf *pdf = [POXPdf fromHtml:@"<h1>Hello</h1><p>World</p>"
css:@"h1 { color: blue; font-size: 24pt }"
fontBytes:font
error:&err];
[pdf saveToPath:@"out.pdf" error:&err];
Elixir:
font = File.read!("DejaVuSans.ttf")
{:ok, pdf} = PdfOxide.from_html_css(
"<h1>Hello</h1><p>World</p>",
"h1 { color: blue; font-size: 24pt }",
font)
PdfOxide.save(pdf, "out.pdf")
マルチフォントカスケード
ドキュメントで複数のフォントファミリーを混在させる場合は Pdf::from_html_css_with_fonts(html, css, fonts) を使用します。任意の要素の CSS font-family は、登録済みのファミリーに対して解決されます(大文字小文字を区別せず、引用符の有無や複数単語の非引用指定にも対応)。未知のファミリーは最初に登録されたフォントにフォールバックします。
from pdf_oxide import Pdf
fonts = [
("DejaVu Sans", open("DejaVuSans.ttf", "rb").read()),
("Noto Sans CJK", open("NotoSansCJKtc-Regular.otf", "rb").read()),
]
pdf = Pdf.from_html_css_with_fonts(
'<h1 style="font-family: DejaVu Sans">English</h1>'
'<p style="font-family: \'Noto Sans CJK\'">中文段落</p>',
"h1 { font-size: 24pt }",
fonts,
)
pdf.save("multilang.pdf")
CJK コンテンツは出力時に自動的にサブセット化されます(v0.3.38 #385)。約 17 MB の CJK フォントから 5 文字を含む PDF でも、通常は 100 KB 未満に収まります。
サポートされる CSS の範囲
- セレクター — L3 + L4 のサブセット:
:is/:where/:not/:has、構造擬似クラス、i/sフラグ付きの属性マッチャー。 - カスケード — オリジン/詳細度/ソース順による並べ替え、継承、インラインスタイルのマージ、カスタムプロパティ(循環検出付きの
var())。 - 関数 —
calc()、min()、max()、clamp()。 - アットルール —
@media print(常に真)、(min/max-width)、マージンボックス付きの@page :first / :left / :right / :blank、@font-face、@import、@supports。 - 型付き値 — 色(名前付き約 150 種、16 進数、rgb/rgba、hsl)、長さ(CSS Values L4 の全単位)、display、font-size / weight / style / family、margin / padding のショートハンド、line-height。
- カウンター —
counter/counters、counter-reset/-increment/-set、ローマ数字/ギリシャ文字/アルファベットによる番号付け。 - 擬似要素 —
::before/::after(リテラル文字列、attr(name)、open-quote/close-quote)。 - レイアウト — block、flex、grid(すべて Taffy 経由)、マージンの相殺、段組み(
column-count/column-width/column-gap)、テーブル(auto と fixed の列アルゴリズム)。 - インライン — UAX #14 改行処理、
text-align、white-spaceモード、ハード改行、アトミックなインラインボックス。 - 効果 —
opacity、transform: translate*()、page-break-before: always、page-break-after: always。 - HTML — HTML5 トークナイザー、
<style>/<link rel="stylesheet">/ インラインのstyle=""の抽出、<img>data-URI のデコード(/XObject)、<a href>→/URI付きの/Link注釈、<ul>/<ol>のリストマーカー。
対象外
CSS フィルター、3D 変形、アニメーション、HTML 内 SVG(実用的な Rust SVG クレートはすべて MPL)、MathML、hyphens: auto、shape-outside、JavaScript の実行、フル行列の transform(拡大縮小/回転)、グラデーション、box-shadow。
ライセンス
cargo deny check licenses は MPL の推移的依存が ゼロ の状態でパスします。Mozilla の CSS スタック(cssparser、selectors、html5ever、lightningcss、stylo)はすべて MPL-2.0 ですが、v0.3.37 では pdf_oxide 全体を MIT/Apache の下に保つため、それらに相当する機能を自作しています。
サポートされる HTML 要素
| 要素 | 説明 |
|---|---|
<h1> から <h6> |
見出し(PDF の見出しサイズにマッピング) |
<p> |
自動的な間隔を持つ段落 |
<b>, <strong> |
太字テキスト |
<i>, <em> |
斜体テキスト |
<ul>, <ol>, <li> |
順序なしリストと順序付きリスト |
<pre>, <code> |
整形済みテキストとインラインコード |
<blockquote> |
ブロック引用 |
<br> |
改行 |
<hr> |
水平線 |
完全な API リファレンス
Pdf::from_html(content)(静的メソッド)
デフォルト設定(Letter ページ、72pt マージン、12pt Helvetica)を使用して HTML コンテンツから PDF を作成します。
Rust:
use pdf_oxide::api::Pdf;
let html = r#"
<h1>Product Specification</h1>
<p>This document describes the <strong>technical requirements</strong>
for the new product line.</p>
<h2>Requirements</h2>
<ul>
<li>Operating temperature: -20C to 60C</li>
<li>Power consumption: <5W</li>
<li>Weight: <200g</li>
</ul>
"#;
let pdf = Pdf::from_html(html)?;
pdf.save("spec.pdf")?;
JavaScript:
import { WasmPdf } from "pdf-oxide-wasm";
import { writeFileSync } from "fs";
const html = `
<h1>Product Specification</h1>
<p>This document describes the <strong>technical requirements</strong>
for the new product line.</p>
`;
const pdf = WasmPdf.fromHtml(html);
writeFileSync("spec.pdf", pdf.toBytes());
Python:
from pdf_oxide import Pdf
html = """
<h1>Product Specification</h1>
<p>This document describes the <strong>technical requirements</strong>
for the new product line.</p>
"""
pdf = Pdf.from_html(html)
pdf.save("spec.pdf")
Java:
import fyi.oxide.pdf.Pdf;
import java.nio.file.Path;
String html = "<h1>Product Specification</h1>"
+ "<p>This document describes the <strong>technical requirements</strong>.</p>";
try (Pdf pdf = Pdf.fromHtml(html)) {
pdf.saveTo(Path.of("spec.pdf"));
}
PHP:
use PdfOxide\Pdf;
$html = '<h1>Product Specification</h1>'
. '<p>This document describes the <strong>technical requirements</strong>.</p>';
$pdf = Pdf::fromHtml($html);
file_put_contents('spec.pdf', $pdf->save());
Ruby:
require 'pdf_oxide'
html = '<h1>Product Specification</h1>' \
'<p>This document describes the <strong>technical requirements</strong>.</p>'
PdfOxide::Pdf.from_html(html) { |pdf| pdf.save('spec.pdf') }
C++:
#include <pdf_oxide/pdf_oxide.hpp>
std::string html =
"<h1>Product Specification</h1>"
"<p>This document describes the <strong>technical requirements</strong>.</p>";
auto pdf = pdf_oxide::Pdf::from_html(html);
pdf.save("spec.pdf");
Swift:
import PdfOxide
let html = """
<h1>Product Specification</h1>
<p>This document describes the <strong>technical requirements</strong>.</p>
"""
let pdf = try Pdf.fromHtml(html)
try pdf.save("spec.pdf")
Kotlin:
import fyi.oxide.pdf.Pdf
val html = """
<h1>Product Specification</h1>
<p>This document describes the <strong>technical requirements</strong>.</p>
""".trimIndent()
Pdf.fromHtml(html).use { it.saveTo(java.nio.file.Path.of("spec.pdf")) }
Dart:
import 'package:pdf_oxide/pdf_oxide.dart';
final html = '<h1>Product Specification</h1>'
'<p>This document describes the <strong>technical requirements</strong>.</p>';
final pdf = Pdf.fromHtml(html);
pdf.save('spec.pdf');
R:
library(pdfoxide)
html <- paste0(
"<h1>Product Specification</h1>",
"<p>This document describes the <strong>technical requirements</strong>.</p>")
pdf <- pdf_from_html(html)
pdf_save(pdf, "spec.pdf")
Julia:
using PdfOxide
html = """
<h1>Product Specification</h1>
<p>This document describes the <strong>technical requirements</strong>.</p>
"""
pdf = from_html(html)
save(pdf, "spec.pdf")
Zig:
const pdf_oxide = @import("pdf_oxide");
const html =
"<h1>Product Specification</h1>" ++
"<p>This document describes the <strong>technical requirements</strong>.</p>";
var pdf = try pdf_oxide.Pdf.fromHtml(html);
try pdf.save("spec.pdf");
Scala:
import fyi.oxide.pdf.Pdf
import scala.util.Using
val html =
"<h1>Product Specification</h1>" +
"<p>This document describes the <strong>technical requirements</strong>.</p>"
Using.resource(Pdf.fromHtml(html))(_.saveTo(java.nio.file.Path.of("spec.pdf")))
Clojure:
(require '[pdf-oxide.core :as pdf])
(let [html (str "<h1>Product Specification</h1>"
"<p>This document describes the <strong>technical requirements</strong>.</p>")
p (pdf/from-html html)]
(.saveTo p (java.nio.file.Path/of "spec.pdf" (into-array String []))))
Objective-C:
#import "POXPdfOxide.h"
NSError *err = nil;
NSString *html = @"<h1>Product Specification</h1>"
"<p>This document describes the <strong>technical requirements</strong>.</p>";
POXPdf *pdf = [POXPdf fromHtml:html error:&err];
[pdf saveToPath:@"spec.pdf" error:&err];
Elixir:
html =
"<h1>Product Specification</h1>" <>
"<p>This document describes the <strong>technical requirements</strong>.</p>"
{:ok, pdf} = PdfOxide.from_html(html)
PdfOxide.save(pdf, "spec.pdf")
Python シグネチャ:
Pdf.from_html(
content: str,
title: str | None = None,
author: str | None = None
) -> Pdf
PdfBuilder::new().from_html(content)(ビルダーパターン)
ページサイズ、マージン、フォントサイズ、ドキュメントメタデータを制御するには PdfBuilder を使用します。
Rust:
use pdf_oxide::api::PdfBuilder;
use pdf_oxide::writer::PageSize;
let pdf = PdfBuilder::new()
.title("Technical Specification")
.author("Engineering")
.page_size(PageSize::A4)
.margin(54.0)
.font_size(11.0)
.from_html("<h1>Spec</h1><p>Version 2.0</p>")?;
pdf.save("spec_a4.pdf")?;
応用例
構造化レポート
use pdf_oxide::api::Pdf;
let html = r#"
<h1>Incident Report</h1>
<h2>Summary</h2>
<p>On <em>2025-11-15</em>, a service disruption was detected in the
<strong>payment processing</strong> pipeline.</p>
<h2>Timeline</h2>
<ol>
<li>14:32 UTC - Alert triggered for elevated error rates</li>
<li>14:35 UTC - On-call engineer acknowledged</li>
<li>14:48 UTC - Root cause identified: database connection pool exhaustion</li>
<li>15:02 UTC - Fix deployed, services recovering</li>
<li>15:15 UTC - Full recovery confirmed</li>
</ol>
<h2>Root Cause</h2>
<p>A configuration change deployed at 14:00 UTC reduced the maximum
connection pool size from 100 to 10.</p>
<h2>Code Reference</h2>
<pre><code>max_connections: 10 # Should be 100
timeout_seconds: 30
</code></pre>
<h2>Action Items</h2>
<ul>
<li>Add validation for connection pool configuration</li>
<li>Implement canary deployment for config changes</li>
<li>Add alerting for connection pool utilization</li>
</ul>
"#;
let pdf = Pdf::from_html(html)?;
pdf.save("incident_report.pdf")?;
Python による動的 HTML
from pdf_oxide import Pdf
rows = [
("Widget A", "$12.99", 150),
("Widget B", "$24.50", 89),
("Widget C", "$7.25", 312),
]
html = "<h1>Inventory Report</h1>"
html += "<p>Generated on 2025-11-20</p>"
html += "<h2>Current Stock</h2><ul>"
for name, price, qty in rows:
html += f"<li><strong>{name}</strong> - {price} ({qty} units)</li>"
html += "</ul>"
pdf = Pdf.from_html(html, title="Inventory Report")
pdf.save("inventory.pdf")
ファイルからの HTML の読み込み
from pdf_oxide import Pdf
with open("report.html") as f:
html = f.read()
pdf = Pdf.from_html(html, title="Report")
pdf.save("report.pdf")
import { WasmPdf } from "pdf-oxide-wasm";
import { readFileSync, writeFileSync } from "fs";
const html = readFileSync("report.html", "utf-8");
const pdf = WasmPdf.fromHtml(html);
writeFileSync("report.pdf", pdf.toBytes());
use pdf_oxide::api::Pdf;
let html = std::fs::read_to_string("report.html")?;
let pdf = Pdf::from_html(&html)?;
pdf.save("report.pdf")?;
関連ページ
- Markdown から作成 – Markdown を PDF に変換
- PdfBuilder Fluent API – ビルダーの全設定オプション
- DocumentBuilder 低レベル API – プログラムによるページ構築