Open and display PDFs in a Salesforce Lightning web component
ComPDF Salesforce SDK enables you to open files from your local machine, Salesforce, or an external URL. First of all, ensure that you have deployed the ComPDF Salesforce SDK to your existing project.
Open a file in Salesforce in one of the following ways:
- Using the built-in user interface (UI), you can open files from your local machine, Salesforce or an external URL.
- Programmatically, you can open files from Salesforce or an external URL.
For more information on uploading documents to Salesforce, see the Salesforce documentation.
Open a file using the built-in UI
You can open files using the built-in UI from your local machine, Salesforce or an external URL.
Use ComPDF file selector
In the top-left corner, open the App Launcher and select ComPDF.

Click browse to upload local PDF files, or open a file from Salesforce.

Configure default PDF URL
In the top-right corner, click "Edit Page" in the setup menu.

Select the ComPDF Viewer section and fill in the PDF URL in the default configuration options on the right.

Click "Save" and "Activation". Exit the editing page.
As a result, ComPDF Viewer will default to opening a document with PDF URL.
Open a file programmatically
Check the
Access-Control-Allow-Originresponse header from the server that serves the file, and ensure that Salesforce is allowed to load the file.In the
./force-app/main/default/lwc/compdfViewer/compdfViewer.jsfile, add the URL of the file to be opened to the PDF URL attribute value.jspdfUrl = 'https://example.com/source.pdf'Deploy the application to the organization again.
How to obtain the URL of a file in Salesforce
- In Salesforce, click on the "Files" tab.
- Right click on the file in the list, and then click "Public Link".
- Create a public link or copy existing public link.