Created
October 31, 2022 12:30
-
-
Save mugishap/c1b16d73fc75d7d86ab97a7815435965 to your computer and use it in GitHub Desktop.
Simple function that transforms HTML to PDF.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import html2canvas from "html2canvas"; | |
import jsPDF from "jspdf"; | |
const exportPDF = () => { | |
const input = document.getElementById("documentID") as HTMLElement; | |
html2canvas(input, { logging: true, useCORS: true }).then((canvas) => { | |
const imgWidth = 290; | |
const imgHeight = 210; | |
const imgData = canvas.toDataURL("img/png"); transform canvas to an image | |
const pdf = new jsPDF("l", "mm", "a4"); // L is landscape, you can use whatever you want for example p to make it portrait | |
pdf.addImage(imgData, "PNG", 5, 5, imgWidth, imgHeight); | |
pdf.save(`documentName`); //The name of the document | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
π π π @mugishap