2026/03/26
請求書PDFの自動生成をAPIで実現する方法【コード付き】
「毎月の請求書作成に何時間もかかっている」「Excelで手作業してミスが発生した」――こんな経験はありませんか?
PDF生成APIを使えば、請求データを渡すだけで請求書PDFの生成からメール送信まで自動化できます。この記事では、実際のコード例を交えて実装方法を解説します。
なぜAPIで自動化するのか
手作業の問題点
- 時間がかかる: 1件ずつExcelやWordで作成 → PDF変換 → メール添付
- ミスが起きやすい: 金額の転記ミス、宛名の間違い
- スケールしない: 顧客が増えると作業量が線形に増える
API自動化のメリット
- 数秒で完了: データベースの値からPDFを自動生成
- ミスゼロ: プログラムが計算・レイアウトするので転記ミスなし
- スケーラブル: 100件でも10,000件でも同じコードで処理
実装の全体像
注文データ → HTMLテンプレートに流し込み → PDF生成API → メール送信
必要な要素は3つだけです:
- HTMLテンプレート — 請求書のレイアウト
- データ — 顧客名、明細、金額
- API呼び出し — PDFの生成とメール送信
Step 1: HTMLテンプレートを作る
まず、請求書のHTMLテンプレートを用意します。FUNBREW PDFのテンプレートエンジンを使えば、{{変数名}}で動的な値を埋め込めます。
<div style="font-family: 'Hiragino Sans', sans-serif; max-width: 800px; margin: 0 auto; padding: 40px;">
<div style="display: flex; justify-content: space-between; margin-bottom: 40px;">
<div>
<h1 style="font-size: 28px; margin: 0;">請求書</h1>
<p style="color: #6b7280;">請求番号: {{invoice_number}}</p>
<p style="color: #6b7280;">発行日: {{issue_date}}</p>
</div>
<div style="text-align: right;">
<p style="font-weight: 700;">株式会社サンプル</p>
<p style="color: #6b7280; font-size: 13px;">東京都渋谷区...</p>
</div>
</div>
<div style="margin-bottom: 32px;">
<p style="font-size: 18px; font-weight: 700;">{{customer_name}} 御中</p>
</div>
<table style="width: 100%; border-collapse: collapse; margin-bottom: 32px;">
<thead>
<tr style="background: #f8fafc;">
<th style="text-align: left; padding: 12px; border-bottom: 2px solid #e5e7eb;">品目</th>
<th style="text-align: right; padding: 12px; border-bottom: 2px solid #e5e7eb;">数量</th>
<th style="text-align: right; padding: 12px; border-bottom: 2px solid #e5e7eb;">単価</th>
<th style="text-align: right; padding: 12px; border-bottom: 2px solid #e5e7eb;">小計</th>
</tr>
</thead>
<tbody>{{line_items}}</tbody>
</table>
<div style="text-align: right; margin-bottom: 32px;">
<p>小計: ¥{{subtotal}}</p>
<p>消費税 (10%): ¥{{tax}}</p>
<p style="font-size: 20px; font-weight: 700;">合計: ¥{{total}}</p>
</div>
<div style="border-top: 1px solid #e5e7eb; padding-top: 16px; color: #9ca3af; font-size: 12px;">
<p>お支払い期限: {{due_date}}</p>
<p>振込先: サンプル銀行 渋谷支店 普通 1234567</p>
</div>
</div>
このテンプレートをFUNBREW PDFのダッシュボードで登録しておきます。
Step 2: APIで請求書PDFを生成する
JavaScript (Node.js)
const response = await fetch("https://pdf.funbrew.cloud/api/pdf/generate-from-template", {
method: "POST",
headers: {
"Authorization": "Bearer sk-your-api-key",
"Content-Type": "application/json",
},
body: JSON.stringify({
template: "invoice",
variables: {
invoice_number: "INV-2026-0042",
issue_date: "2026/03/26",
customer_name: "株式会社テスト",
line_items: buildLineItemsHtml(items),
subtotal: "150,000",
tax: "15,000",
total: "165,000",
due_date: "2026/04/30",
},
email: {
to: "customer@example.com",
subject: "【請求書】2026年3月分のご請求",
},
options: { engine: "quality" },
}),
});
const result = await response.json();
console.log("PDF URL:", result.data.download_url);
Python
import requests
response = requests.post(
"https://pdf.funbrew.cloud/api/pdf/generate-from-template",
headers={"Authorization": "Bearer sk-your-api-key"},
json={
"template": "invoice",
"variables": {
"invoice_number": "INV-2026-0042",
"issue_date": "2026/03/26",
"customer_name": "株式会社テスト",
"line_items": build_line_items_html(items),
"subtotal": "150,000",
"tax": "15,000",
"total": "165,000",
"due_date": "2026/04/30",
},
"email": {
"to": "customer@example.com",
"subject": "【請求書】2026年3月分のご請求",
},
"options": {"engine": "quality"},
},
)
print("PDF URL:", response.json()["data"]["download_url"])
PHP (Laravel)
$response = Http::withToken('sk-your-api-key')
->post('https://pdf.funbrew.cloud/api/pdf/generate-from-template', [
'template' => 'invoice',
'variables' => [
'invoice_number' => 'INV-2026-0042',
'issue_date' => '2026/03/26',
'customer_name' => '株式会社テスト',
'line_items' => $this->buildLineItemsHtml($items),
'subtotal' => '150,000',
'tax' => '15,000',
'total' => '165,000',
'due_date' => '2026/04/30',
],
'email' => [
'to' => 'customer@example.com',
'subject' => '【請求書】2026年3月分のご請求',
],
'options' => ['engine' => 'quality'],
]);
$downloadUrl = $response->json('data.download_url');
Step 3: バッチ処理で月次請求を一括生成
月末に全顧客分の請求書を一括生成するなら、バッチAPIが便利です。
// 全顧客の請求データを配列で渡す
const response = await fetch("https://pdf.funbrew.cloud/api/pdf/batch", {
method: "POST",
headers: {
"Authorization": "Bearer sk-your-api-key",
"Content-Type": "application/json",
},
body: JSON.stringify({
items: customers.map(c => ({
type: "template",
template: "invoice",
variables: buildInvoiceVars(c),
filename: `invoice-${c.id}-202603.pdf`,
email: {
to: c.email,
subject: `【請求書】${c.name}様 2026年3月分`,
},
})),
}),
});
// 結果: 全件の生成結果が返る
const { data } = await response.json();
console.log(`${data.results.length}件の請求書を生成しました`);
Webhookで生成完了を通知
PDF生成が完了したらSlackやシステムに通知したい場合は、Webhookを設定できます。
ダッシュボードの「Webhook」設定でURLを登録すると、PDF生成完了時に以下のようなPOSTリクエストが届きます:
{
"event": "pdf.generated",
"data": {
"filename": "invoice-42-202603.pdf",
"file_size": 48210,
"download_url": "https://pdf.funbrew.cloud/dl/abc123..."
}
}
まとめ
PDF生成APIを使えば、請求書の作成・送付を完全に自動化できます。
- テンプレートを一度作れば再利用可能
- APIを呼ぶだけで生成 + メール送信
- バッチAPIで月次処理も一括
- Webhookで完了通知
まずは無料プラン(月30件)で試してみてください。テンプレートエディタで請求書テンプレートを作成し、Playgroundで動作を確認できます。