Build a React.js PDF Renderer Document Generator in Browser Using react-pdf Library in Javascript Full Project For Beginners

Build a React.js PDF Renderer Document Generator in Browser Using react-pdf Library in Javascript Full Project For Beginners

 

Welcome folks today in this blog post we will be building a react.js pdf renderer document generator in browser using react-pdf library in javascript. All the full source code of the application is given below.

 

 

Get Started

 

 

react-pdf official website

 

In order to get started you need to install the below library using the npm command as shown below

 

1. Install React and react-pdf

Starting with react-pdf is extremely simple.

Using Yarn

yarn add @react-pdf/renderer

Using npm

npm install @react-pdf/renderer --save

 

 

2. Create your PDF document

 

 

import React from 'react';
import { Page, Text, View, Document, 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>
);

 

READ  Build a React.js Pixel Art Text Generator in Browser Using HTML5 CSS3 and Javascript Full Project For Beginners

 

Save in a file

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

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

 

 

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

 

 

Screenshot

 

Leave a Reply