Convert Figma logo to code with AI

diegomura logoreact-pdf

📄 Create PDF files using React

14,643
1,162
14,643
551

Top Related Projects

11,595

Client/server side PDF printing in pure JavaScript

28,977

Client-side JavaScript PDF generation for everyone.

Quick Overview

react-pdf is a powerful library for creating PDF documents using React components. It provides a declarative way to define PDF content, allowing developers to leverage their React knowledge to generate complex PDF layouts and designs programmatically.

Pros

  • Seamless integration with React applications
  • Highly customizable and flexible for creating various PDF layouts
  • Supports both client-side and server-side rendering
  • Active development and community support

Cons

  • Learning curve for developers new to PDF generation concepts
  • Limited support for complex interactive PDF features
  • Performance can be slower for large or complex documents
  • Some advanced PDF features may require additional libraries or workarounds

Code Examples

Creating a simple PDF document:

import React from 'react';
import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';

const styles = StyleSheet.create({
  page: { padding: 30 },
  title: { fontSize: 24, marginBottom: 10 },
  text: { fontSize: 12, marginBottom: 5 },
});

const MyDocument = () => (
  <Document>
    <Page style={styles.page}>
      <Text style={styles.title}>Hello, react-pdf!</Text>
      <Text style={styles.text}>This is a simple PDF document.</Text>
    </Page>
  </Document>
);

Adding images to a PDF:

import React from 'react';
import { Document, Page, Image, StyleSheet } from '@react-pdf/renderer';

const styles = StyleSheet.create({
  page: { padding: 30 },
  image: { width: 200, height: 200 },
});

const ImageDocument = () => (
  <Document>
    <Page style={styles.page}>
      <Image style={styles.image} src="/path/to/image.jpg" />
    </Page>
  </Document>
);

Creating a table in PDF:

import React from 'react';
import { Document, Page, View, Text, StyleSheet } from '@react-pdf/renderer';

const styles = StyleSheet.create({
  table: { display: 'table', width: 'auto', borderStyle: 'solid', borderWidth: 1, borderColor: '#bfbfbf' },
  tableRow: { flexDirection: 'row' },
  tableCell: { width: '25%', borderStyle: 'solid', borderWidth: 1, borderColor: '#bfbfbf', padding: 5 },
});

const TableDocument = () => (
  <Document>
    <Page>
      <View style={styles.table}>
        <View style={styles.tableRow}>
          <View style={styles.tableCell}><Text>Row 1, Cell 1</Text></View>
          <View style={styles.tableCell}><Text>Row 1, Cell 2</Text></View>
        </View>
        <View style={styles.tableRow}>
          <View style={styles.tableCell}><Text>Row 2, Cell 1</Text></View>
          <View style={styles.tableCell}><Text>Row 2, Cell 2</Text></View>
        </View>
      </View>
    </Page>
  </Document>
);

Getting Started

  1. Install react-pdf:

    npm install @react-pdf/renderer
    
  2. Import necessary components:

    import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';
    
  3. Create a PDF document:

    const MyDocument = () => (
      <Document>
        <Page>
          <Text>Hello, react-pdf!</Text>
        </Page>
      </Document>
    );
    
  4. Render the document:

    import { PDFViewer } from '@react-pdf/renderer';
    
    const App = () => (
      <PDFViewer>
        <MyDocument />
      </PDFViewer>
    );
    

Competitor Comparisons

11,595

Client/server side PDF printing in pure JavaScript

Pros of pdfmake

  • More lightweight and focused solely on PDF generation
  • Supports both client-side and server-side PDF creation
  • Extensive documentation and examples available

Cons of pdfmake

  • Less integration with React ecosystem
  • Limited styling options compared to react-pdf's React-like approach
  • May require more manual configuration for complex layouts

Code Comparison

pdfmake:

var docDefinition = {
  content: [
    'First paragraph',
    'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
  ]
};
pdfMake.createPdf(docDefinition).download();

react-pdf:

import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';

const MyDocument = () => (
  <Document>
    <Page>
      <View>
        <Text>First paragraph</Text>
        <Text>Another paragraph, this time a little bit longer...</Text>
      </View>
    </Page>
  </Document>
);

Both libraries offer PDF generation capabilities, but react-pdf provides a more React-friendly approach with components and styling similar to React development. pdfmake, on the other hand, offers a more traditional JavaScript object-based configuration for creating PDFs.

28,977

Client-side JavaScript PDF generation for everyone.

Pros of jsPDF

  • Lightweight and standalone, with no dependencies
  • Supports a wide range of PDF features, including forms and encryption
  • Can be used in both browser and Node.js environments

Cons of jsPDF

  • Less intuitive API for complex layouts and styling
  • Limited support for advanced text formatting and positioning
  • Requires more manual work for creating complex documents

Code Comparison

react-pdf:

import { Document, Page, Text, View, StyleSheet } from '@react-pdf/renderer';

const MyDocument = () => (
  <Document>
    <Page>
      <View>
        <Text>Hello, World!</Text>
      </View>
    </Page>
  </Document>
);

jsPDF:

import jsPDF from 'jspdf';

const doc = new jsPDF();
doc.text('Hello, World!', 10, 10);
doc.save('document.pdf');

Summary

react-pdf offers a more React-friendly approach with declarative syntax and easier styling, while jsPDF provides a lightweight solution with broader PDF feature support. react-pdf is better suited for complex layouts and React-based projects, whereas jsPDF is more versatile and can be used in various JavaScript environments.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

React renderer for creating PDF files on the browser and server

Lost?

This package is used to create PDFs using React. If you wish to display existing PDFs, you may be looking for react-pdf.

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';

// 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 DOM

import React from 'react';
import 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 file

import React from 'react';
import ReactPDF from '@react-pdf/renderer';

ReactPDF.render(<MyDocument />, `${__dirname}/example.pdf`);

Contributors

This project exists thanks to all the people who contribute. Looking to contribute? Please check our [contribute] document for more details about how to setup a development environment and submitting code.

Sponsors

Thank you to all our sponsors! [Become a sponsors]

Backers

Thank you to all our backers! [Become a backer]

License

MIT © Diego Muracciole


NPM DownloadsLast 30 Days