Skip to content

Instantly share code, notes, and snippets.

@mugishap
Created October 31, 2022 12:30
Show Gist options
  • Save mugishap/c1b16d73fc75d7d86ab97a7815435965 to your computer and use it in GitHub Desktop.
Save mugishap/c1b16d73fc75d7d86ab97a7815435965 to your computer and use it in GitHub Desktop.
Simple function that transforms HTML to PDF.
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
});
};
@pacifiquem
Copy link

👏 👏 👏 @mugishap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment