[SalesForce] PDF file download in Lightning

How can we download or view the PDF file using Lightning Component from documents in salesforce1 app, is it possible to do or not, if yes please suggest how can i achieve this.

Best Answer

If you use Files or content related object there is a simple event with new lightning namespace which can be used

Here is a sample quick POC

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" >
   <aura:attribute name="contentId" type="String" default="069B00000015QMwIAM"/>
<lightning:button variant="brand" label="Preview Reciept" onclick="{!c.preview }" />

Note that the default here is the Id of the content document .

The controller code for same is below

({
   preview : function(component, event, helper) {
     $A.get('e.lightning:openFiles').fire({
        recordIds: [component.get("v.contentId")]
    });
   }
})

Here is how the component looks in the UI

enter image description here

Once you click this the below should appear

For SF1 there is a repo which covers the same using a third party JS library

https://github.com/kumarrk21/PDFViewer/tree/master/src/aura

enter image description here

Related Topic