What is @react-pdf/renderer?
@react-pdf/renderer is a library that allows you to create PDF documents using React components. It provides a way to build complex PDF documents with a declarative syntax, leveraging the power of React's component-based architecture.
What are @react-pdf/renderer's main functionalities?
Creating a Simple PDF Document
This code demonstrates how to create a simple PDF document with two sections using @react-pdf/renderer. The PDFViewer component is used to render the PDF in the browser.
const { PDFViewer, Document, Page, Text, View, StyleSheet } = require('@react-pdf/renderer');
const styles = StyleSheet.create({
page: { flexDirection: 'row', backgroundColor: '#E4E4E4' },
section: { margin: 10, padding: 10, flexGrow: 1 }
});
const MyDocument = () => (
<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>
);
const App = () => (
<PDFViewer>
<MyDocument />
</PDFViewer>
);
Styling PDF Components
This example shows how to apply styles to PDF components using the StyleSheet object. The styles are similar to CSS and can be applied to various elements within the PDF document.
const { Document, Page, Text, View, StyleSheet } = require('@react-pdf/renderer');
const styles = StyleSheet.create({
page: { flexDirection: 'row', backgroundColor: '#E4E4E4' },
section: { margin: 10, padding: 10, flexGrow: 1, backgroundColor: '#ffffff', border: '1px solid #000' },
text: { fontSize: 12, color: '#000' }
});
const MyStyledDocument = () => (
<Document>
<Page size="A4" style={styles.page}>
<View style={styles.section}>
<Text style={styles.text}>Styled Section #1</Text>
</View>
<View style={styles.section}>
<Text style={styles.text}>Styled Section #2</Text>
</View>
</Page>
</Document>
);
Embedding Images in PDF
This code demonstrates how to embed an image in a PDF document using the Image component. The image source can be a URL or a local file path.
const { Document, Page, Image, StyleSheet } = require('@react-pdf/renderer');
const styles = StyleSheet.create({
page: { flexDirection: 'row', backgroundColor: '#E4E4E4' },
image: { margin: 10, width: 200, height: 200 }
});
const MyImageDocument = () => (
<Document>
<Page size="A4" style={styles.page}>
<Image style={styles.image} src="https://example.com/image.png" />
</Page>
</Document>
);
Other packages similar to @react-pdf/renderer
pdfkit
PDFKit is a JavaScript library for generating PDF documents, written in Node.js. It provides a lower-level API compared to @react-pdf/renderer, which allows for more fine-grained control over the PDF creation process. However, it lacks the declarative, component-based approach of React.
jspdf
jsPDF is a popular library for generating PDF documents in the browser. It offers a wide range of features for creating and manipulating PDFs, but it does not use a React-based approach. Instead, it relies on a more traditional, imperative API.
pdfmake
pdfmake is a library for creating PDF documents in the browser and Node.js. It uses a declarative approach similar to @react-pdf/renderer but does not integrate with React. It provides a robust set of features for building complex PDF documents.
React renderer for creating PDF files on the browser and server
How to install
yarn add @react-pdf/renderer
How it works
import React from 'react';
import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';
const styles = StyleSheet.create({
page: {
flexDirection: 'row',
backgroundColor: '#E4E4E4'
},
section: {
margin: 10,
padding: 10,
flexGrow: 1
}
});
const MyDocument = () => (
<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>
);
Render in DOM
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(<MyDocument />, document.getElementById('root'));
Save in a file
import React from 'react';
import ReactPDF from '@react-pdf/renderer';
ReactPDF.render(<MyDocument />, `${__dirname}/example.pdf`);
Render in mobile
Coming soon
Examples
For each example, try opening output.pdf
to see the result.
To run the examples, first clone the project and install the dependencies:
git clone https://github.com/diegomura/react-pdf.git
cd react-pdf
yarn install
Then, run yarn example -- <example-name>
yarn example -- fractals
Contributors
This project exists thanks to all the people who contribute. [Contribute].
Thank you to all our sponsors! [Become a sponsors]
Backers
Thank you to all our backers! [Become a backer]
License
MIT © Diego Muracciole