+ Start a Discussion
Soumya Srivastava 1Soumya Srivastava 1 

Not getting the content in PDF through apex in Attachment


I have an issue with PDF content as we have created an autmation process where we have created quick action on opportunity tab which is creating  quotes and quotelineitem and then redirecting to another VF page having Quote as standard controller and generating PDF. We are not getting the whole content in the attached on Quote Object whose id we are passing in attachment in the below part of code.

We are calling attachQuote method from action of a VF page(Parent) :

 /* The action method that will generate a PDF document from the QuotePDF page and attach it to 
       the quote provided by the standard controller. Called by the action binding for the attachQuote
       page, this will do the work and take the user back to the quote detail page. */
        public PageReference attachQuote() {
        /* Get the page definition */
        PageReference pdfPage = Page.quotePDF;
        /* set the quote id on the page definition */
        Blob  pdfBlob;
        /* generate the pdf blob */
        pdfBlob =Blob.valueof('UNIT.TEST');
        pdfBlob = pdfPage.getContent();
        System.Debug('Checking ID 1 : ' + q.id);
        /* create the attachment against the quote */
        Attachment a = new Attachment(parentId = q.id, name=q.Quote_Number__c+'.pdf', body = pdfBlob,contentType = 'application/pdf',isPrivate=false);      
        /* insert the attachment */
        System.Debug('Checking ID 2 : ' + q.id);
        insert a;
   return controller.view();      
Please let me know if anything is required and please do suggest as we are using getContent() but the data is not there in it, although i am fetching the Quoteid.


1.If the VF(pdfPage) page is rendered as PDF then use getContent() method.
Else use getContentAsPDF().
2.Some times we need to pass parameters to the VF page.If we don't pass params then it will fail to run.
3.Please preview your vf page from devloper console once so that you will get to know whether it requires params or not.If it didn't have any body jus enter sample text and run the class.

Let us know if you have any issues.
Mark it as best answer if it works.
Soumya Srivastava 1Soumya Srivastava 1

I am using getContent() and renderAs=pdf on the VF page still i am getting a damaged PDF file. I have also previewed with a id and the n i was able to see it as required with quoteid as a pramaeter.

Soumya Srivastava 1Soumya Srivastava 1

It is working in the Salesforce Classic and creating the PDF but when we use lightening as we are redirecting from standard controller to a VF page , currently i need to change redirect URL I think according to Lightening, if you can suggest how we can redirect using Lightening to other page let me know.

Soumya Srivastava 1Soumya Srivastava 1

Currently the page refrence which i am giving is this from Opportunity Page when we click Add Quote button.

        PageReference pageRef = new PageReference('/apex/TestPageQ?id='+q.Id);
        return pageRef;
    /* The standard controller object which will be used later for navigation and to invoke
       it's save action method to create the new Quote. */
 Please help as I am currently redirecting in Lightning which is not working.

Jansi AJansi A
Hi Swmya,

I have tried to create PDF from VF on click of a Button on a record detail Page. I have enabled the VF for Mobile and SF1 as well. The PDF was creating and storing as an attachment in both Classic, Lightning and SF1 as well.

I have deleted the button and created a new one now,. After the PDF was generating correctly in Classic, but in lightning showing empty PDF. Plaes help me if you have any solution for this. It will really really saves our time.

Jansi AJansi A
The above is working fine through SF1. But the problen is only in Lightning.