I have a Batch class that calls a Visualforce page using getContentAsPDF() to generate a single PDF. This works flawlessly in a scratch org and in the developer logs I can see a separate call made to the Visualforce page as expected.
In production however, the logs show that the batch runs as expected, records are fetched and processed but a blank file is generated. There are no errors in the production logs and the call to the Visualforce page is never made.
To illustrate and simplify the code I've created another batch class as below and run it against the scratch org and production which yielded the same results:
public class BatchTestPDF implements Database.Batchable<sObject>, Database.AllowsCallouts {
public Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator('SELECT ID FROM Contact WHERE Lead_Score__c = 9999');
}
public void execute(Database.BatchableContext bc, List<Contact> contacts){
// Generate PDF
PageReference pageRef = new PageReference('/apex/TestPDF');
Blob contentBlob = pageRef.getContentAsPDF();
// Save file to contact record
ContentVersion cv = new ContentVersion();
cv.ContentLocation = 'S';
cv.Title = 'Test File';
cv.PathOnClient = 'test.pdf';
cv.VersionData = contentBlob;
insert cv;
cv = [SELECT ContentDocumentId FROM ContentVersion WHERE ID = :cv.Id];
ContentDocumentLink contentDocumentLink = new ContentDocumentLink(
LinkedEntityId = contacts[0].ID,
ContentDocumentId = cv.ContentDocumentId,
ShareType = 'I'
);
insert contentDocumentLink;
}
public void finish(Database.BatchableContext bc){
}
}
TestPDF.page
<apex:page renderAs="pdf" applyHtmlTag="false" showHeader="false" cache="false">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
<body>
<p>This is a test</p>
</body>
</html>
</apex:page>
Am I missing a permission or some other setting in production for this to work?