This is the apex controller:

    public class pdfGenerate {

        public static final Integer limitRecords = 10;
        @AuraEnabled
        public static Blob generatePdf(){

            List<Account> records = [SELECT ID, Name, AccountNumber, Match_Billing_Address__c FROM Account LIMIT :limitRecords];
            String pdfText;
            pdfText = '<HTML><BODY>';
            pdfText = pdfText + '<H2>Account Records in PDF</H2>';
            //pdfText
            for(Integer i=0; i<limitRecords; i++){

                pdfText = pdfText +('<p>' + records.get(i).ID + '<&emsp>' + records.get(i).Name + '<&emsp>' 
                                    + records.get(i).AccountNumber + '<&emsp>'+ records.get(i).Match_Billing_Address__c + '<\br></p>');
            }
            pdfText = pdfText + '</BODY></HTML>';
            System.debug(pdfText);


            Blob pdfContent = Blob.toPdf(pdfText);
            System.debug(pdfContent);
            return pdfContent;
        }

    }

This is the client side controller:

({
    initHandler : function(component, event, helper) { 

        var action = component.get(component.get('c.generatePdf'));

        action.setCallback(this, function(response) {
            if (response.getState() == 'SUCCESS') {
                var templateData = response.getReturnValue();
                console.log(templateData);
            }
        });
        $A.enqueueAction(action);

    },

})

The component and the client-side controller part is where I'm stuck. I don't know how to go about it now. Any help would be appreciated.

  • Do you want to display this blob inside your component? – Martin Lezer Dec 7 at 8:46
  • Do you want to display the PDF in the lightning component or giving the option to save the pdf file? – Saroj Bera Dec 7 at 9:13
  • I need to display it in a lightning component. But I wouldn't mind adding the functionality of saving it. – vedant gupta Dec 7 at 10:42
up vote 0 down vote accepted

You can use PDF.JS Library for this, please follow link for details. Find Git repository for code here.

Brief:

You can download this component from this github repo. Use the ‘Deploy to Salesforce button’ in the repo to deploy to your org. Once deployed, go to /auradocs/reference.app->components->c->pdfViewer to go through the documentation on the usage of this component. There is also an example component named pdfViewerExampleComponent provided as a part of the documentation. This component takes the PDF data in base64 format and displays it in the viewer.

You need to make changes in your controller as below basically change return type to string and convert your blob to base64 encoded string.

    public class pdfGenerate {

    public static final Integer limitRecords = 10;
        @AuraEnabled
        public static String generatePdf(){

            List<Account> records = [SELECT ID, Name, AccountNumber, Match_Billing_Address__c FROM Account LIMIT :limitRecords];
            String pdfText;
            pdfText = '<HTML><BODY>';
            pdfText = pdfText + '<H2>Account Records in PDF</H2>';
            //pdfText
            for(Integer i=0; i<limitRecords; i++){

                pdfText = pdfText +('<p>' + records.get(i).ID + '<&emsp>' + records.get(i).Name + '<&emsp>' 
                                    + records.get(i).AccountNumber + '<&emsp>'+ records.get(i).Match_Billing_Address__c + '<\br></p>');
            }
            pdfText = pdfText + '</BODY></HTML>';
            System.debug(pdfText);


            Blob pdfContent = Blob.toPdf(pdfText);
            System.debug(pdfContent);
            return EncodingUtil.base64Encode(pdfContent);
    }

}
  • You could have provided some more details like the approach some code snippets etc. Link only answer will not be helpful to somebody in future if the links will die. – Saroj Bera Dec 7 at 9:16
  • @SarojBera thanks for the suggestion! I have added brief about how to use it – rahul gawale Dec 7 at 9:24
  • Actually I am using an apex controller to get records from my Account object as a blob. I want to use this in my js controller to display these records in that pdf format.. – vedant gupta Dec 7 at 10:25
  • So first you need to install package from the link into your org, then all of the required components will be created in your org, there is a component named pdfViewerExampleComponent if you open its controller, on line no 3 there is variable named pdfData, it has some hard coded value, you need to assign output from your apex controller to this variable. – rahul gawale Dec 7 at 11:57
  • 1
    Thank You. I was able to make it work. Now I'm trying to do it without using this library. I don't know if it's possible to generate a pdf in the lightning component client-side controller but I'll still try. If anyone of you knows a way, you're most welcome to share it. @rahulgawale – vedant gupta Dec 10 at 6:13

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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