2026/05/12

請求書PDFテンプレートのHTMLデザインガイド|CSS・レイアウト・日本語対応

請求書HTMLテンプレートPDF生成CSSデザイン

請求書PDFの自動生成でAPIの呼び出し方を理解したら、次のステップはテンプレートのデザイン品質を上げることです。この記事では、PDF APIで使うHTMLテンプレートの設計パターンを、CSSレイアウト・日本語対応・インボイス制度対応まで実装コード付きで解説します。

テンプレート設計の基本方針

請求書テンプレートを設計するとき、以下の3点を最初に決めます。

  1. ページサイズ: この記事ではA4縦(APIの options.page-size は A3・A4・A5・Letter・Legal を受け付けます)
  2. 用紙の余白: この記事の例では天地20mm / 左右15mm
  3. フォント: 生成環境に入っている日本語フォント(FUNBREW の quality エンジンなら Noto Sans CJK JP を指定できます。生成したPDFで実際の表示を確認してください)
  4. エンジン: この記事のテンプレートは Flexbox と @page のマージンボックスを使うため、quality エンジン(Chromium。Pro プラン以上)を前提にしています。fast エンジン(wkhtmltopdf)は CSS2.1+一部の CSS3 のため、レイアウトが崩れることがあります

@page ルールでページ設定を固定する

@page {
  size: A4 portrait;
  margin: 20mm 15mm;
}

body {
  font-family: 'Noto Sans CJK JP', 'Noto Sans JP', 'Hiragino Kaku Gothic ProN', sans-serif;
  font-size: 10pt;
  line-height: 1.6;
  color: #1a1a1a;
  margin: 0;
}

FUNBREW の quality エンジン(Gotenberg)は、既定では用紙サイズと余白を API の options(page-size・margin-*。余白を省略すると 10mm)から決めます。@page の size よりも API の指定が使われるため、@page と API の options には同じ値を書いておき、生成したPDFで確認してください。CSS @page ルールの詳細は CSS @page ルール PDFガイドを参照してください。

ヘッダーセクション:会社情報と請求書番号

<header class="invoice-header">
  <div class="company-info">
    <h1 class="company-name">株式会社サンプル</h1>
    <p>〒100-0001 東京都千代田区千代田1-1-1</p>
    <p>TEL: 03-0000-0000 FAX: 03-0000-0001</p>
    <p>登録番号: T1234567890123</p>
  </div>
  <div class="invoice-meta">
    <h2 class="invoice-title">請求書</h2>
    <table class="meta-table">
      <tr><th>請求書番号</th><td>INV-2026-0042</td></tr>
      <tr><th>発行日</th><td>2026年5月12日</td></tr>
      <tr><th>お支払期限</th><td>2026年6月30日</td></tr>
    </table>
  </div>
</header>
.invoice-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  border-bottom: 2px solid #0052cc;
  padding-bottom: 12pt;
  margin-bottom: 16pt;
}

.company-name {
  font-size: 14pt;
  font-weight: 700;
  color: #0052cc;
  margin: 0 0 4pt;
}

.invoice-title {
  font-size: 22pt;
  font-weight: 700;
  color: #0052cc;
  margin: 0 0 8pt;
  text-align: right;
}

.meta-table {
  margin-left: auto;
  border-collapse: collapse;
  font-size: 9pt;
}

.meta-table th {
  text-align: right;
  padding: 2pt 8pt 2pt 0;
  color: #555;
  font-weight: normal;
}

.meta-table td {
  text-align: left;
  font-weight: 600;
}

宛先セクション:請求先の表示

<section class="bill-to">
  <p class="bill-to-label">請求先</p>
  <p class="company-name-large">{{client_name}} 御中</p>
  <p>{{client_address}}</p>
  <p>ご担当者: {{contact_name}} 様</p>
</section>
.bill-to {
  background: #f5f7fa;
  border-left: 4px solid #0052cc;
  padding: 10pt 12pt;
  margin-bottom: 20pt;
}

