Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@react-pdf/renderer
Advanced tools
@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.
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>
);
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 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 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
yarn add @react-pdf/renderer
import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';
// Create styles
const styles = StyleSheet.create({
page: {
flexDirection: 'row',
backgroundColor: '#E4E4E4',
},
section: {
margin: 10,
padding: 10,
flexGrow: 1,
},
});
// Create Document Component
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>
);
Web.
Render in DOMimport ReactDOM from 'react-dom';
import { PDFViewer } from '@react-pdf/renderer';
const App = () => (
<PDFViewer>
<MyDocument />
</PDFViewer>
);
ReactDOM.render(<App />, document.getElementById('root'));
Node.
Save in a fileimport ReactPDF from '@react-pdf/renderer';
ReactPDF.render(<MyDocument />, `${__dirname}/example.pdf`);
For each example, try opening output.pdf
to see the result.
Text |
Resume |
Fractals |
Knobs |
Page wrap |
This project exists thanks to all the people who contribute. [Contribute].
Thank you to all our sponsors! [Become a sponsors]
Thank you to all our backers! [Become a backer]
MIT © Diego Muracciole
FAQs
Create PDF files on the browser and server
The npm package @react-pdf/renderer receives a total of 537,761 weekly downloads. As such, @react-pdf/renderer popularity was classified as popular.
We found that @react-pdf/renderer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.