I'd like to be able to export React Page to PDF file(s). So far, I've tried jsPDF and html2canvas to capture the page as an image and save it as a pdf. Sample code:
const input = document.getElementById('exportToPDF')
window.scrollTo(0,0)
html2canvas(input)
.then((canvas) => {
document.body.appendChild(canvas)
const imgData = canvas.toDataURL('image/png')
const pdf = new jsPDF()
pdf.addImage(imgData, 'PNG', 0, 0)
pdf.save("test.pdf")
});
and 'exportToPDF' example:
<div id="exportToPDF">...</div>
I ran into problems with the canvas got cut off when the page content is too large/long. How can I get it to break into multiple pages when needed? It appears as it's limited to one page only.
What I have tried: set window width and height to html2canvas but it didn't help.
Update: I'm open to try other ways to export React page to PDF file(s) and not having to use html2canvas that are free.
html2canvas
makes an image - you almost certainly don't want an image in a PDF if it has text in it – Andy Ray