Finally: generate PDF documents in Batch Apex

Before, we lacked a friendly way to explain changes to customer accounts. We relied on exported Excel reports and complex statements with historical tracking, but neither was satisfactory to the customer. Batch PDF helped us increase customer satisfaction through added transparency. Now, we schedule the generation of simplified customer statements. Whenever questions come up, we easily pull up the "before" and "after" PDF copy of the statement and present to the customer a clear picture of what items had been added to their statement and when the addition occurred.
- Calle Toren, Finance Manager at Hult International Business School AG

BatchPDF is available on AppExchange from Salesforce. Simply install into your organization; then you can use it in batch apex, triggers, future contexts, scheduled apex or queueable jobs.

Unlike off-platform systems, there is no integration required. It is a drop-in replacement for PageReference 'getContent' or 'getContentAsPdf' API that you will be familiar with, but invocable from batch context.

Perfect for:

  • compliance audit
  • reporting snapshots
  • invoices and statements
batch pdf appexchange

Installation

BatchPDF is easily installed from the AppExchange.
Please pay attention to all steps during the installation wizard.

1. Get It Now from the Salesforce AppExchange

install step one

 

2. Click 'Install' to deploy the package

install step two

 

3. Follow the on-screen instructions

install step three

 


Tutorial

1. Create your Visualforce Page, eg 'OpportunityDetail'

<apex:page renderAs="pdf" standardController="Opportunity">
    <apex:detail />
</apex:page>

 

2. Write your Batch Apex Class, eg 'OpportunityPrinter'

public class OpportunityPrinter implements Database.Batchable<SObject>, Database.AllowsCallouts {
    
    public Database.QueryLocator start(Database.BatchableContext context) {
        return Database.getQueryLocator([SELECT Id, Name FROM Opportunity LIMIT 10]);
    }
    
    public void execute(Database.BatchableContext context, List<Opportunity> scopes) {
        Opportunity opportunity = scopes[0];
        
        //use the relative url of your page
        BatchPDF.PageReference pr = new BatchPDF.PageReference('/apex/OpportunityDetail');
        pr.getParameters().put('id', opportunity.Id);
        Blob data = pr.getContent();
        
        //save the PDF as an attachment on the record
        insert new Attachment(
            Name = 'detail.pdf',
            Body = data,
            ParentId = opportunity.Id
        );
    }
    
    public void finish(Database.BatchableContext context) {
        //
    }
}

 

3. Execute your Batch Apex, eg from Developer Console

OpportunityPrinter batch = new OpportunityPrinter();
Database.executeBatch(batch, 1);

 

4. Verify your generated PDF output

tutorial step one

In this example, the Notes & Attachments related list now contains 'detail.pdf' showing your Opportunity:

tutorial step two