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.



  • Why would you want to do this? A better solution would be to re-define the problem to use workflows and email templates.
    – Anup
    Sep 10 '12 at 15:14
  • 1
    Anup - Good question. Maybe I'm coming at it from the wrong angle. The requirement I'm working with is that a user is using a disconnected mobile salesforce client where they record a transaction that includes a signature as a png. Once that transaction hits the salesforce backend, the transaction textual data + the signature image needs to be rendered in a single PDF and emailed.
    – pfeilbr
    Sep 10 '12 at 15:49
  • I haven't tried this but I believe it is technically possible to implement this using workflow rule fired when the transaction data is created and/or edited, which then sends an email using the template.
    – Anup
    Sep 10 '12 at 15:52
  • Myself and a handful of others I work with have been trying to figure out a way to do this from batch apex for a while now without much luck - hitting the same issues you mentioned. I strongly suspect that your HTTP callout to the visualforce system may well be the best option for now if you have to use a page. Sep 11 '12 at 17:43

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.

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.


  • 1
    Thanks Rup. I appreciate the response. Looks like that'd work. Unfortunately I'm constrained to using what comes with salesforce and don't have the option to leverage other services.
    – pfeilbr
    Sep 12 '12 at 14:20

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...

  • 1
    Thanks for the suggestion. I did try this out, but ran into the same issue. Rendering a vf page that includes dynamic images as a PDF when a browser isn't initiating the process seems to be the hurdle. The solution I landed on (mentioned in the question) uses the HTTP classes to mimic the behavior of a browser.
    – pfeilbr
    Sep 13 '12 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.