+ Start a Discussion
anup ramanup ram 

invalid pdf error for email with pdf attachment

Hi,

I have a requirement to generate a pdf and send a email with thsi pdf attachments.

The pdf is generated and attached and email is sent.

 

when i login as system admin : pdf generated opens in adobe / nitro pdf

 

for other profiles > email is sent with attachments but pdf attachment doesnt open in pdf , it says invalid pdf file...

 

whats the problem...

The profile has access to the page which is rendered as pdf.

 

  PageReference pdf = Page.PdfGenerator;
                pdf.getParameters().put('id',objectId);
                pdf.setRedirect(true);
            // The contents of the attachment from the pdf stored as a Blob
                Blob body;       
                try
                {
                // Get contents of the attachment from the pdf
                    body = pdf.getContent();      
                }
                catch (VisualforceException e)
                {
                    body = Blob.valueOf('Encountered VF exception');
                }
               
system.debug(' body of pdf content ' + body);               
            //Generating a Email PDF attachment
                Messaging.EmailFileAttachment PDFAttachFile = new Messaging.EmailFileAttachment();
                PDFAttachFile.setContentType('application/pdf');
                PDFAttachFile.setFileName('test.pdf');
                PDFAttachFile.setInline(false);
                PDFAttachFile.Body = body;
            // Construct the email envelope
                OrgWideEmailAddress[] orgemail = [select Id,Address from OrgWideEmailAddress where Address = 'some email' limit 1];
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setUseSignature(false);
                if ( orgemail.size() > 0 )
               {
                    mail.setOrgWideEmailAddressId(some id);
                }
                mail.setToAddresses(new String[]{toEmailAddr });
                mail.setSubject('hi 123' );
                mail.setHtmlBody('Hi ');
                mail.setFileAttachments(new Messaging.EmailFileAttachment[] { PDFAttachFile });
            // Send the mail with Attachment
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail});

 

 

why oher profiles other than system admin cant generate a proper pdf?

plz urgent requirement...........................

sfdcfoxsfdcfox
This sounds crazy, I know, but open the PDF file in your web browser or Notepad. What's happening here is you're getting an error and it is thusly creating an HTML page with the error. Adobe/Nitro can't read it, because it's not really a PDF. Your web browser or Notepad will be able to read it just fine, however... Once you fix the error, you'll be able to generate the PDF correctly. It most likely has to do with user permissions (e.g. the user doesn't have permission to view the record and/or some of the fields on the record).
Ajay_SFDCAjay_SFDC

Hi Anup ,

 

Try using 

Blob body;
try {
body = pdf.getContentAsPDF();

}
catch (VisualforceException e) {
body = Blob.valueOf('No content in PDF');
}

 

Let me know if it helps you !!!

 

Thanks