Created
December 23, 2022 13:32
-
-
Save ojknation/36201fca01a10dbf7f84e9865e43dc15 to your computer and use it in GitHub Desktop.
This file contains 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 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