Thursday, August 4, 2022

Generate PDF in LWC using third party JavaScript libraries

We can use third-party JavaScript libraries with Lightning web components. For example, we can use a library with interactive charts and graphs or a library that reduces code complexity.


Steps to be followed:


  1. Download the library from the third-party library's site.

  2. Upload the library to your Salesforce organization as a static resource, which is a Lightning Web Components content security policy requirement.

  3. In a JavaScript class that extends LightningElement:\n

  • Import the library by its static resource name.

import resourceName from'@salesforce/resourceUrl/resourceName';

For example, if you named the static resource myLib:

import myLib from '@salesforce/resourceUrl/myLib';


  • Import methods from the platformResourceLoader module.
    import { loadStyle, loadScript } from 'lightning/platformResourceLoader';

4.  Load the library and call its functions in a then() method.

    loadScript(this, myLib + '/myLib.js').then(() => {

    let result = myLib.myFunction(2,2);

    });

    What we are trying to achieve:- 




Steps taken to achieve this functionality are mentioned below : 


Step 1:-  Save the JS library as static resource in your org as shown below : 

       


               and That's it....


  

No comments:

Post a Comment