Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Thursday, May 29
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Lightning»Generate PDF using jsPDF in Lightning Web Component

    Generate PDF using jsPDF in Lightning Web Component

    Dhanik Lal SahniBy Dhanik Lal SahniSeptember 25, 2022Updated:June 11, 20239 Comments3 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    jSPDF in Lightning Web Component
    Share
    Facebook Twitter LinkedIn Pinterest Email

    PDF generation is very important for business and in many business scenarios like invoice generation, agreement generation etc, we have to generate PDFs. We can generate PDFs using server-side (by Salesforce Apex) and client-side ( using lightning component and visual force pages). This post will explain how we can generate PDF using jSPDF in Lightning Web Component.

    There are many client-side tools available for PDF generation using Javascript like jsPDF, PDFKit, pdfmake, Puppeteer, pdf-lib, pdfme. Due to lightning locker service, most client-side libraries are blocked. We can use jsPDF library but a few functionalities like HTML to pdf generation will not work in Salesforce as dependent libraries are blocked by lightning locker.

    Let us see the steps to use jsPDF in Salesforce LWC.

    • Add jsPDF libraries in Static Resources
    • Use jsPDF in the Lightning Web component
    • Test Component

    1. Add jsPDF libraries in Static Resources

    Download jsPDF (jspdf.umd.min.js) from its Github repository. After downloading add that file into Salesforce Static resource as jsPDF. Make sure it is added as public.

    2. Use jsPDF in the Lightning Web component

    once jsPDF is added to a static resource, we can include that library in Lightning Web Component using loadscript of platformResourceLoader.

    Three steps are required to generate PDFs using jsPDF.

    • Create jsPDF instance
    • Add text, table, images, diagrams, etc.
    • Save the file as PDF

    a. Create jsPDF instance

    To use jsPDF we have to create an instance of this library using jsPDF class. It supports default constructors as well as parameter constructors.

    //Default constructor
    var doc = new jsPDF();
    //Paramter constructor for portrait pdf 
    const doc = new jsPDF('p', 'in', 'letter');
    //using constructor with attribute
    const doc =new jsPDF({   orientation: 'landscape',   unit: 'in',   format: [4, 2] }); 

    b. Add text, table, images, diagrams, etc.

    We can add text, tables, images, diagrams, and other elements to PDF documents.

    //add text to PDF
    doc.text(20, 20, 'Hello SalesforceCodex!');
    doc.text(20, 30, 'jsPDF is client-side Javascript to generate a PDF.');
    
    // Add new page
    doc.addPage();
    doc.text(20, 20, 'Visit SalesforceCodex.com');
    
    //Sets the text color setTextColor(ch1, ch2, ch3, ch4) 
    doc.setTextColor(100); 

    There are a lot of other methods available for PDF generation. You can check those in the it’s documentation.

    c. Save the file as a PDF

    Once you put all code for document generation, you can call the save method to generate and save PDF files in the local system.

    //Save File
    doc.save("a4.pdf");

    Complete Source Code

    3. Test Component

    Add the above-created component on the lightning page. It will show generate button as we are creating a PDF completely from Js code. We can use text from template control like a text box or rich text but we have to handle styles in the JS code itself. For this post I have used hardcoded text to put in PDF.

    Here are two PDFs created using the above code.

    jSPDF in Lightning Web Component

    References:

    jsPDF

    Similar Posts

    Send Email Template As PDF Attachment using Salesforce Apex

    Schedule Email alerts based on Business Hours

    generate PDF in lightning component jsPdf lighting lightning lightning component Lightning web component lwc pdf generation pdf generation in lwc pdf in lwc
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleReusable Custom Calendar LWC using FullCalendar Js Library
    Next Article Generic Component to update Custom Metadata
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni4 Mins Read

    How to Build a Generic Modal Window in Lightning Web Component

    May 26, 2025
    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    View 9 Comments

    9 Comments

    1. Surya on January 19, 2023 8:08 am

      Hi, I tried the code in my org but it is throwing the error as “Error TypeError: Cannot destructure property ‘jsPDF’ of ‘window.jspdf’ as it is undefined.”

      Reply
      • Dhanik Lal Sahni on January 22, 2023 4:28 pm

        Hello Surya,
        Please check jsPdf is loading properly or not. As per the error, the library is not loaded correctly.

        Thank You,
        Dhanik

        Reply
    2. Edward on February 20, 2024 12:28 am

      Hi,
      Dhanik
      How can i add an imagen using static source?
      and also Can you please explicain how use table with fields related in pdf?

      Thanks

      Reply
      • Dhanik Lal Sahni on February 20, 2024 8:26 am

        Hello EdWard,
        You can convert your images to base64 and add using addImage method. You can also check jsFiddle for image at https://jsfiddle.net/godcznLt/
        You can check link for table example.

        Thank You,
        Dhanik

        Reply
    3. Hussain on April 14, 2024 6:07 pm

      Hello im getting this Error: Lightning Web Security: platformResourceLoader error loading ‘/resource/1713097642000/jsPDF/jspdf.umd.min.js’.

      Reply
      • Dhanik Lal Sahni on April 15, 2024 8:48 pm

        Hello Hussain,
        Please check that you have uploaded jsPDF file in static resource or not. If uploaded, then please check you are referring correct path like in your error it showing that you have created jsPDF folder and jspdf**.js file kept inside it. Is this correct?

        Thank You,
        Dhanik

        Reply
    4. Rajish on April 14, 2024 6:53 pm

      Hello DHANIK
      i get this Error: Lightning Web Security: platformResourceLoader error loading ‘/resource/1713097642000/jsPDF/jspdf.umd.min.js’.
      (anonymous) @ aura_prod.js:102

      Reply
      • Dhanik Lal Sahni on April 15, 2024 8:48 pm

        Hello Rajish,
        Please check that you have uploaded jsPDF file in static resource or not. If uploaded, then please check you are referring correct path like in your error it showing that you have created jsPDF folder and jspdf**.js file kept inside it. Is this correct?

        Thank You,
        Dhanik

        Reply
    5. Dhanik Lal Sahni on January 19, 2025 8:23 pm

      Hello Aishwary,
      Looks like jsPDF is not initliazed. Please check that part once.

      Thank You,
      Dhanik

      Reply
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • How to Build a Generic Modal Window in Lightning Web Component
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (111) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) file upload (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (30) Lightning web component (62) lwc (51) named credential (8) news (4) optimize apex code (4) Permission set (4) pmd (3) Queueable (9) rest api (23) S3 Server (4) salesforce (141) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.