1

I have a VF page, where I am not using renderas "PDF". In the single email messaging method of Controller, I am using the Attachment as pdf of the VF page.

I used getContent() function which generated a PDF, but when I tried opening it, received an error saying 'The file decoding was incorrect' .

I used getContentasPDF() which gave me the error saying 'PDF generation failed. Check the page markup is valid.'

Used renderAs= "PDF" in VF page, still received the same error 'PDF generation failed. Check the page markup is valid.'

Here is the Controller

Attachment pdfAttachment = new Attachment();

            String orderID = ApexPages.CurrentPage().getparameters().get('id');

            system.debug('Id for attchement' +orderID);

            PageReference pdf = Page.SendInvce;

            pdf.getParameters().put('id',(String)orderID);

            pdf.setRedirect(true);

            Blob b = pdf.getContentAsPDF();

    Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();

    efa.setFileName('Invce.pdf');

    efa.setBody(b);

    EmailTemplate templateId = [Select id from EmailTemplate where name =  'Email template'];

    Contact c = [select id, Email from Contact where email <> null limit 1];

     email.setTargetObjectId(c.Id);

     email.setTreatTargetObjectAsRecipient(false);

     email.setTemplateID(templateId.Id);

email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});

Greatly appreciate any help

  • 2
    Usually such error occur when you pass an incorrect Id to the getContent() method call. Check if it is correct? Also note that getContent is now treated as a callout. – Raul Dec 11 '17 at 23:17
  • I did check the ID passed and I was able to get the correct Id – user5432 Dec 11 '17 at 23:47
  • 1
    Can you access the PDF in the browser directly via "/apex/SendInvce?id=<YOUR_ID>"? (Of course replace <YOUR_ID> with a proper ID...) – Christoph Dec 12 '17 at 10:29
  • yes I can access it when I use getContent(). If I use getContentaspdf() then I get the same error message on the UI – user5432 Dec 12 '17 at 18:33

Your Answer

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

Browse other questions tagged or ask your own question.