I need to view/generate PDF in Lightning component. How can I achieve that?
Do I have to user VF page with "renderAs" attribute inside Lightning Component? Or is there any other way to achieve this?
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 here
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.
Let me know if this helps!
You can embed a vfPage (renderas="pdf") in lightning component and utilize the component in required place.
<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global" >
<iframe src="{! '/apex/pdfPage'}" width="100%" height="500px;" frameBorder="0"/>
Hope it helps!
As of 2020, I'm having good results with the lwc viewer at github.com/annyhe/pdfViewer Doesn't require visualforce
You can also check the below PDF viewer which works well in all scenarios.
https://hellosnl.blogspot.com/2019/10/pdf-reader-as-salesforce-lightning-web-component.html
This is based on PDF.js library to render a PDF document inside an iframe. Code is provided in the github repo, the link of which you can get in the blog post.