function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Starz26Starz26 

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;
        
    
    }


 

Best Answer chosen by Admin (Salesforce Developers) 
James LoghryJames Loghry

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.

This method can't be used in:
  • Triggers
  • Scheduled Apex
  • Batch jobs
  • Test methods
  • Apex email services

Looks like you might be out of luck?

All Answers

James LoghryJames Loghry

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.

This method can't be used in:
  • Triggers
  • Scheduled Apex
  • Batch jobs
  • Test methods
  • Apex email services

Looks like you might be out of luck?

This was selected as the best answer
Starz26Starz26

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!

Starz26Starz26

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

 

 

Starz26Starz26

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

Starz26Starz26

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

CaukajunCaukajun

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. 

 

 

 

bakul.patelbakul.patel

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

ksareenksareen

If someone came up with some solution for this please help out with a sample code 

nagalakshminagalakshmi

Hi 

Bruno SoaresBruno Soares
Im getting empty pdf as well. Has someone solved this problem? Im using scheduled class to call a future method using webservice to create multiple pdfs. All empty.
oleksiyoleksiy
Seems to be a workaround: http://www.shivasoft.in/blog/salesforce/apex/send-email-with-generated-pdf-as-attachment-from-trigger (http://www.shivasoft.in/blog/salesforce/apex/send-email-with-generated-pdf-as-attachment-from-trigger" target="_blank)