I'm currently trying to set up a schedule that will create PDF files and save them as attachments in Salesforce.
Methods I have tried:
PageReference.getContentAsPdf() – works fine by itself but is not supported in schedulable / batchable classes, resulting in blank PDF files.
Blob.toPDF() – This method does not support inline images or CSS, resulting in very basic looking PDFs.
REST API Callout to PDF Crowd (https://pdfcrowd.com/html-to-pdf-api/) – Quickly tried this but it returned a corrupted PDF file. It may work but I haven't had a chance to contact their support to see why the returned file was corrupted.
I've seen a few snippets online of people talking about using webservices, or Force.com sites to achieve this functionality.
Surely there's an easier way to do this without resorting to the likes of Conga etc?
Any help appreciated.
Best Answer
If you're considering calling out to a 3rd party app, you could also consider calling out to your own instance of Salesforce. In this case, you could create a REST service that calls
PageReference.getContentAsPdf()
and then insert the Attachment against the object. When the service is being called like this (i.e. using HTTPRequest callout from your Batch Apex), it doesn't know it is from the Batch context thus does not complain. I have done this in the past successfully.However, to do this, you would need to pass in the Session as initial state to the Batch Apex job (i.e. as part of the constructor so that you have it available to pass to the 'attachment' service. This may or may not present challenges with session timeouts, depending on your use case.
Sample code below, you'll also need to add your salesforce instance to Remote Settings as an external site:
And the Scheduled Batch Apex class which reschedules itself to run every 20 mins:
The the code to call this: