Create Custom PDF in Lightning Web Component | Salesforce ????
Kapil Batra
Salesforce MVP | SalesforceBolt.com | Bharat Dreamin' | 20x Salesforce Certified Application Architect | All Star Ranger ?? | Blogger | YouTube Creator
In this article you will learn how you can create a PDF in Lightning Web Component. I will use a PDF library to create the PDF.
Please follow steps below :?
Official Document :?https://pdf-lib.js.org/
Step 1
Save the JS library as static resource in your org as shown below :?
https://unpkg.com/[email protected]/dist/pdf-lib.js
Step 2
Import the library in your lightning web component
import pdflib from "@salesforce/resourceUrl/pdflib";
import { loadScript } from "lightning/platformResourceLoader";
Step 3
Create a rendercallback to load the Script
renderedCallback() {
loadScript(this, pdflib).then(() => {});
}
Step 4
Add a button on your Lightning Web Component
<template>
<lightning-card title="Create PDF File" icon-name="utility:user">
</lightning-card>
<div style="background-color: white; padding: 10px">
<lightning-button label="Create PDF" onclick={createPdf}>
</lightning-button>
</div>
</template>
Step 5
Add create pdf and download function in the Javascript file
import { LightningElement } from "lwc";
import pdflib from "@salesforce/resourceUrl/pdflib";
import { loadScript } from "lightning/platformResourceLoader";
export default class CreatePDF extends LightningElement {
renderedCallback() {
loadScript(this, pdflib).then(() => {});
}
async createPdf() {
const pdfDoc = await PDFLib.PDFDocument.create();
const timesRomanFont = await pdfDoc.embedFont(
PDFLib.StandardFonts.TimesRoman
);
const page = pdfDoc.addPage();
const { width, height } = page.getSize();
const fontSize = 30;
page.drawText("Learning with Salesforce Bolt !", {
x: 50,
y: height - 4 * fontSize,
size: fontSize,
font: timesRomanFont,
color: PDFLib.rgb(0, 0.53, 0.71)
});
const pdfBytes = await pdfDoc.save();
this.saveByteArray("My PDF", pdfBytes);
}
saveByteArray(pdfName, byte) {
var blob = new Blob([byte], { type: "application/pdf" });
var link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
var fileName = pdfName;
link.download = fileName;
link.click();
}
}
Output
Checkout complete video tutorial below :
6x Salesforce Certified | Accredited Professional Certified | Salesforce Technical Lead at Bluvium
1 年This is great Kapil Batra. Is there any way I can render the custom html of my LWC instead of creating the entire html in js and then rendering as PDF?
Salesforce Developer|2x Salesforce certified
1 年Hi Kapil Batra,Thank you for knowledge sharing. My question is If there are multiple lines content to display how to fit that content since if we use drawText method it's not splitting to next lines.
Salesforce Developer | Apex, Integrations, REST
1 年Hi Kapil Batra, I'm following your article(via blog) and get these errors: The resource from “https://localhost:3333/assets/project/ccfb36c02f/staticresources/pdflib” was blocked due to MIME type (“application/octet-stream”) mismatch (X-Content-Type-Options: nosniff) Loading failed for the <script> with source “https://localhost:3333/assets/project/ccfb36c02f/staticresources/pdflib” Then when clicking on Create PDF button: Uncaught (in promise) TypeError: pdflib.PDFDocument is undefined At this point not sure if I'm missing anything of if things changed. Any guidance here would be much appreciated!
CEO at The Store | Experienced Nonprofit Leader | Advancing a Compassionate Response to Poverty and Food Insecurity
2 年This is great. If I wanted to work in HTML and Apex to generate the content for the pdf, rather than Javascript, is that possible using this solution or would I need a completely different approach.
Salesforce Consultant at Infosys | Ex-Wipro
2 年if the PDF generated in apex, can we import it to js and have a downloadable link???