+ Start a Discussion
ShaTShaT 

How to Get PDF Content

Hi ,

 

I have created a Batch class ,in which i m calling a visual force page which is rendred as PDF.

i m attaching it to mail. but content is not displayed.  I tried both getContent() and getContentAsPDF().

In get content it gives me error that Email Attachment was not correctly decoded and in getContentAsPDF() it gives a blank PDF.

 

 

Code -:

                            PageReference newPage = Page.SendInvoice;
                            newPage.getParameters().put('id',o.id);
                           
                            Blob blobFormPDF = null;
                            try {                              
                              blobFormPDF = newPage.getContent();  
                            } catch (VisualforceException e) {
                              blobFormPDF = Blob.valueOf('Some Text');
                            }                              
                            system.debug('---------blobFormPDF'+blobFormPDF);                        
                            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
                            attach.setContentType('application/pdf');
                            attach.setFileName(o.Name+ '.pdf');
                            attach.setInline(false);
                            attach.setBody(blobFormPDF);               
                            
                            Messaging.Singleemailmessage mail= new Messaging.Singleemailmessage();                          
                            list<string>maillist = new list<string>();
                            maillist.add(c.email);
                            mail.setSubject('Invoice');
                            
                            mail.setPlainTextBody('Please find Attached Invoice');
                            mail.setToAddresses(maillist);
                            mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });                       
                            Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});   

 

How can i get the content in mail as attachment?

 

Thanks

Shailu

 

Navatar_DbSupNavatar_DbSup

Hi,

 

I have tested your code in my Org and it works fine. Make sure your mail list have Email addresses  and o.Name have some value. 

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

bob_buzzardbob_buzzard

From the Apex Developer's Guide:

 

You cannot use the getContent and getContentAsPDF PageReference methods in a batch job.    

 

DrBixDrBix

Yes, I've recently run into this situation myself.  I "think" the issue is that there's no HTTP context for the generation of the PDF.  What I ran into was that my batch job would generate invalid PDF because, and again, this is just an assumption, that the salesforce login page is being rendered by the batch job because there's no logged in user.  I've looked at what does get generated and it seems to be the same javascript that you find in the login page, which is why I came to that conclusion.  I actually think something like this "could" work if you could, in the batch job, authenticate with Salesforce.  Doing this would create a HTTP session in the batch job and you could then use that to do the PDF generation.  Again, this is just an educated guess and I'm looking at using a different approach for doing this that doesn't involve a batch job or @future method.

Chat84Chat84
DrBix- Ever make any progress on the workaround? I came to a similar conclusion a few weeks ago before finding this post... Thanks
Prasanth Krishna 10Prasanth Krishna 10

DrBix is have any another way to get Contents in Batch class .. ? 

Chat84 could you tell me what is that conclusion that you found ?