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.