Blog Post

Generate PDFs in Salesforce with Lightning Web Components

Illustration: Generate PDFs in Salesforce with Lightning Web Components

This tutorial will guide you through the process of creating a Lightning web component that leverages the jsPDF library to dynamically generate PDF documents directly within Salesforce. Additionally, you’ll explore the limitations of client-side PDF generation and gain insights into when to consider alternative approaches.


About PSPDFKit for Salesforce

If you work with PDF documents in Salesforce, PSPDFKit provides a client-side JavaScript library that can be integrated into a Lightning web component to enable opening, editing, and saving PDFs from a web browser. Some of our most popular features include the ability to:

  • View and edit PDF content directly in Salesforce

  • Collaborate on documents by highlighting text and adding notes or stamps

  • Organize documents by adding, deleting, rotating, or reordering pages

  • Programmatically fill PDF forms with information from Salesforce

  • Add an eSignature by drawing, typing, or uploading an image of your signature

  • Permanently redact sensitive information like email addresses, SSNs, phone numbers, and more

Step 1 — Setting Up Your Development Environment

  1. Open Visual Studio Code on your computer.

  2. Log in to your Salesforce developer account by pressing Control-Shift-P (on Windows) or Command-Shift-P (on macOS). Then, select SFDX: Authorize an Org > Project Default and press Enter. This action will redirect you to log in to your Salesforce organization.

Step 2 — Creating a New Lightning Web Component

  1. In Visual Studio Code, open the Command Palette using Control-Shift-P (on Windows) or Command-Shift-P (on macOS).

  2. Type SFDX and select SFDX: Create Lightning Web Component (not SFDX: Create Lightning Component).

  3. Enter PdfGenerator as the name of the new component.

  4. Press Enter to accept the default path, force-app/main/default/lwc.

  5. Press Enter again.

Step 3 — Designing the Component

  1. Open the pdfGenerator.html file and add the following content:

		<h1>PDF Generator</h1>
			label="Generate PDF"

Step 4 — Including a PDF Generation Library

Static resources play a crucial role in Salesforce development by allowing you to upload and reference external files, such as JavaScript libraries and stylesheets, within your Salesforce organization. Since Lightning web components operate within a secure context and have limitations on how external resources are loaded, using static resources is the recommended approach to bring in third-party libraries.

To generate a PDF, you’ll need a PDF generation library. One popular choice is jsPDF. Create a static resource in Salesforce to hold the jsPDF library.

  1. Log in to your Salesforce developer account.

  2. Click App Launcher (grid icon) and search for Static Resources.

  3. Click Static Resources to access the Static Resources page.

  4. Click New.

  5. Enter a name for the static resource. Name it something like jsPDFLibrary.

  6. Leave the Cache Control field set to Public.

  7. Choose the Upload File option and select the jspdf.umd.min.js file in the node_modules/jspdf/dist folder. This is the jsPDF library you want to upload. Double-check that the name of the static resource you uploaded is exactly jspdf.umd.min.js, as the name is case-sensitive.

  8. Click Save.

static resource

Step 5 — Implementing PDF Generation Logic

  1. Open the pdfGenerator.js file to handle PDF generation. This function will use the jsPDF library to generate the PDF document based on the content you’ve designed.

Load the static resource containing the jsPDF library using loadScript:

import { LightningElement } from 'lwc';
import { loadScript } from 'lightning/platformResourceLoader';
import JS_PDF from '@salesforce/resourceUrl/jsPDFLibrary';

export default class PdfGenerator extends LightningElement {
	jsPDFInitialized = false;

	renderedCallback() {
		if (!this.jsPDFInitialized) {
			this.jsPDFInitialized = true;
			loadScript(this, JS_PDF)
				.then(() => {
					console.log('jsPDF library loaded successfully');
				.catch((error) => {
					console.error('Error loading jsPDF library', error);

	handleGeneratePDF() {
		if (this.jsPDFInitialized) {
			// Make sure to correctly reference the loaded jsPDF library.
			const doc = new window.jspdf.jsPDF();

			// Add content to the PDF.
			doc.text('Hello PDF!', 10, 10);

			// Save the PDF.'generated_pdf.pdf');
		} else {
			console.error('jsPDF library not initialized');
  1. In the pdfGenerator.js-meta.xml file, add the following lines to the targets section:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="">

Step 6 — Pushing Changes to Your Organization

  1. Press Control-Shift-P (on Windows) or Command-Shift-P (on macOS) to open the Command Palette.

  2. Type SFDX and select SFDX: Deploy This Source to Org.

Step 7 — Verify the Component in Your Organization

  1. Open your Salesforce organization.

  2. Click App Launcher (grid icon) and search for Sales.

  3. Click the setup gear icon, and then select Edit Page.

  4. Drag the pdfGenerator component from the custom section to the page.

  5. Save the page.

  6. Click Generate PDF to create a PDF document.

adding component to lightning page

Upon successful completion, feel free to deploy your Lightning web component to your production environment and customize it to suit your Salesforce application’s needs.

Limitations of PDF Generation Using jsPDF and LWC

  1. Client-Side Processing — jsPDF generates PDFs on the client side, which could impact performance for complex or large PDFs.

  2. Styling and Layout — jsPDF is suited for basic PDFs; complex styling and layouts might require additional effort.

  3. Advanced Features — Advanced elements like complex tables, interactive forms, or intricate charts may require extra handling.


By following the steps outlined in this tutorial, you’ve learned how to integrate a PDF generation library and create PDFs on the fly in Salesforce. While jsPDF offers a powerful solution for simple PDF generation, it’s important to consider the limitations and constraints of client-side PDF generation. For more complex PDF requirements, or if you’re looking for advanced features, you might explore other options, including integrating with server-side PDF generation services or commercial PDF libraries. Always assess your project’s specific requirements and constraints before deciding on a solution.

We offer a Salesforce PDF library that can be used to add PDF viewing, annotation, and editing to your native Salesforce application. Get started with a free trial, or contact our Sales team for licensing questions.

Share Post
Free 60-Day Trial Try PSPDFKit in your app today.
Free Trial

Related Articles

Explore more
DESIGN  |  Baseline UI • Web

Part I — Design Systems: Building Blocks for Seamless User Experiences

PRODUCTS  |  Web • Releases • Components

PSPDFKit for Web 2024.3 Features New Stamps and Signing UI, Export to Office Formats, and More

PRODUCTS  |  Web • Releases • Components

PSPDFKit for Web 2024.2 Features New Unified UI Icons, Shadow DOM, and Tab Ordering