Generating PDFs in Salesforce

By | September 21, 2015

PDFs are widely used for generating reports or other stuff.

Today, I want generally describe 2 situation, which I encountered during my work and problems I faced solving them. 

First one is generating PDFs in emails.

Second one is generating PDFs in triggers.

Creating PDF as attachment

First, I want to start with basics and show how to generate and attach PDF to any object:

 

PDFGeneratorSample allows you to have simple way of generating PDF, when you have a plain text or HTML as source that you can pass as a parameter to Blob.valueOf method.

Generating PDFs for emails

Here I want to describe two ways in which you can generate PDF attachments for emails.

First way is by code.

To show how to do it, I have made some modifications to the code of PDFGeneratorSample class. To make things a little bit more interesting SendPDFs method will send multiple emails with same content to multiple users.

 

This code will do well, if you  have PDF represented as string.

But, I think it’s not so common to have around string version of needed PDF, the more transparent and flexible will be to use Visualforce email templates.

Visualforce email templates provide clear structure and allow users to edit them in salesforce instead of changing apex code. Templates have special tag <messaging:attachment> that allow creation of attachments. There are some limitation to using visualforce in emails, but most of them can be bypassed. For example you cannot execute apex. But it’s possible to overcome this restriction with visualforce components.

Here is a sample of template that allow to send Birthday greetings card.

To find more information about visualforce templates you can follow official salesforce guide link.

Here is the code for component we use in email template <c:GreetingsCard>

For more info about visualforce components follow this link to official visualforce site.

Here is modified code of PDFGeneratorSample. In this version we pass templateId as parameter to use our visualforce template.

 

To get more info about sending PDF as attachment you can follow this link to official salesforce guide.

Generate PDFs with Visualforce pages from trigger

And now time for the most interesting part. How to generate PDFs with Visualforce pages from trigger.

To get Content of visualforce page you can use getContent or getContentAsPDF methods of PageReference class, but for now they have lots of restrictions about where to use them.

For example, in triggers they will return corrupted file. If use them in future methods and test methods, they will return blank PDF.

To overcome this restriction it is possible to use REST HTTP request, which will be called from future method with callout.

In this example we are going to attach greetings PDF to every contact on its creation.

So closer to details. Lets firstly create some basic Visualforce page for this example and name it WelcomePage.

 

Now it is time for our REST resource (don’t forget to add your organization address to Remote site settings, because otherwise you won’t be able to make HTTP request on your org).

 

CreateReportAttachments creates welcome PDF and attaches it to Contact. Because SObject cannot be passed as parameter to REST resource we serialize contracts first and then deserialize it in POST method.

urlMapping is used to set URL for our REST resource.

Now we should move towards trigger handler and show how to use created REST service.

 

Future notation with callouts enabled is used, because we can’t make callout from trigger context, they currently does not support it.

And now it’s for a final part – the trigger code.

 

Here it is. If you have any question feel free to leave a comment or contact me via email.

Ivan Bakun, Salesforce Developer by CloudWharf
bakun@cloudwharf.com

Leave a Reply

Your email address will not be published. Required fields are marked *