For the complete documentation index, see llms.txt. This page is also available as Markdown.

Batching & Combining Documents

Why?

A common business requirement is the "Bulk Print" or "Monthly Statement" run. Generating 500 individual PDFs is useful for digital records, but if those documents need to be printed and mailed, or sent as a single legal packet, the user needs them merged into one continuous file.

The Batch SDK allows you to automate the generation of multiple records while the CombinedDocumentHandler handles the heavy lifting of merging those files into a single PDF.

What: The "Handler" Pattern

When using generateDocsInBatch, you can pass a list of BatchActions. The SDOC.CombinedDocumentHandler is a specialized action that listens for the completion of each document in the batch and appends it to a "Master" PDF.


Solve: Implementation Steps

1. Identify the Target Records

Collect the IDs of the records you wish to process.

Java

List<Account> overdueAccounts = [SELECT Id FROM Account WHERE Status__c = 'Overdue'];
List<Id> accountIds = new List<Id>();
for(Account acc : overdueAccounts) {
    accountIds.add(acc.Id);
}

2. Configure the Combined Handler

Instantiate the handler and define your output settings.

Java

3. Execute the Batch

Pass the handler into a list of BatchActions and invoke the SDK.

Java


Technical Behavior

  • Output Location: By default, the combined PDF is attached to the first record in the accountIds list.

  • Large Batches: If the combined file becomes too large (exceeding Salesforce file limits), the SDK will automatically "split" the output into multiple combined files (e.g., Part 1, Part 2).

  • Execution: This process is asynchronous. You can monitor progress via Setup > Apex Jobs.


⚠️ Constraints & Best Practices

  • PDF Only: The CombinedDocumentHandler currently only supports merging PDF and PDF-UPLOAD templates.

  • Naming Conventions: Ensure your fileName does not contain special characters or file extensions (like .pdf), as the SDK appends the extension automatically.

  • Heap Limits: Merging 500+ pages can occasionally hit Apex Heap limits. If you encounter failures on very large runs, try reducing the number of records in the initial List<Id>.

  • Template Consistency: For the best visual results, ensure all templates being merged share the same page size (e.g., all Letter or all A4).

Last updated

Was this helpful?