請求書テンプレートの作成

Adobe文書生成APIでは、Microsoft Wordベースのテンプレートと、JSONドキュメントを使用して、動的なPDFまたはWordドキュメントを作成する必要があります。 請求アプリケーション用のMicrosoft Wordテンプレートを作成し、無料のDocument Generation Taggerアドインを使用してテンプレートタグを作成します。 アドインをインストールして、Microsoft Wordでタブを開きます。

Document Generation Taggerアドインのスクリーンショット

上記のようにJSONコンテンツをアドインにペーストしたら、「タグを生成」をクリックします。 これで、このプラグインはオブジェクトの形式を表示します。 基本テンプレートでは、お客様の名前とメールアドレスを使用できますが、注文情報は表示されません。 注文情報については、このチュートリアルの後半で説明します。

Document Generation Tagger Authorテンプレートのスクリーンショット

Microsoft Word文書の中で、請求書テンプレートの記述を開始します。 動的データを挿入する位置にカーソルを置き、Adobeアドインウィンドウからタグを選択します。 [テキストの挿入] ​をクリックすると、Tagger Document Generation TaggerアドインでAdobeを生成および挿入できます。 カスタマイズするには、お客様の名前とメールアドレスを入力します。

次に、新しい請求書ごとに変更されるデータに移動します。 アドインの[詳細設定]タブを選択します。 顧客が注文した製品に基づいて動的テーブルを生成するために使用可能なオプションを表示するには、[テーブルとリスト]をクリックします。

最初のドロップダウンから「注文」を選択します。 2番目のドロップダウンで、このテーブルの列を選択します。 このチュートリアルでは、表をレンダリングするオブジェクトの3つの列をすべて選択します。

[Document Generation Tagger Advanced]タブのスクリーンショット

Document Generation APIは、配列内の要素の集約などの複雑な操作も実行できます。 [詳細設定]タブで、[数値計算]を選択し、[集計]タブで、計算を適用するフィールドを選択します。

Document Generation Taggerの数値計算のスクリーンショット

計算の挿入 ​ボタンをクリックして、このタグを文書内の必要な場所に挿入します。 次のテキストがMicrosoft Wordファイルに表示されます。

Microsoft Word文書のタグのスクリーンショット

この請求書サンプルには、顧客情報、注文済製品、および合計未払金額が含まれています。

Adobe文書生成APIを使用した請求書の生成

Adobe PDF Services Node.js software development kit(SDK)を使用して、MicrosoftのWord文書とJSON文書を組み合わせます。 Document Generation APIを使用して、Node.jsアプリケーションを構築して請求書を作成します。

PDFサービスAPIにはDocument Generation Serviceが含まれているため、両方に同じ資格情報を使用できます。 6か月間の無料体験版を利用した後、文書のトランザクション1件につきわずか0.05ドルをお支払いください。

PDFを結合するコードは次のとおりです。

async function compileDocFile(json, inputFile, outputPdf) {
    try {
        // configurations
        const credentials =  adobe.Credentials
            .serviceAccountCredentialsBuilder()
            .fromFile("./src/pdftools-api-credentials.json")
            .build();

        // Capture the credential from app and show create the context
        const executionContext = adobe.ExecutionContext.create(credentials);

        // create the operation
        const documentMerge = adobe.DocumentMerge,
            documentMergeOptions = documentMerge.options,
            options = new documentMergeOptions.DocumentMergeOptions(json, documentMergeOptions.OutputFormat.PDF);

        const operation = documentMerge.Operation.createNew(options);

        // Pass the content as input (stream)
        const input = adobe.FileRef.createFromLocalFile(inputFile);
        operation.setInput(input);

        // Async create the PDF
        let result = await operation.execute(executionContext);
        await result.saveAsFile(outputPdf);
    } catch (err) {
        console.log('Exception encountered while executing operation', err);
    }
}

このコードは、入力JSONドキュメントと入力テンプレートファイルから情報を取得します。 次に、文書の結合処理を行い、複数のファイルを1つのPDFレポートにまとめます。 最後に、API資格情報を使用して操作が実行されます。 まだお持ちでない場合は、資格情報を作成してください(Document GenerationおよびPDFサービスAPIで同じ資格情報を使用します)。

ドキュメント要求を処理するには、Expressルータ内で次のコードを使用します。

// Create one report and send it back
try {
    console.log(\`[INFO] generating the report...\`);
    const fileContent = fs.readFileSync(\`./public/documents/raw/\${vendor}\`,
    'utf-8');
    const parsedObject = JSON.parse(fileContent);

    await pdf.compileDocFile(parsedObject,
    \`./public/documents/template/Adobe-Invoice-Sample.docx\`,
    \`./public/documents/processed/output.pdf\`);

    await pdf.applyPassword("p@55w0rd", './public/documents/processed/output.pdf',
    './public/documents/processed/output-secured.pdf');

    console.log(\`[INFO] sending the report...\`);
    res.status(200).render("preview", { page: 'invoice', filename: 'output.pdf' });
} catch(error) {
    console.log(\`[ERROR] \${JSON.stringify(error)}\`);
    res.status(500).render("crash", { error: error });
}

このコードが実行されると、提供されたデータに基づいて、動的に生成された請求書を含むPDF文書が提供されます。 サンプルのJSONデータ(上記で提供)を使用すると、このコードの出力は次のようになります。

動的に生成されたPDF請求書のスクリーンショット

この請求書には、JSON文書の動的データが含まれています。