You are on page 1of 3

generating PDF using Lightning

Components
Dear Community,
I am new to Lightning experience,i have some knowledge about lightning.i tried to
generate the pdf for Account or any standard or custom object.i wrote the some logic
the pfd is generated with empty sheet,i want all the fields of the perticular object which
is i mentioned.i think i missed some logic in my components/controllers.
can any one suggest where i did mistake and also can tell me how to generate the pdf
for perticular object either custom or standard
my code is 
Apex class:
public class DataDisplayController {
    public String PDFData{get;set;}
    
    public DataDisplayController(){
        
        PDFData = '';
  }
    public PageReference downloadPDF(){
        System.PageReference pageRef = new
System.PageReference('/apex/PDFGenerator');
        //ensure pdf downloads and is assigned with defined name
        pageRef.getHeaders().put('content-disposition', 'attachment;
filename=TestPDF.pdf');
        
        return pageRef;
  }
    }
Component:
<aura:component >
    <aura:attribute name = "sendData" type = "Account"/>
    <lightning:button label = "Download Document" onclick = "{!c.downloadDocument}"
/>
    
</aura:component>

client-side controller.js
({
 downloadDocument : function(component, event, helper){

  var sendDataProc = component.get("v.sendData");


  var dataToSend = {
     "label" : "This is test"
  }; //this is data you want to send for PDF generation

  //invoke vf page js method


  sendDataProc(dataToSend, function(){
              
  });
 }
})
Helper.js
({
    helperMethod : function(component,callbackMethod) {
        var action = component.get("c.download");
        action.setCallback(this,function(response){
            var state = response.getState();
            if(state === "SUCCESS"){
                var generatePdf = reponse.getReturnValue();
                component.set("v.sendData",generatePdf);
                console.log('pdf generated successfully');
                
      }
            else{
                console.log('unable to generate the pdf');
      }
        });
        $A.enqueueAction(action);
        
    }
})
VF Page:
<apex:page controller="DataDisplayController" showHeader="false">
    <apex:includeLightning />
    
    <apex:form>
        <apex:inputHidden id="hidData" value="{!PDFData}"/>
        <apex:actionFunction name="jsGeneratePDF" action="{!downloadPDF}"/>
        <div id = "lightning" />
        
        <script>
            function saveData(data, callback){
            var hidData = document.getElementById('{!$Component.hidData}');
            hidData.value = JSON.stringify(data);
            
            //invoke PDF Generation
            jsGeneratePDF();
     
            //invoke callback;
            if(typeof callback == 'function') callback();
    }
        
        
        function loadComponents(){
            console.log("Loading lightning component: DataProcessor");
            
            $Lightning.use("c:LightningPDFGeneratorDemoApp", function() {
                $Lightning.createComponent("c:DataProcessor",
                { 
                    sendData : saveData
                },
                "lightning",
                function(cmp) {
                    // do some stuff
                });
            });
    }
        
        loadComponents();
        </script>            
           </apex:form>
</apex:page>

<apex:page controller="DataDisplayController"  renderAs="pdf">


    {!PDFData}
</apex:page>

App:
<aura:application extends = "ltng:outApp" >
    <c:DataProcessor />
    
</aura:application>

thanks in advance

You might also like