You need to sign in to do that
Don't have an account?
Starz26
Batch Apex and VF Page getContentasPDF() - Help
I am trying to write a batch apex job that will cycle through a list of records..
For each record, I need to get the VF Document related to it as a PDF and email it as an attachment.
The PDF always comes across as blank. I know I must bemissing something small because the exact same code works in a static apex class......
Can someone please help me resolve this?
Note: the code attached just attaches the file to the object for testing purposes...
this is the execute method. Everything else works except the actual PDF is empty
Map<ID,MYOBJECT__c> mFSA = New Map<ID,MYOBJECT__c>((MYOBJECT__c[])(database.query(query))); PageReference pr; blob pdf; For(FSA__c f : mFSA.values()){ pr = New PageReference('/apex/ReportPDF?id=' + f.id ); pdf = pr.getContentAsPDF(); Attachment a = New Attachment(); a.body = pdf; a.parentID = f.id; a.Name = 'FSA.pdf'; insert a; }
Per the Salesforce documentation..
http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_pagereference.htm
Returns the page as a PDF, regardless of the <apex:page> component's renderAs attribute.
Looks like you might be out of luck?
All Answers
Per the Salesforce documentation..
http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_pagereference.htm
Returns the page as a PDF, regardless of the <apex:page> component's renderAs attribute.
Looks like you might be out of luck?
Ahhh.... my brain read that as can, not can't!
I am sure the renderAs in the apex:page tag has the same limitation.....
Time to find a way around this!
Ugh...
It seems even in @future apex method it returns empty pdf.
Seems the only wat to get content as a pdf is user interaction through the UI or apex class called from a controller....
Well it seems this is not possible in any way:
The old solution of using a scheduled apex class to call an @future method that called a webservice method does not work.
Using inbound email handler to run the class does not work.
Using inboud to call @future method that calls the class does not work..
The only way to do this seems to be a syncronous method using apex or ui. Which means I would have to manuall do this once a week..
I fail to see why I cannot send a PDF of a VF page in an email on a scheduled basis....
This is frustrating.
Anyone have any ideas or been successful, maybe I missed something
The only solution I can come up with is that ANYTIME someone edits a record, I redirect to a vf page that calls a method to create the pdf, store it, and during the weekly batch send the most recent and then delete them all.
What a waste of space, processing, time, etc.
ugh.....that means I would have to create a visualforce page to override the default so I can redirect to a new page, etc....... what a mess....
sorry, venting
Starz26,
I recently ran into the same issue and I wanted to clarify a few things that I learned during this process. First of all, you are correct in that you cannot call getContentAsPdf() inside of a future method, however, there are ways to get around this. I was able to side step this by calling getContentAsPdf in a non-future method and simply passing the returned content blob to the future method to be shipped off via web service callout.
If you are sending out this PDF to your customer or some other contact, then you might also look into exposing the VF page through sites and then sending link to customer so that they can download the pdf.
Bakul
If someone came up with some solution for this please help out with a sample code
Hi Starz26,
I am also facing same problem. I am also getting empty pdf. How can you solve this. Please help me out. Its very urgent.
Thanks,
Lakshmi.