Skip to content

非同期 PDF 処理

PDF Oxide は Python、C#、Node.js でファーストクラスの非同期 API を提供しており、抽出処理がイベントループや HTTP リクエストハンドラをブロックすることはありません。同じ PdfDocument のメソッドが非同期ラッパーとして公開され、バックグラウンドスレッド/スレッドプールにディスパッチされます。

Python — AsyncPdfDocument

PdfDocument のすべてのメソッドには、AsyncPdfDocument 上に await 可能な対応版があります。各呼び出しは同期メソッドを asyncio.to_thread() でラップします。

Python

import asyncio
from pdf_oxide import AsyncPdfDocument

async def extract(path):
    doc = await AsyncPdfDocument.open(path)
    text = await doc.extract_text(0)
    return text

asyncio.run(extract("report.pdf"))

ページ間のファンアウト

Python

import asyncio
from pdf_oxide import AsyncPdfDocument

async def extract_all(path):
    doc = await AsyncPdfDocument.open(path)
    page_count = await doc.page_count()
    pages = await asyncio.gather(*[doc.extract_text(i) for i in range(page_count)])
    return pages

作成

AsyncPdf は作成フロー向けに Pdf クラスをミラーリングします:

Python

from pdf_oxide import AsyncPdf

pdf = await AsyncPdf.from_markdown("# Hello")
await pdf.save_async("out.pdf")

Office 変換

AsyncOfficeConverter は DOCX / XLSX / PPTX → PDF の非同期変換を処理します。

Python

from pdf_oxide import AsyncOfficeConverter

converter = AsyncOfficeConverter()
pdf_bytes = await converter.docx_to_pdf_bytes("input.docx")

フリースレッド版 Python (cp314t)

pdf_oxide 拡張モジュールは gil_used = false を宣言しているため、cp314t(Python 3.14 のフリースレッドビルド)でも安全に使用できます。複数のスレッドが GIL によるシリアル化なしに PdfDocument のメソッドを並列で呼び出せます。

Python

from concurrent.futures import ThreadPoolExecutor
from pdf_oxide import PdfDocument

doc = PdfDocument("large.pdf")

with ThreadPoolExecutor(max_workers=8) as pool:
    pages = list(pool.map(doc.extract_text, range(doc.page_count())))

C# — async Task<T> with CancellationToken

すべての抽出メソッドと保存メソッドには、Task<T> を返し、オプションの CancellationToken を受け取る *Async 版があります。

C#

using PdfOxide.Core;

using var doc = PdfDocument.Open("report.pdf");

string text = await doc.ExtractTextAsync(0);

using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
var tasks = Enumerable.Range(0, doc.PageCount)
    .Select(i => doc.ExtractTextAsync(i, cts.Token));
string[] pages = await Task.WhenAll(tasks);

エディタ

C#

using var editor = DocumentEditor.Open("form.pdf");
editor.SetFormFieldValue("name", "Jane Doe");
await editor.SaveAsync("filled.pdf");

Node.js — *Async methods

すべての同期メソッドには非同期版があります — extractTextextractTextAsyncsavesaveAsync など。非同期呼び出しは libuv のスレッドプール上で実行されます。

Node.js

const { PdfDocument } = require("pdf-oxide");

async function extractAll(path) {
  const doc = new PdfDocument(path);
  try {
    const pageCount = doc.getPageCount();
    const pages = await Promise.all(
      Array.from({ length: pageCount }, (_, i) => doc.extractTextAsync(i))
    );
    return pages;
  } finally {
    doc.close();
  }
}

HTTP ハンドラの例

Node.js

import express from "express";
import { PdfDocument } from "pdf-oxide";

const app = express();

app.post("/extract", express.raw({ type: "application/pdf", limit: "50mb" }), async (req, res) => {
  const doc = PdfDocument.openFromBytes(req.body);
  try {
    const text = await doc.extractTextAsync(0);
    res.json({ text });
  } finally {
    doc.close();
  }
});

並行性モデル

言語 仕組み
Python asyncio.to_thread が同期呼び出しをデフォルトの executor にディスパッチ
Python (cp314t) 真のスレッド並列性 — GIL はオプション
C# タスクベース非同期パターン、ThreadPool にディスパッチ
Node.js libuv のワーカースレッド(Napi::AsyncWorker
Go 不要 — goroutine が同期メソッドを直接呼び出す
Rust 提供なし — tokio::task::spawn_blocking または任意の executor を使用

複数スレッド間で単一の PdfDocument を共有する方法については、並行処理ガイド を参照してください。

関連項目