Find your content:

Search form

You are here

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

 
Share

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={!$CurrentPage.parameters.id}" /> 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 http://t.co/w9D8EZaQ

The PageReference getContent and getContentAsPDF methods can't be used with the future annotation. https://t.co/3KKKenxh

Here's a good discussion about it on the Force.com Discussion Boards http://boards.developerforce.com/t5/Apex-Code-Development/Generate-PDF-with-apex-trigger/td-p/481325

This clever idea linked out from the thread works for everything, but doesn't handle images http://corycowgill.blogspot.ch/2012/02/generating-pdf-in-apex-trigger.html

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.

https://twitter.com/pfeilbr/status/244887869947269121

https://gist.github.com/3686866


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="{!relatedTo.name}.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.

Rup


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 https://salesforce.stackexchange.com/questions/1302

My Block Status

My Block Content