Introduction
In this article, we will learn how to generate a PDF in ReactJS spplications. Generating a PDF of reports or invoices or any other documents in web application is the basic requirement. In this article we use the following libraries to create our pdf. Rendering React as a pdf is generally complex, but using these libraries we can generate it easily.
- html2canvas
- jsPDF
Prerequisites
- We should have basic knowledge of React.js and Web API.
- Visual Studio and Visual Studio Code IDE should be installed on your system.
- SQL Server Management Studio.
Create ReactJS Project
Let's create a React app by using this following command.
- npx create-react-app Matfrom
Open the newly created project in Visual Studio Code. and install following packages using these command.
- npm install jspdf --save
- npm install --save html2canvas
In this demo we use Material UI for design, so let's install material ui in this application.
- npm install @material-ui/core --save
- npm install --save axios
Now let’s create a component named 'MatTable.js' and import the following packages in this component
- import jsPDF from 'jspdf';
- import html2canvas from 'html2canvas';
Now add the following code in this component.
- import React, { Component } from 'react'
- import Table from '@material-ui/core/Table';
- import TableBody from '@material-ui/core/TableBody';
- import TableCell from '@material-ui/core/TableCell';
- import TableContainer from '@material-ui/core/TableContainer';
- import TableHead from '@material-ui/core/TableHead';
- import TableRow from '@material-ui/core/TableRow';
- import Paper from '@material-ui/core/Paper';
- import axios from 'axios';
- import jsPDF from 'jspdf';
- import Button from '@material-ui/core/Button';
- import './App.css';
- import html2canvas from 'html2canvas';
- export class MatTable extends Component {
- constructor(props) {
- super(props)
- this.state = {
- ProductData: []
- }
- }
- printDocument() {
- const input = document.getElementById('pdfdiv');
- html2canvas(input)
- .then((canvas) => {
- var imgWidth = 200;
- var pageHeight = 290;
- var imgHeight = canvas.height * imgWidth / canvas.width;
- var heightLeft = imgHeight;
- const imgData = canvas.toDataURL('image/png');
- const pdf = new jsPDF('p', 'mm', 'a4')
- var position = 0;
- var heightLeft = imgHeight;
- pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
- pdf.save("download.pdf");
- });
- }
- componentDidMount() {
- axios.get('http://localhost:51760/Api/Emp/employee').then(response => {
- console.log(response.data);
- this.setState({
- ProductData: response.data
- });
- });
- }
- render() {
- console.log(this.state.ProductData);
- return (
- <div>
- <TableContainer id="pdfdiv" className="txt" component={Paper}>
- <Table stickyHeader aria-label="sticky table">
- <TableHead>
- <TableRow>
- <TableCell>Id</TableCell>
- <TableCell align="right">Name</TableCell>
- <TableCell align="right">Age</TableCell>
- <TableCell align="right">Address</TableCell>
- <TableCell align="right">City</TableCell>
- <TableCell align="right">ContactNum</TableCell>
- <TableCell align="right">Salary</TableCell>
- <TableCell style={{ paddingRight: "60px" }} align="right" >Department</TableCell>
- </TableRow>
- </TableHead>
- <TableBody>
- {
- this.state.ProductData.map((p, index) => {
- return <TableRow key={index}>
- <TableCell component="th" scope="row">
- {p.Id}
- </TableCell>
- <TableCell align="right">{p.Name}</TableCell>
- <TableCell align="right">{p.Age}</TableCell>
- <TableCell align="right">{p.Address}</TableCell>
- <TableCell align="right">{p.City}</TableCell>
- <TableCell align="right">{p.ContactNum}</TableCell>
- <TableCell align="right">{p.Salary}</TableCell>
- <TableCell style={{ paddingRight: "114px" }} align="right">{p.Department}</TableCell>
- </TableRow>
- })
- }
- </TableBody>
- </Table><br></br>
- <Button onClick={this.printDocument} variant="contained" color="primary">
- Generate Pdf
- </Button>
- </TableContainer>
- </div>
- );
- }
- }
- export default MatTable










Kishore KumarPosted Apr 16, 2020, 12:33 PM
Hi I am getting error at step 1 while installing npm install jspdf --save cannot install Error: "undefined ls-remote -h -t ssh://[email protected]/eligrey/FileSaver.js.git", can you please help
Vineet ChetuPosted Apr 16, 2020, 2:22 AM
You code is not working ?
Vineet ChetuPosted Apr 16, 2020, 1:40 AM
Hi, data is not coming in pdf from your code. Please give me any solutions ? My code are as follows: import React, { Component } from 'react' import jsPDF from 'jspdf'; import html2canvas from 'html2canvas'; export class PdfTable extends Component { constructor(props) { super(props) this.state = { UserData: [ {'id':1,'name':'abc','age':19,'designation':'Software Developer','city':'Noida'}, {'id':1,'name':'abc','age':19,'designation':'Software Developer','city':'Noida'}, {'id':1,'name':'abc','age':19,'designation':'Software Developer','city':'Noida'}, ] } } printDocument = () => { const input = document.getElementById('pdfdiv'); html2canvas(input) .then((canvas) => { var imgWidth = 200; var pageHeight = 290; var imgHeight = canvas.height * imgWidth / canvas.width; var heightLeft = imgHeight; const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF('p', 'mm', 'a4') var position = 0; var heightLeft = imgHeight; pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight); pdf.save("download.pdf"); }); } render() { const { UserData } = this.state console.log(UserData); return ( <div id="pdfdiv"> <table> <tr> <th align="right">Id</th> <th align="right">Name</th> <th align="right">Age</th> <th align="right">Designation</th> <th align="right">City</th> </tr> { UserData.map((p, index) => { return <tr key={index}> <td>{p.id}</td> <td align="right">{p.name}</td> <td align="right">{p.age}</td> <td align="right">{p.designation}</td> <td align="right">{p.city}</td> </tr> }) } </table><br></br> <button onClick={this.printDocument}> Generate Pdf </button> </div> ); } } export default PdfTable
Ankur MistryPosted Dec 28, 2019, 7:19 PM
Useful stuff thank you for sharing