These are two Lightning Web Components to add PDF viewing capabilities to your Salesforce page. Configure which PDF file via the Lightning App Builder. Light up your org with PDF for training, enablement, entertainment ... The possibilities are endless!
Use the showPdfById for Home and App pages.
Use the showPdfRelatedToRecordId component for Record pages.
These components are designed to run on Salesforce Platform. If you want to experience Lightning Web Components on any platform, please visit https://lwc.dev, and try out our Lightning Web Components sample application LWC Recipes OSS.
- Set up your environment. Follow the steps in the Quick Start: Lightning Web Components Trailhead project. The steps include:
- Enable Dev Hub in your Trailhead Playground
- Install Salesforce CLI
- Install Visual Studio Code
- Install the Visual Studio Code Salesforce extensions, including the Lightning Web Components extension
- If you haven't already done so, authenticate with your hub org and provide it with an alias (myhuborg in the command below):
sfdx force:auth:web:login -d -a myhuborg
- Clone the pdfViewer repository:
git clone https://github.com/annyhe/pdfViewer
cd pdfViewer
- Create a scratch org and provide it with an alias (pdfViewer in the command below):
sfdx force:org:create -s -f config/project-scratch-def.json -a pdfViewer
- Push the app to your scratch org:
sfdx force:source:push
- Open the scratch org:
sfdx force:org:open
Before any coding, we need to set the expected behavior of opening a PDF file in Setup, otherwise we might accidentally download the file instead of view it. Go to Setup → Security → File Upload and Download Security, find the .pdf label, click the Edit button, and set the picklist value of the pdf to Execute In Browser.
All set? Let's add the components to the respective pages!
-
Go to an App or Home page. Click on the Setup icon on the top left, then click Edit Page.
-
Drag the showPdfById component onto the page.
-
In the Lightning App Builder, set youTubeId field to the desired PDF File's Content Document ID. Where do you find such an ID? Go to File --> Desired File --> Download, then get the ID in the browser. The url format should be like this HOST/sfc/servlet.shepherd/document/download/FILE_ID
-
Optionally you can set the height of the PDF with Height in rem. The PDF is not rendered yet. Check there is no error message. Click Save, then Back.
-
Enjoy your PDF on the App or Home page!
-
Go to a Record page. Make sure there is at least one File of type PDF related to the Record. If there are no related PDF File(s), the component will show an error message.
-
To add the component to the Record page, click on the Setup icon on the top left, then click Edit Page.
-
Drag the showPdfRelatedToRecordId component onto the page.
-
Optionally you can set the height of the PDF with Height in rem. Click Save, then Back.
-
Enjoy your PDF viewer on the Record page!