1

Good day Everyone. I am trying to create a functionality that will create a pdf and make it as an attachment. It is working fine when you are an authenticated user however when I try to create a public site and from their call the class that will create a pdf, the pdf is created but when you try to open it, an error will show saying "Failed to load PDF document.". What are the possible reason of this error ? Please help. thank you. Below is my sample code.

Class:

global class AS_pdfTriggerController{
 @Future(callout=true) @auraEnabled
    webservice static void addinvoicePDFandEmail(list<id> IdList){
        Account acc = new Account();
        acc.Name = 'testing for PDF';
        acc.RecordTypeId = '0125D000000Ck2C';
        Insert acc;

         //create a pageReference instance of the VF page.
        pageReference pdf = Page.testPdf;
        //pass the Account Id parameter to the class.
    //    pdf.getParameters().put('id',orderId);
        Attachment attach = new Attachment();
        Blob body;
        if(!test.isRunningTest()){
            body = pdf.getContent();
        }else{
            body=blob.valueOf('TestString');
        }
        attach.Body = body;
        attach.Name = 'testPDF.pdf';
        attach.IsPrivate = false;
        attach.ParentId = acc.Id;
        insert attach;
}
}

testPdf PAGE:

<apex:page showHeader="false" renderAs="pdf">
   THIS IS FOR TESTING
</apex:page>
1

There might be chances that you are missing the required permission of Account object for this guest user.

So first check your guest user have all required permission.

Then you need to make sure The Guest User is able to directly access the visualforce page which you render as PDF. So in order to provide access to it you have to construct a URL for it. If the Guest User finds the path through its URL it will support the call getContentAsPDF(); and values will be present in the PDF.

  • 1
    Thank you so much mate. I already check the permission and it is fine. I also already added the visualforce page. What I am missing is that I use the url for authenticated user and not for guest I just realized it after viewing your comment. – Hope Mar 1 '18 at 3:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.