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
Raju Chi 5Raju Chi 5 

pdfComponent

Hi Team,

I want to generate pdf page using lightning component inside vf page.
I have tried using below code but showing empty pdf file. could please help on this.

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="sendData" type="object"/>

    <lightning:recordViewForm recordId="{!v.recordId}" objectApiName="Account">
       
            
       
       
         <div class="slds-wrap slds-text-align_center">
        <lightning:button aura:id="printButton" class="topMargin noPrint" label="Print In Pdf" onclick="{!c.downloadDocument}" />
        </div>
         <div class="slds-wrap slds-text-align_center">
            RWA FORM
        </div>
        <table  class="slds-table slds-table_bordered slds-table_cell-buffer">
            <div class="slds-grid slds-gutters">
                <div class="slds-col">
                    <span>Account##</span>
                </div>
                <div class="slds-col">
                    <span> <lightning:outputField variant="label-hidden" fieldName="Name" /></span>
                </div>
                <div class="slds-col">
                    <span>Effective date</span>
                </div>
                <div class="slds-col">
                    <span><lightning:outputField variant="label-hidden" fieldName="Effective_Date__c" /></span>
                </div>
            </div>
            <br/>
        </table>
        <br/>    
    </lightning:recordViewForm>   
</aura:component>

({
    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(){
          window.print();
        });
         
    }
    
})

Page1:

<apex:page controller="DataDisplayController" showHeader="false">
    <apex:includeLightning />
    
    <!-- Page code -->
    <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:test", function() {
                $Lightning.createComponent("c:AccountViewForm",
                { 
                    sendData : saveData
                },
                "lightning",
                function(cmp) {
                    // do some stuff
                });
            });
        }
        
        loadComponents();
    </script>
    </apex:form> 
   
</apex:page>

Page2:
<apex:page controller="DataDisplayController" showHeader="false">
    <apex:includeLightning />
    
    <!-- Page code -->
    <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:test", function() {
                $Lightning.createComponent("c:AccountViewForm",
                { 
                    sendData : saveData
                },
                "lightning",
                function(cmp) {
                    // do some stuff
                });
            });
        }
        
        loadComponents();
    </script>
    </apex:form> 
   
</apex:page>
controller:
public class DataDisplayController {
    public String PDFData {get; set;} 
    
    public DataDisplayController(){
       // PDFData = '';
    }
    
    public PageReference downloadPDF(){
        System.PageReference pageRef = new System.PageReference('/apex/AccountPdfPage');
        
        //ensure pdf downloads and is assigned with defined name
        pageRef.getHeaders().put('content-disposition', 'attachment; filename=RWAForm.pdf');
        
        return pageRef;
    }
}

Regards,
Raju
Suraj Tripathi 47Suraj Tripathi 47

Hi,

There are two methods to show a PDF in Lightning Component.
First, you can create a Visualforce page and embed it in the Lightning component.

See detailed example : https://www.vermanshul.com/2017/07/lightning-generate-pdf-from-lightning.html


Second is, usage of PDF.JS library to directly use lightning component to display the PDF. See details here .

Entire source code can be found here : https://github.com/kumarrk21/PDFViewer

https://medium.com/my-journey-with-salesforce1-platform/pdf-viewer-lightning-component-f96e6a5704ab

If it helps you please Mark it as Best Answer so that other people would take reference from it.

Thank You!

Raju Chi 5Raju Chi 5
Hi Suraj,

My requirement is smilar to First Option and i have tried based on Webstite.
But when click on button it is navigating to pdf page but not displaing data . showing empty screen.

Regards,
Raj