Find your content:

Search form

You are here

How to download a VF page as PDF and email it from a trigger?


I have a renderAsPDF VF page that takes an object id parameter as part of the URL string. It returns a PDF with some textual data and some images (e.g. <apex:image id="theImage" value="/servlet/servlet.FileDownload?file={!$}" /> that point to image/png Attachements ). Within a trigger, I'm attempting to download the VF page as PDF, but unable to do it.

I asked on twitter and got some good suggestions, but none of them solved the problem. Here are some links from the conversation

The PageReference.getContentAsPDF can't be called from a trigger

The PageReference getContent and getContentAsPDF methods can't be used with the future annotation.

Here's a good discussion about it on the Discussion Boards

This clever idea linked out from the thread works for everything, but doesn't handle images

I posted a this code which works, but not sure it'll stand the test of time. Relies on grabbing session ids out of cookies, and following redirects.

Attribution to: pfeilbr

Possible Suggestion/Solution #1

If you're open to using a visualforce email template* instead of a visualforce page to generate the PDF then you have a good option: generate the PDF on the fly as part of the template.

This is in fact fully supported via the use of the messaging:attachment tag, like so:

<messaging:attachment renderAs="pdf" filename="{!}.pdf">content</messaging:attachment>

I've used this before and I know there are ISV applications that rely on this functionality so it should be supported and safe to use if your use case allows.

*caveats include:

  • You cannot use a custom controller for the template (although you can use custom components with apex controllers).
  • You cannot perform DML in an email template like you could in a page.

Attribution to: ca_peterson

Possible Suggestion/Solution #2

This is typically a good field of action for Conga Composer.

A trigger detecting incoming info could trigger Conga Composer : this solution will easily assemble anything for you from one or several SObjects, collate them in a PDF; it can attach the PDF to a record, send it by email to a contact, etc., etc.

We are only users of Conga Composer, but it is such a great solution that you should try it if you have no other solution.


Attribution to: Rup

Possible Suggestion/Solution #3

Would scheduled Apex be able to work around the limitations of the trigger? The trigger could flag the records needed for action, and a scheduled task could generate the actual email...

Attribution to: joshbirk
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content