+ Start a Discussion

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;
                            Blob blobFormPDF = null;
                            try {                              
                              blobFormPDF = newPage.getContent();  
                            } catch (VisualforceException e) {
                              blobFormPDF = Blob.valueOf('Some Text');
                            Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
                            attach.setFileName(o.Name+ '.pdf');
                            Messaging.Singleemailmessage mail= new Messaging.Singleemailmessage();                          
                            list<string>maillist = new list<string>();
                            mail.setPlainTextBody('Please find Attached Invoice');
                            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?








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. 


From the Apex Developer's Guide:


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



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.

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 ?