.bill-to-label {
  font-size: 8pt;
  color: #777;
  margin: 0 0 4pt;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.company-name-large {
  font-size: 14pt;
  font-weight: 700;
  margin: 0 0 4pt;
}

明細テーブル:請求項目の設計

請求書の核心は明細テーブルです。行が多くなっても読みやすい縞模様と、金額の右揃えを実装します。{{#each}}・{{#if}} はHandlebarsの構文で、後述のとおりアプリ側でHTMLに展開してからAPIへ送ります(FUNBREW のテンプレートAPI /api/pdf/generate-from-template は {{変数名}} の単純な置換だけで、ループや条件分岐には対応していません)。行番号の {{no}} は、データを渡す前に1から振っておく値です(Handlebarsの @index は0始まりです)。

<table class="line-items">
  <thead>
    <tr>
      <th class="col-no">No</th>
      <th class="col-desc">品目・摘要</th>
      <th class="col-qty">数量</th>
      <th class="col-unit">単位</th>
      <th class="col-price">単価</th>
      <th class="col-amount">金額</th>
      <th class="col-tax">税率</th>
    </tr>
  </thead>
  <tbody>
    {{#each items}}
    <tr>
      <td class="col-no">{{no}}</td>
      <td class="col-desc">{{description}}</td>
      <td class="col-qty text-right">{{quantity}}</td>
      <td class="col-unit text-center">{{unit}}</td>
      <td class="col-price text-right">{{unit_price_formatted}}</td>
      <td class="col-amount text-right">{{amount_formatted}}</td>
      <td class="col-tax text-center">{{tax_rate}}%</td>
    </tr>
    {{/each}}
  </tbody>
</table>
.line-items {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 16pt;
  font-size: 9pt;
}

.line-items thead tr {
  background-color: #0052cc;
  color: #ffffff;
}

.line-items thead th {
  padding: 6pt 8pt;
  text-align: center;
  font-weight: 600;
}

.line-items tbody tr:nth-child(even) {
  background-color: #f0f4ff;
}

.line-items tbody td {
  padding: 5pt 8pt;
  border-bottom: 1px solid #e0e7ff;
  vertical-align: middle;
}

/* 列幅の固定 */
.col-no    { width: 5%; }
.col-desc  { width: 38%; }
.col-qty   { width: 7%; }
.col-unit  { width: 7%; }
.col-price { width: 14%; }
.col-amount{ width: 15%; }
.col-tax   { width: 7%; }

.text-right  { text-align: right; }
.text-center { text-align: center; }

HTMLテーブルのPDF出力における詳しいCSSテクニックはHTML to PDF テーブルレイアウトガイドも参照してください。

合計金額セクション:税率別の集計

インボイス制度(適格請求書等保存方式)の適格請求書には、国税庁のインボイス制度の概要にあるとおり、次の6つを記載します。

  1. 交付先(相手方)の氏名または名称
  2. 売手(自社)の氏名または名称と登録番号
  3. 取引年月日
  4. 取引内容(軽減税率の対象品目である旨)
  5. 10%・8%それぞれの対象となる対価の総額と適用税率
  6. 10%・8%それぞれの消費税額等

この節では5と6を税率別の内訳表で表します。2の登録番号はヘッダー、1は宛先セクションに入れています。3の取引年月日は、発行日と別に必要なら明細や備考に加えてください。

<div class="totals-wrapper">
  <div class="tax-summary">
    <h3>消費税内訳</h3>
    <table class="tax-table">
      <thead>
        <tr><th>税率</th><th>対象金額</th><th>消費税額</th></tr>
      </thead>
      <tbody>
        <tr>
          <td>10%対象</td>
          <td class="text-right">{{tax10_base_formatted}}</td>
          <td class="text-right">{{tax10_amount_formatted}}</td>
        </tr>
        <tr>
          <td>8%対象(軽減税率)</td>
          <td class="text-right">{{tax8_base_formatted}}</td>
          <td class="text-right">{{tax8_amount_formatted}}</td>
        </tr>
      </tbody>
    </table>
  </div>
  <div class="grand-total">
    <table class="total-table">
      <tr>
        <th>小計</th>
        <td class="amount">{{subtotal_formatted}}</td>
      </tr>
      <tr>
        <th>消費税合計</th>
        <td class="amount">{{tax_total_formatted}}</td>
      </tr>
      <tr class="grand-total-row">
        <th>合計金額(税込)</th>
        <td class="amount total">{{grand_total_formatted}}</td>
      </tr>
    </table>
  </div>
</div>
.totals-wrapper {
  display: flex;
  justify-content: flex-end;
  gap: 20pt;
  margin-bottom: 20pt;
}

.tax-summary h3 {
  font-size: 9pt;
  color: #555;
  margin: 0 0 6pt;
}

.tax-table, .total-table {
  border-collapse: collapse;
  font-size: 9pt;
}

.tax-table th, .tax-table td,
.total-table th, .total-table td {
  padding: 4pt 10pt;
  border: 1px solid #ddd;
}

.total-table th {
  text-align: left;
  background: #f5f7fa;
  min-width: 110pt;
}

.total-table .amount {
  text-align: right;
  min-width: 80pt;
}

.grand-total-row th,
.grand-total-row td {
  background-color: #0052cc;
  color: #ffffff;
  font-size: 11pt;
  font-weight: 700;
}

.grand-total-row .amount.total {
  font-size: 13pt;
}

備考・振込先セクション

<section class="notes-section">
  <div class="payment-info">
    <h3>お振込先</h3>
    <p>○○銀行 ××支店 普通口座 1234567</p>
    <p>口座名義: カ)サンプル</p>
    <p class="note">振込手数料はご負担をお願いいたします。</p>
  </div>
  {{#if notes}}
  <div class="custom-notes">
    <h3>備考</h3>
    <p>{{notes}}</p>
  </div>
  {{/if}}
</section>
.notes-section {
  display: flex;
  gap: 20pt;
  font-size: 9pt;
  border-top: 1px solid #ddd;
  padding-top: 12pt;
}

.notes-section h3 {
  font-size: 9pt;
  font-weight: 700;
  margin: 0 0 6pt;
  color: #333;
}

.note {
  color: #888;
  font-size: 8pt;
}

複数ページ対応:ヘッダー・フッターの繰り返し

明細行が多い場合にページをまたぐ際、各ページに列見出しとページ番号を表示します。@page のマージンボックス(@bottom-center など)とページ番号のカウンターは Chrome 131 で入った機能です(Chrome 131 リリースノート)。quality エンジンでも生成環境の Chromium のバージョンに依存し、fast エンジン(wkhtmltopdf)では表示されないため、生成したPDFで確認してください。

/* テーブルヘッダーを各ページの先頭に繰り返す(CSS2.1 では「繰り返してよい」とされ、実際の挙動はエンジンによる) */
.line-items thead {
  display: table-header-group;
}

/* ページフッター */
@page {
  @bottom-center {
    content: counter(page) " / " counter(pages);
    font-size: 8pt;
    color: #aaa;
  }
}

/* ページをまたぐ行の途中での分割を防ぐ(page-break-inside は break-inside の旧名。古いエンジン向けに併記) */
.line-items tbody tr {
  break-inside: avoid;
  page-break-inside: avoid;
}

日本語フォントの設定

PDF出力で日本語を正しく表示するには、フォントを明示的に指定します。生成環境に入っていないフォントは使われず、次の候補に移ります。

/* システムフォントのフォールバックチェーン */
body {
  font-family:
    'Noto Sans CJK JP',
    'Noto Sans JP',
    'Hiragino Kaku Gothic ProN',
    'Hiragino Sans',
    'Yu Gothic Medium',
    'Meiryo',
    sans-serif;
}

/* 金額の桁をそろえる(数字を等幅にする。フォントが対応していれば効く) */
.amount, .col-price, .col-amount {
  font-variant-numeric: tabular-nums;
}

日本語フォントの詳細設定はHTML to PDF 日本語フォントガイドを参照してください。

APIへの送信

作成したHTMLテンプレートに動的データを注入して送信します。

const fs = require('fs');
const Handlebars = require('handlebars'); // npm install handlebars

const template = Handlebars.compile(fs.readFileSync('invoice-template.html', 'utf-8'));

async function generateInvoicePdf(invoiceData) {
  // 行番号({{no}})を1から振ってからテンプレートに渡す。{{...}} の値はHandlebarsがHTMLエスケープする
  const html = template({
    ...invoiceData,
    items: invoiceData.items.map((item, i) => ({ ...item, no: i + 1 })),
  });

  const response = await fetch('https://pdf.funbrew.cloud/api/pdf/generate-from-html', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.FUNBREW_PDF_API_KEY}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      html,
      filename: `invoice-${invoiceData.invoiceNumber}.pdf`,
      options: {
        'page-size': 'A4',
        'margin-top': 20,    // mm(@page の margin と同じ値にする)
        'margin-bottom': 20,
        'margin-left': 15,
        'margin-right': 15,
        engine: 'quality',   // Flexbox・マージンボックスを使うため。Free プランでは 403
      },
    }),
  });

  const result = await response.json();
  if (!response.ok || !result.success) {
    throw new Error(`PDF generation failed: ${response.status} ${result.message ?? ''}`);
  }
  return result.data.download_url; // 生成されたPDFのダウンロードURL
}

APIはPDF本体ではなく、data.download_url などを含むJSONを返します。要素(thead・合計行など)の背景色は quality エンジンでそのまま印刷されますが、html・body に指定した背景は既定では印刷されません(Gotenberg のドキュメント)。背景色は要素側に指定してください。

PlaygroundでHTMLを貼り付けてプレビューすれば、テンプレートの仕上がりをすぐ確認できます。

テンプレートデザインのチェックリスト

  • @page とAPIの options(page-size・margin-*)に同じA4サイズと余白を指定している
  • 日本語フォントのフォールバックを設定している
  • 金額列は右揃えにしている
  • thead に display: table-header-group を設定し、複数ページで繰り返している
  • 税率別の対価の合計と消費税額(10%・8%)を明記している(インボイス制度の記載事項の5・6)
  • 登録番号(「T」+数字13桁)を記載している(インボイス制度の記載事項の2)
  • break-inside: avoid(と旧名の page-break-inside)で行が途中で分割されないようにしている
  • 背景色は html・body ではなく要素(thead・合計行など)に指定している

まとめ

請求書HTMLテンプレートの主要な設計ポイント:

  • @page とAPIの options でA4・余白をそろえる: 生成したPDFで仕上がりを確認する
  • 明細テーブルは縞模様 + 右揃え金額: 視認性の高いデザイン
  • 税率別内訳を必ず掲載: 適格請求書の記載事項(6項目)のうち2つ。ほかの項目もそろえる
  • thead の繰り返し設定: 複数ページでも列ヘッダーを常に表示

テンプレートが完成したら、請求書PDFの自動生成でAPIを使った月次バッチ処理・メール送信の自動化に進みましょう。APIの呼び出し全般はAPIドキュメントで確認できます。PDF生成が完了したらWebhookで完了通知を受け取るフローも合わせて実装することをおすすめします。

関連リンク

Powered by FUNBREW PDF