» npm install react-pdf
Videos
How to generate PDFs with React? PDF Report Generation
reactjs - How do I download a pdf file onClick with react-pdf? - Stack Overflow
reactjs - Generating a PDF file from React Components - Stack Overflow
How to create a PDF from React Component
» npm install @react-pdf/renderer
Hey everyone!
During my few years working as a web developer focused on React, I ran into a recurring issue: Generating multi-page PDFs or PDF reports with React. No React-based solution offers an easy and quick way to customize headers, footers, covers, or handle automatic pagination.
To solve this, I created React Smart Print (react-smart-print), a library that allows you to generate PDF reports directly from React components. The biggest advantage is that you don’t have to worry about pagination, element overflow, or layout adjustments. It works like writing a .doc document: content that fits on a page stays there, and anything that doesn’t automatically moves to the next one—without abrupt cuts, overflow issues, or misaligned elements. It also maintains headers, footers, and margins properly. Most aspects are customizable, making the development process much simpler.
The library is already available on npm under the name react-smart-print. The project is also public on GitHub, and I’d love for you to check out the code and contribute if you see potential. If this tool had existed earlier, it would have saved me weeks of development, so I hope it proves useful to you!
https://github.com/JoaquinBenegas2/react-smart-print
I have found below in their site documentation
import { PDFDownloadLink, Document, Page } from '@react-pdf/renderer'
const MyDoc = () => (
<Document>
<Page>
// My document data
</Page>
</Document>
)
const App = () => (
<div>
<PDFDownloadLink document={<MyDoc />} fileName="somename.pdf">
{({ blob, url, loading, error }) => (loading ? 'Loading document...' : 'Download now!')}
</PDFDownloadLink>
</div>
)
hi its working fine for me
import React from 'react';
import logo from './logo.svg';
import './App.css';
import { PDFDownloadLink, Page, Text, View, Document, StyleSheet } from '@react-pdf/renderer';
const styles = StyleSheet.create({
page: {
flexDirection: 'row',
backgroundColor: '#E4E4E4'
},
section: {
margin: 10,
padding: 10,
flexGrow: 1
}
});
const MyDoc = () => (
<Document>
<Page size="A4" style={styles.page}>
<View style={styles.section}>
<Text>Section #1</Text>
</View>
<View style={styles.section}>
<Text>Section #2</Text>
</View>
</Page>
</Document>
);
function App() {
return (
<div className="App">
<PDFDownloadLink document={<MyDoc />} fileName="somename.pdf">
{({ blob, url, loading, error }) => (loading ? 'Loading document...' : 'Download now!')}
</PDFDownloadLink>
</div>
);
}
export default App;
Rendering react as pdf is generally a pain, but there is a way around it using canvas.
The idea is to convert : HTML -> Canvas -> PNG (or JPEG) -> PDF
To achieve the above, you'll need :
- html2canvas &
- jsPDF
import React, {Component, PropTypes} from 'react';
// download html2canvas and jsPDF and save the files in app/ext, or somewhere else
// the built versions are directly consumable
// import {html2canvas, jsPDF} from 'app/ext';
export default class Export extends Component {
constructor(props) {
super(props);
}
printDocument() {
const input = document.getElementById('divToPrint');
html2canvas(input)
.then((canvas) => {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF();
pdf.addImage(imgData, 'JPEG', 0, 0);
// pdf.output('dataurlnewwindow');
pdf.save("download.pdf");
})
;
}
render() {
return (<div>
<div className="mb5">
<button onClick={this.printDocument}>Print</button>
</div>
<div id="divToPrint" className="mt4" {...css({
backgroundColor: '#f5f5f5',
width: '210mm',
minHeight: '297mm',
marginLeft: 'auto',
marginRight: 'auto'
})}>
<div>Note: Here the dimensions of div are same as A4</div>
<div>You Can add any component here</div>
</div>
</div>);
}
}
The snippet will not work here because the required files are not imported.
An alternate approach is being used in this answer, where the middle steps are dropped and you can simply convert from HTML to PDF. There is an option to do this in the jsPDF documentation as well, but from personal observation, I feel that better accuracy is achieved when dom is converted into png first.
Update 0: September 14, 2018
The text on the pdfs created by this approach will not be selectable. If that's a requirement, you might find this article helpful.
@react-pdf/renderer is a great resource for this.
It is a bit time consuming converting your markup and CSS to React-PDF's format, but it is easy to understand. Exporting a PDF and from it is fairly straightforward.
To allow a user to download a PDF generated by react-PDF, use their on the fly rendering, which provides a customizable download link. When clicked, the site renders and downloads the PDF for the user.
Here's their REPL which will familiarize you with the markup and styling required. They have a download link for the PDF too, but they don't show the code for that here.