Skip to content

Instantly share code, notes, and snippets.

@ojknation
Created December 23, 2022 13:32
Show Gist options
  • Save ojknation/36201fca01a10dbf7f84e9865e43dc15 to your computer and use it in GitHub Desktop.
Save ojknation/36201fca01a10dbf7f84e9865e43dc15 to your computer and use it in GitHub Desktop.
import html2canvas from "html2canvas";
import { jsPDF } from "jspdf";
const PDFDownload = () => {
const ref = React.createRef();
const handleDownloadPdf = async () => {
const element = ref.current;
const canvas = await html2canvas(element, {
allowTaint: false,
useCORS: true,
});
const data = canvas.toDataURL("image/png");
// document.body.appendChild(canvas);
const pdf = new jsPDF();
const imgProperties = pdf.getImageProperties(data);
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (imgProperties.height * pdfWidth) / imgProperties.width;
pdf.addImage(data, "PNG", 0, 0, pdfWidth, pdfHeight);
pdf.save("file_name.pdf");
var blob = pdf.output("blob", "file_name.pdf");
// use blob as you please
};
return (
// react code you want to convert to pdf
<div ref={ref}>
</div>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment