function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SK SGSSK SGS 

pdf generation using lightning components

Hi All,
I am generating  Pdf for Account object using lightning components.in that client side controller file it will through 
component.setParams() callback failed..i passed the accountId also but getting same error.can any one help me on this issue becuase i am new to lightning exp.
my code is
apex cls
public class TextVFPDFController {
    public Account acc{get;set;}
    public TextVFPDFController(){
        Id accId = apexpages.currentpage().getparameters().get('id');
        acc = [select id,Name from Account where id=: accId];
    }
}

vf page
<apex:page controller="TextVFPDFController" renderAs="PDF">
    {!acc.Name}
</apex:page>

component

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" controller="TestAppController" >
    <aura:attribute name = "accountId" type = "Account" />
    <lightning:button variant = "brand" label = "Generate Pdf" onclick = "{!c.savePDF}" />
</aura:component>

client-side controller.js

({
    savePDF : function(component, event, helper) {
        var action = component.get("c.savePDFAccount");
        action.setCallback(this, function(response) {
            component.setParams({"recordId" : component.get("v.accountId")});
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.recordId",response.getReturnValue());
                alert('Attachment saved successfully');
                              
            }        
               else {
                        console.log("Unknown error");
                    }
                
        });
        $A.enqueueAction(action);
    }
})

server-side controller.cls

public class TestAppController {
    @auraEnabled
    public static void savePDFAccount(String recordId){
        PageReference pdfPage = new PageReference('/apex/TextVFPDF');
        pdfPage.getParameters().put('Id',recordId);
        Blob pdfContent = pdfPage.getContent();
        Attachment attach1= new Attachment();
        //attach1.ParentId = parentId;
        attach1.Name = 'Test Attachment for PDF';
        attach1.Body = pdfContent;
        attach1.contentType = 'application/pdf';
        insert attach1;
        
    }
}

and the error is

Uncaught Error in $A.getCallback() [component.setParams is not a function]
Callback failed: apex://TestAppController/ACTION$savePDFAccount

can any one help this issue
thanks in advance
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

You need to change the code from the client-side controller.
 
savePDF : function(component, event, helper) {
        var action = component.get("c.savePDFAccount");
        action.setParams({"recordId" : component.get("v.accountId")});
        action.setCallback(this, function(response) {
              var state = response.getState();
              if (state === "SUCCESS") {
                  // Your code
              }
              else {
                 console.log("Unknown error");
              }
        });
        $A.enqueueAction(action);
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
Dhanik L SahniDhanik L Sahni
Hello SK,

You can create PDF using email template and can generate PDF in lightning using Apex. Check out this link (https://salesforcecodex.com/salesforce/send-email-template-as-pdf-attachment-using-salesforce-apex/).

Thank You,
Dhanik