Skip to content

サンプル: 検索可能 PDF の生成

このサンプルでは YomiTokucreate_searchable_pdf() を用いて、 スキャン画像から テキストを埋め込んだ検索可能 PDF を作成する手順を示します。 PDF 内のテキストはコピー & 検索が可能になるため、 社内共有や電子帳簿保存法対応の前処理として便利です。

from yomitoku import DocumentAnalyzer
from yomitoku.data.functions import load_image
from yomitoku.utils import create_searchable_pdf

images = load_image("demo/samples/demo.jpg")
analyzer = DocumentAnalyzer(visualize=True, device="cuda")

results = []
for img in images:
    result, ocr_vis, layout_vis = analyzer(img)
    results.append(result)

create_searchable_pdf(
    images,
    results,
    output_path="demo_searchable_pdf.pdf",
)

analyzer.close()

create_searchable_pdf() パラメータ

引数 必須 説明
images list[np.ndarray] ページ画像のリスト(RGB/BGR どちらでも可)。
results list[DocumentAnalyzerSchema] 各ページの解析結果。len(results)len(images) と一致する必要があります。
output_path str 生成する PDF の保存先パス。
font_path str 埋め込みに使用するTrueTypeフォントデータのパス(.ttf)

よくある質問

質問 回答
解析結果が 1 ページでも results をリストにする必要は? はい。[single_result] のように 必ずリスト で渡してください。

実行結果

  • demo_searchable_pdf.pdf

  • 画像はそのままの見た目

  • Ctrl/Cmd + F で文字列検索可
  • Ctrl/Cmd + C でテキストコピー可