Skip to content

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:

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

  1. In the top-left corner, open the App Launcher and select ComPDF. ComPDF App

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

Configure default PDF URL

  1. In the top-right corner, click "Edit Page" in the setup menu. Edit Page

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

  3. 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

  1. Check the Access-Control-Allow-Origin response header from the server that serves the file, and ensure that Salesforce is allowed to load the file.

  2. In the ./force-app/main/default/lwc/compdfViewer/compdfViewer.js file, add the URL of the file to be opened to the PDF URL attribute value.

    js
    pdfUrl = 'https://example.com/source.pdf'
  3. Deploy the application to the organization again.

How to obtain the URL of a file in Salesforce

  1. In Salesforce, click on the "Files" tab.
  2. Right click on the file in the list, and then click "Public Link".
  3. Create a public link or copy existing public link.