0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LibreOfficeでHTMLをPDFに変換する

Last updated at Posted at 2025-12-27

これは LibreOffice Advent Calendar 2025 の25日目の記事です。


HTML を PDF に変換する wkhtmltopdf というツールが開発終了したけど代替どうする? という話があります。

参考: ゼロからはじめるPython(114) HTMLからPDFを作成するツール「wkhtmltopdf」が開発終了・代替ライブラリのxhtml2pdfとWeasyPrintを試してみよう | TECH+(テックプラス)(2024-03-11)

しかし、残念なことにレンダリングエンジンとして使っていたQt Webkitのメンテナンスが止まってしまった。それが原因でwkhtmltopdfの開発も終了することになった。2023年1月にGitHubのリポジトリはアーカイブされ読み取り専用となった。

上記記事では xhtml2pdf と WeasyPrint という候補が挙げられていますが、LibreOffice でもできます。

soffice --version
  #=> LibreOffice 24.2.7.2 420(Build:2)

  # Windows の場合は soffice.exe を使う(はず)
soffice --convert-to pdf sample.html
  #=> sample.pdf が出力される

ローカルのファイルではなく URL を指定することもできます。

soffice --convert-to pdf https://ja.libreoffice.org/

コマンドでの実行では細かい制御はできなさそうですが、要件によってはこれで十分かもしれません。

LibreOffice Writer は HTML ファイルの読み込みに対応しており、当然それを PDF として保存することもできます。知ってる人には当たり前かもしれませんが、知らない人も多そうなので、こうして「LibreOffice でもできますよ」と簡単に紹介してみました。

Docker

Docker コンテナでも実行できます。以下はベースイメージとして ubuntu:24.04 を使った場合の最低限の例です。

# Dockerfile

FROM ubuntu:24.04

RUN apt-get update \
  && apt-get -y install --no-install-recommends \
    libreoffice-writer \
    fonts-noto-cjk \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

WORKDIR /root/work
  # イメージをビルド
docker build -t libreoffice-html-to-pdf:1 .

  # 実行
docker run --rm -v "$(pwd):/root/work" libreoffice-html-to-pdf:1 \
  soffice --convert-to pdf sample.html

SDK 利用(未確認)

LibreOffice SDK を使うともうちょっと細かいことができそうですが、今回はそこまで手が回りませんでした。気が向いたら調べるかも。

この記事を読んだ人は(ひょっとしたら)こちらも読んでいます

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?