概要
請求書をリアルタイムに生成する機能は、請求システムや e‑コマース、SaaS アプリケーションで頻繁に求められます。
GroupDocs.Assembly Cloud を使えば、サーバーにオフィスソフトをインストールすることなく、完全にクラウド上でプロフェッショナルな請求書を作成できます。
主な特長
- テンプレートの柔軟性 – Microsoft Word、LibreOffice Writer など、好きなデスクトップエディタでレイアウトをデザインできます。
- 動的データバインディング – JSON や XML データをテンプレートに埋め込めます。プレースホルダーだけでなく、LINQ 式による計算やループ、条件分岐もサポートしています。
- 複数の出力形式 – 編集可能な DOCX と、配布用の読み取り専用 PDF のどちらでも生成可能です。
- 安全なストレージ – 認証トークンを使用して、GroupDocs Cloud に保存されているテンプレートや文書へアクセスできます。
- 軽量な実行環境 – すべての処理は GroupDocs Cloud 側で行われるため、サーバー側にサードパーティのライブラリや高負荷の CPU、メモリは不要です。
以下のセクションでは、テンプレート作成からデータソース構築、そして .NET SDK(C#)とシンプルな cURL コマンドの両方を用いた請求書生成までの一連の手順を解説します。
請求書テンプレートの作成
Microsoft Word、LibreOffice Writer など、お好みのデスクトップエディタで請求書レイアウトを作成します。
その後、プレースホルダータグをテンプレートに埋め込みます。例として以下のように JSON フィールドに対応させます。
| テンプレート | JSON パス |
|---|---|
<<[Invoice.Number]>> | invoice.number |
<<[Customer.Name]>> | customer.name |
テーブルでの繰り返し構文は次のように記述します。
| 品目 | 数量 | 単価 | 金額 |
|---|---|---|---|
<<foreach [in Items]>><<[Description]>> | <<[Quantity]>> | $<<[UnitPrice]:"N2">> | $<<[Quantity*UnitPrice]:"N2">><</foreach>> |
ご参考までに、以下の テンプレート例 をダウンロードしてご利用ください。
もしくはご自身でテンプレートを作成し、次の章で GroupDocs Cloud ストレージにアップロードしてください。
テンプレートの Cloud ストレージへのアップロード
C# コードの主な手順
- App SID と App Key を設定した
Configurationオブジェクトを作成する。 - AssemblyApi クライアントを初期化する。
- テンプレートファイルをクラウド上のパス(例:
templates/invoice-template.docx)へアップロードする。
// 1️⃣ Initialize the Cloud API configuration
var config = new Configuration
{
AppSid = "YOUR_APP_SID", // replace with your App SID
AppKey = "YOUR_APP_KEY" // replace with your App Key
};
var assemblyApi = new AssemblyApi(config);
// 2️⃣ Define the cloud path where the template will be stored
string cloudTemplatePath = "templates/invoice-template.docx";
// 3️⃣ Open the local template file (prepare this file beforehand)
using (var templateStream = File.OpenRead("resources/invoice-template.docx"))
{
// 4️⃣ Build the upload request – the API expects a stream + target path
var uploadRequest = new UploadFileRequest(templateStream, cloudTemplatePath);
// 5️⃣ Execute the upload; the template is now available in Cloud storage
assemblyApi.UploadFile(uploadRequest);
}
Tip: コマンドラインで完結させたい場合は、cURL でテンプレートをアップロード する手順(下記「cURL で請求書生成」の章)を参照してください。
データソース(JSON)の作成
請求書データの JSON を作成する
以下はテンプレートのプレースホルダーに対応したサンプル JSON です。invoice-data.json という名前で resources フォルダーに保存するか、コード内に直接埋め込んでください。
{
"invoice": {
"number": "INV-2024-001",
"issueDate": "2024-01-15T00:00:00"
},
"customer": {
"name": "John Smith",
"company": "Acme Corporation",
"email": "john.smith@example.com",
"address": "123 Main Street",
"city": "New York",
"state": "NY",
"zipCode": "10001",
"country": "USA"
},
"items": [
{
"description": "Web Development Services",
"quantity": 40,
"unitPrice": 150.00
},
{
"description": "UI/UX Design",
"quantity": 20,
"unitPrice": 120.00
},
{
"description": "Consulting Services",
"quantity": 10,
"unitPrice": 200.00
}
],
"taxRate": 10.0
}
コードから JSON を使用する例
// Load the JSON data from a file – this string will be passed to the API
string jsonData = File.ReadAllText("resources/invoice-data.json");
Note: cURL でリクエストを送る場合は、JSON をリクエストボディにインラインで記載できます。ファイルに保存しておくと、データが大きくなるケースで便利です。
.NET SDK で請求書を生成
C# コードの主な手順
- テンプレートのアップロード(前述)。
- JSON データを読み込む。
AssembleOptionsを設定し、出力形式(docxまたはpdf)とテンプレートのパスを指定する。AssembleDocumentを呼び出して請求書を生成する。- 生成されたストリームをローカルまたは別のクラウド場所に保存する。
using GroupDocs.Assembly.Cloud.Sdk;
using GroupDocs.Assembly.Cloud.Sdk.Model;
using GroupDocs.Assembly.Cloud.Sdk.Model.Requests;
using System.IO;
// 1️⃣ Initialize the Assembly API (same config as upload step)
var config = new Configuration { AppSid = "YOUR_APP_SID", AppKey = "YOUR_APP_KEY" };
var assemblyApi = new AssemblyApi(config);
// 2️⃣ Load JSON data (see previous section)
string jsonData = File.ReadAllText("resources/invoice-data.json");
// 3️⃣ Prepare assembly options
var assembleOptions = new AssembleOptions
{
// Choose the format you need – "docx" for editable, "pdf" for final PDF
SaveFormat = "pdf",
// Pass the whole JSON string – the API parses it automatically
ReportData = jsonData,
// Reference the template that we uploaded earlier
TemplateFileInfo = new TemplateFileInfo
{
FilePath = "templates/invoice-template.docx" // cloud path
}
};
// 4️⃣ Build the request object
var assembleRequest = new AssembleDocumentRequest(assembleOptions);
// 5️⃣ Execute the request – the response contains the generated file stream
var assembledDocument = assemblyApi.AssembleDocument(assembleRequest);
// 6️⃣ Persist the result locally
using (var outputStream = File.Create("output/invoice-output.pdf"))
{
// Copy the API stream to a file on disk
assembledDocument.CopyTo(outputStream);
}
Result:
invoice-output.pdf(SaveFormatをdocxに変更すれば DOCX が生成されます)には、顧客へ送付可能な完全に埋め込まれた請求書が格納されます。
cURL で請求書を生成
SDK をインストールしたくない、あるいは純粋に HTTP 呼び出しだけで完結させたい場合は、以下の手順で実現できます。
1️⃣ アクセストークンの取得
curl.exe -X POST "https://api.groupdocs.cloud/connect/token" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET" \
-H "Content-Type: application/x-www-form-urlencoded"
レスポンスに含まれる access_token を次のリクエストで使用します。
2️⃣ テンプレートのアップロード(必要な場合)
curl.exe -X PUT "https://api.groupdocs.cloud/v1.0/assembly/storage/file/templates/invoice-template.docx" \
-H "Authorization: Bearer ACCESS_TOKEN" \
-F "File=@resources/invoice-template.docx"
すでにテンプレートがクラウドに保存されている場合はこの手順を省略できます。
3️⃣ 請求書の組み立て
resources フォルダーに作成した invoice-data.json を使用します。
以下は JSON データを一度エスケープして変数に格納し、AssembleDocument エンドポイントへ POST する例です。
REPORT_DATA_ESCAPED=$(python -c "import json,sys; print(json.dumps(open('resources/invoice-data.json','r',encoding='utf-8').read()))")
curl.exe -v "https://api.groupdocs.cloud/v1.0/assembly/assemble" \
-X POST \
-H "Authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: application/json" \
--data-binary @- <<EOF
{
"TemplateFileInfo": {
"FilePath": "/templates/invoice-template.docx"
},
"SaveFormat": "pdf",
"OutputPath": "/output/invoice-generated.pdf",
"ReportData": $REPORT_DATA_ESCAPED
}
EOF
このコマンドにより、請求書が生成され /output/invoice-generated.pdf に保存されます。
4️⃣ 生成された請求書のダウンロード
curl.exe \
"https://api.groupdocs.cloud/v1.0/assembly/storage/file/Output/invoice-generated.pdf" \
-H "Authorization: Bearer ACCESS_TOKEN" \
-H "Accept: application/octet-stream" \
-o invoice-generated.pdf
Result: C# コードを書かずに、コマンドラインだけで完成した請求書(PDF)を取得できます。
まとめ
本稿では GroupDocs.Assembly Cloud を利用した請求書生成の流れを実践的に紹介しました。
- 軽量なクラウド処理 – サーバーに重いツールは不要です。
- テンプレート設計の自由度 – Word/LibreOffice で作成した DOCX をそのまま利用可能。
- 動的データバインディング – JSON(または XML)を直接渡せ、LINQ や計算式、ループ、条件分岐もサポート。
- 多彩な出力形式 – 編集可能な DOCX と、改ざん不可な PDF を選択できます。
- シンプルな統合 – .NET SDK でも、数行の cURL コマンドでも実装可能です。
さあ、テンプレートをアップロードし、AssembleDocument エンドポイントを呼び出すだけで、請求書業務の自動化を今すぐ始めましょう!
参考情報
- GroupDocs.Assembly Cloud API リファレンス – https://reference.groupdocs.cloud/assembly/
- GroupDocs.Assembly Cloud ドキュメント – https://docs.groupdocs.cloud/assembly/
- テンプレート作成クイックスタートガイド – https://docs.groupdocs.cloud/assembly/getting-started/quick-start/
- 高度な要素(バーコード、ブックマーク、ハイパーリンク等)の取り扱い – https://docs.groupdocs.cloud/assembly/developer-guide/working-with-other-elements/
- 高度なテンプレート定義構文の包括的ガイド – https://docs.groupdocs.com/assembly/net/advanced-usage/
よくある質問 (FAQs)
LibreOffice Writer でもテンプレートは作れますか?
はい。DOCX 形式であれば、使用したエディタに関係なく API が正しく処理します。テンプレート内で合計金額や税金の計算は可能ですか?
もちろんです。プレースホルダーに LINQ 式や単純な算術式を書くだけで実現できます(例:<<[item.quantity * item.unitPrice]>>)。テンプレートやデータファイルの最大サイズはどれくらいですか?
500 MB までのファイルをサポートしています。通常の請求書テンプレートはこの上限をはるかに下回ります。アップロード前にクラウドストレージのフォルダーを作成する必要がありますか?
いいえ。指定したパスにフォルダーが存在しない場合、アップロード API が自動的に作成します。無料トライアルはありますか?
はい、月間 150 回の無料 API コールをご利用いただけます。
