> For the complete documentation index, see [llms.txt](https://help.sdocs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.sdocs.com/developer-hub/document-workflows/software-development-kit/implementation-solves/batching-and-combining-documents.md).

# 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

```
// Define the merge logic
SDOC.CombinedDocumentHandler myHandler = new SDOC.CombinedDocumentHandler();
myHandler.fileName = 'Monthly_Invoices_Batch_' + Date.today().format(); // No extension
```

**3. Execute the Batch**

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

Java

```
// Prepare the action list
List<SDOC.BatchAction> actions = new List<SDOC.BatchAction>{ myHandler };

// Execute: (Record IDs, Template IDs, Batch Size, Actions)
Id jobId = SDOC.DocumentSDK.generateDocsInBatch(
    accountIds, 
    new Set<Id>{'00Xxxxxxxxxxxxx'}, 
    5, 
    actions
);
```

***

#### 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).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.sdocs.com/developer-hub/document-workflows/software-development-kit/implementation-solves/batching-and-combining-documents.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
