If you need to eport React.JS components to PDF, this is one way how to do it. This approach uses html2canvas to generated canvas objects, then uses pdfMake to export them to PDF. You need to add ids to components you want to export:
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 React, {useState, Component} from 'react'; | |
import html2canvas from "html2canvas"; | |
import pdfMake from "pdfmake/build/pdfmake"; | |
import Button from '@material-ui/core/Button'; | |
const PdfExport = props => { | |
const DOWNLOAD_FILE_NAME = "WebDesigner-Design.pdf" | |
const printToPdf = () => { | |
const { ids } = props; | |
function nextStep(sections = [], i = 0) { | |
if (i >= ids.length) { | |
let pdfExportSetting = { | |
content: sections | |
}; | |
pdfMake.createPdf(pdfExportSetting).download(DOWNLOAD_FILE_NAME); | |
return; | |
} | |
html2canvas(document.getElementById(ids[i])).then(canvas => { | |
let data = canvas.toDataURL(); | |
let d = { | |
image: data, | |
width: 450 | |
} | |
sections.push(d); | |
nextStep(sections, ++i); | |
}); | |
} | |
nextStep(); | |
}; | |
return ( | |
<div> | |
<Button onClick={printToPdf} style={{ backgroundColor: "green" }}> | |
Print It | |
</Button> | |
</div> | |
); | |
}; | |
export default PdfExport; |
Undoubtedly, all of ours WordPress templates for business and services are responsive. So you can make a professional website easily.
ReplyDeleteThis is my first time visit here. From the tremendous measures of comments on your articles.I deduce I am not only one having all the fulfillment legitimately here!
ReplyDeletepmp certification in malaysia
You should talk it's shocking. Your blog survey would extend your visitors. I was fulfilled to find this site.I expected to thank you for this phenomenal read!!
ReplyDeletedata scientist course in noida