2

I have a requirement where if I edit /insert a record, every time a pdf containg the field's value of that object will be generated and send as an attachment to the user. I wrote the below code.

     List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
for(Order_vod__c ord : trigger.new)
{   
    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
    attach.setContentType('application/pdf');
    attach.setFileName('Employee.pdf');

    String body;

    body = '<html><h1 style=\"text-align:center;\">Employee Information</h1><br/><br/><table align=\"center\"><tr><td>Employee Name</td><td>' + ord.Name + '</td></tr><tr><td>Age</td><td>';
    System.debug('HTML is ' + body);

    attach.Body = Blob.toPDF(body);

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(new String[] {'apurba.roy@accenture.com});
    mail.setSubject('PDF Generation');
    mail.setHtmlBody('PFA');
    mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });     

    mails.add(mail);  
}
if(!mails.isEmpty())
{
    Messaging.SendEmail(mails);
}

But I am getting an error at the line - "attach.Body = Blob.toPDF(body)". An Error Occured while parsing the input string.

Please let me know if there is any alternative! TIA.

  • You cannot just convert an HTML string to pdf this way. If you use this function the parameter passed to the toPDF() function should be an actual string representation of a pdf file. – rael_kid Apr 22 '14 at 13:26
  • Hi Lex, thanks. what should be the way then? can you suggest some pseudo code lines? – user4644 Apr 22 '14 at 13:29
  • 2
    @CRMGuy This method can turn HTML into PDF - see e.g. corycowgill.blogspot.ie/2012/02/…. The problem is probably that your HTML does not have a full set of matching start and end tags - I suggest you ensure your HTML is clean. – Keith C Apr 22 '14 at 13:39
  • @KeithC, wow, didn't know that. That's actually pretty nice – rael_kid Apr 22 '14 at 14:45
1

Looks like your HTML is badly formed (as Keith C mentions in the comments) - you're missing the <body> tag and you aren't closing the <table> or <html> tags. Try replacing the body = line with:

body = '<html><body><h1 style=\"text-align:center;\">Employee Information</h1><br/><br/><table align=\"center\"><tr><td>Employee Name</td><td>' + ord.Name + '</td></tr><tr><td>Age</td><td></td></tr></table></body></html>';
| improve this answer | |
1

Here's the code...

List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();

for(Order_vod__c ord : trigger.new)
{    
    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
    attach.setContentType('application/pdf');
    attach.setFileName('Emplo`enter preformatted text here`yee.pdf');

    String body;

    body = '<html><h1 style=\"text-align:center;\">Employee Information</h1><br/><br/><table align=\"center\"><tr><td>Employee Name</td><td>' + ord.Name + '</td></tr><tr><td>Age</td><td>' </table></html>';
    System.debug('HTML is ' + body);

    attach.Body = Blob.toPDF(body);

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setToAddresses(new String[] { 'apurba.roy@accenture.com' });
    mail.setSubject('PDF Generation');
    mail.setHtmlBody('PFA');
    mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });     

    mails.add(mail);  
}
if(!mails.isEmpty())
{
    Messaging.SendEmail(mails);
}
| improve this answer | |
  • Thanks, one more question. Sinec the Salesforce object has diffenent fields based on page layouts. Is it possible to dynamically generate the PDF contents based on the fields available for a particular page. – user4644 Apr 23 '14 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy