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?

Gotenbergで行うPDF生成

0
Posted at

Gotenbergとは

Gotenbergは、Chromiumエンジンをベースにしたマイクロサービス型のPDF生成ツールです。RESTful APIを通じて、HTML、URL、Office文書などからPDFを生成できます。

主な特徴

1. モダンなレンダリングエンジン

Chromiumエンジンを使用することで、最新のWeb標準に完全対応:

  • ES6+ JavaScript: アロー関数、async/await、ES Modulesなどモダンな構文をそのまま実行可能
  • CSS3完全対応: Grid、Flexbox、カスタムプロパティなど最新のCSS機能
  • Web標準準拠: ブラウザで見える通りのPDF生成が可能

2. マイクロサービスアーキテクチャ

独立したDockerコンテナとして動作し、以下のメリットを提供:

  • 言語非依存: RESTful APIによりあらゆる言語から利用可能
  • スケーラビリティ: 負荷に応じて水平スケーリングが容易
  • 分離された実行環境: アプリケーションから独立してリソース管理
  • 安定したリソース消費: 内部キューイングにより、同時リクエストが来てもリソース消費が一定に保たれる

3. 豊富な内蔵フォント

Gotenbergには多数のフォントがプリインストールされており、追加設定なしで利用可能:

  • 日本語フォント: IPAゴシック / IPA明朝、Noto Sans CJK JP / Noto Serif CJK JP
  • 欧文フォント: Liberation Sans/Serif/Mono、DejaVu fontsなど

特に日本語環境では、IPAフォントとNotoフォントが標準搭載されているため、追加設定なしで美しい日本語PDFを生成できます。

# フォントの種類をカウント
gotenberg@3461d10f2335:~$ fc-list | wc -l
648

4. 柔軟な変換オプション

様々な入力形式に対応:

  • HTML文字列
  • URL
  • Markdownファイル
  • Microsoft Office文書(LibreOffice経由)

環境構築

Dockerで簡単に起動できます:

# Gotenbergを起動
docker run -d --rm -p 3000:3000 --name gotenberg gotenberg/gotenberg:8

# ヘルスチェック
curl http://localhost:3000/health
# {"status":"up"}

基本的な使い方

HTMLファイルからPDF生成

sample.html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>サンプル</title>
    <style>
        body {
            font-family: 'Noto Sans CJK JP', sans-serif;
            padding: 40px;
        }
        h1 { color: #333; }
    </style>
</head>
<body>
    <h1>Gotenberg PDF生成テスト</h1>
    <p>日本語も問題なく表示されます。</p>
</body>
</html>
# PDFに変換
curl -X POST http://localhost:3000/forms/chromium/convert/html \
  -F "files=@sample.html" \
  -F "printBackground=true" \
  -o sample.pdf

URLからPDF生成

curl -X POST http://localhost:3000/forms/chromium/convert/url \
  -F "url=https://example.com" \
  -F "printBackground=true" \
  -o website.pdf

Office文書からPDF生成

# Word、Excel、PowerPointに対応
curl -X POST http://localhost:3000/forms/libreoffice/convert \
  -F "files=@document.docx" \
  -o document.pdf

主要なオプション

オプション 説明
printBackground 背景色・画像を印刷 true
landscape 横向き true
marginTop/Bottom/Left/Right 余白 1cm
waitDelay 待機時間 2s
waitForExpression JS条件待機 window.ready === true

ハマりポイントと解決策

1. 外部CSS/JSファイルが読み込めない

HTMLを渡して生成する場合に、HTML内でアセットを相対パスで参照している場合、Gotenbergコンテナからはそのファイルにアクセスできません。

<!-- この書き方だとGotenbergから読み込めない -->
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>

解決策1: インラインで埋め込む

<style>
  /* CSSを直接記述 */
  body { font-family: sans-serif; }
</style>
<script>
  // JSを直接記述
  console.log('Hello');
</script>

解決策2: Webサーバー経由でアクセス可能にする

外部ファイルをWebサーバーに配置し、絶対URLで参照します。

<!-- Gotenbergコンテナからアクセス可能なURLを指定 -->
<link rel="stylesheet" href="http://your-server/assets/style.css">

重要なのは、GotenbergコンテナからそのURLにアクセスできることです。

解決策3: baseタグを使用

HTMLの<base>タグでベースURLを指定することで、相対パスの基準を変更できます。

<!DOCTYPE html>
<html>
<head>
    <!-- ベースURLを指定 -->
    <base href="http://your-server/assets/">

    <!-- これで http://your-server/assets/style.css を参照 -->
    <link rel="stylesheet" href="style.css">
</head>
<body>
    ...
</body>
</html>

2. Docker環境での利用

Docker Compose環境では、コンテナ間通信にはコンテナ名を使用します。

# compose.yaml
services:
  gotenberg:
    image: gotenberg/gotenberg:8
    ports:
      - "3000:3000"
    networks:
      - app-network

  webserver:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./static-files:/usr/share/nginx/html:ro
    networks:
      - app-network

networks:
  app-network:

この構成では:

  • ホストから: http://localhost:8080/style.css
  • Gotenbergコンテナから: http://webserver/style.css

GotenbergにURLを渡す際は、コンテナ名webserver)を使用します:

curl -X POST http://localhost:3000/forms/chromium/convert/url \
  -F "url=http://webserver/index.html" \
  -o output.pdf

3. HTTPS・自己署名証明書の問題

開発環境で自己署名証明書を使用している場合、Gotenbergがアクセス時に証明書エラーを起こします。

解決策: Gotenbergの起動オプションで証明書検証を無効化

docker run -d --rm -p 3000:3000 --name gotenberg \
  gotenberg/gotenberg:8 \
  gotenberg \
  --chromium-ignore-certificate-errors

Docker Composeの場合:

services:
  gotenberg:
    image: gotenberg/gotenberg:8
    command:
      - "gotenberg"
      - "--chromium-ignore-certificate-errors"

4. 同時リクエスト時の処理待ち

Gotenbergは内部でキューイングを行っており、同時に複数のリクエストが来ても順次処理されます。これによりリソース消費が一定に保たれるというメリットがありますが、同時に複数のPDFを並列生成することはできません

大量のPDF生成が必要な場合の対策:

  • 複数インスタンス/コンテナを起動する。
    • アプリケーション側でリクエストを振り分けるか、ロードバランサーを配置して対応します。

まとめ

Gotenbergを使用する際のポイント:

  1. 外部リソースのアクセス経路を確認 - Gotenbergコンテナからアクセスできる必要がある
  2. Docker環境ではコンテナ名を使用 - localhostではなくコンテナ名で指定
  3. 自己署名証明書は要注意 - 開発環境では--chromium-ignore-certificate-errorsオプションを使用
  4. 動的コンテンツは完了を待つ - waitForExpressionでJavaScriptの実行完了を待機

これらのポイントを押さえれば、Gotenbergを使って様々な形式のPDFを簡単に生成できます。

参考リンク

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?