DEV Community

Cover image for React & PDF Rendering
Damian Piwowarczyk
Damian Piwowarczyk

Posted on • Updated on

React & PDF Rendering

Portable Document Format (PDF) - developed 30 years ago still exists and is one of the most widely-used documents formats. There are many reasons why people still prefer to use them such as the widely supported document format which works is compatible with many devices and apps, and the content always remains the same format.

What is React-PDF ?

React-pdf lets us render documents on server and web.
It exports a set of React primitives that can be used to render things into documents easily and we can use CSS properties for styling and flexbox for layout. A list of supported primitives can be found here It supports rendering text, images, SVGs and many more.

What we going to build ?

Today we will be looking at how we can create and style PDF with react-pdf renderer. React-pdf package lets us create awesome looking PDFs using React. Its simple to use and the documentation is developer-friendly. We will create a simple application that dynamically updates our PDF-styled template which we render in DOM.

This example will show how you can render the document in DOM and how directly save the document into the file without the need of displaying it.


Demo


1. Setup

npx create-react-app app && cd app && yarn add @react-pdf/renderer
Enter fullscreen mode Exit fullscreen mode

As in the time of writing tutorial react-pdf render need some extra dependencies and craco configuration.

yarn add process browserify-zlib stream-browserify util buffer assert @craco/craco
Enter fullscreen mode Exit fullscreen mode

Change the scripts section in package.json as below:

  "scripts": {
    "start": "craco start",
    "build": "craco build",
    "test": "craco test",
    "eject": "react-scripts eject"
  },
Enter fullscreen mode Exit fullscreen mode

Next, create a new file in the root of the project
craco.config.js with

const webpack = require("webpack");

module.exports = {
  webpack: {
    configure: {
      resolve: {
        fallback: {
          process: require.resolve("process/browser"),
          zlib: require.resolve("browserify-zlib"),
          stream: require.resolve("stream-browserify"),
          util: require.resolve("util"),
          buffer: require.resolve("buffer"),
          asset: require.resolve("assert"),
        },
      },
      plugins: [
        new webpack.ProvidePlugin({
          Buffer: ["buffer", "Buffer"],
          process: "process/browser",
        }),
      ],
    },
  },
};

Enter fullscreen mode Exit fullscreen mode
mkdir Components && cd Components && mkdir PDF && cd PDF && touch Preview.js && touch LeftSection.js && touch RightSection.js
Enter fullscreen mode Exit fullscreen mode
├── App.css
├── App.js
├── index.js
├── PDF
│   ├── LeftSection.js
│   ├── Preview.js
│   └── RightSection.js
└── styles
    └── index.js
Enter fullscreen mode Exit fullscreen mode

In our App.js we will create a state that updates on user input when changes are detected we will re-render our page.

import Preview from './PDF/Preview'
import React, { useState } from 'react'
function App() {
  const [profile, setProfile] = useState({
    type: 'Profile',
    name: 'John Doe',
    profession: 'Junior Developer',
    profileImageURL: 'https://i.imgur.com/f6L6Y57.png',
    display: true,
    about: 'About...',
  })

  const handleChange = (name, value) => {
    setProfile({ ...profile, [name]: value })
  }

  return (
    <div
      style={{
        width: '100%',
        height: '100vh',
        display: 'flex',
      }}
    >
      <div style={{ width: '50%' }}>
        <div>
          <label>Name</label>
          <input
            name='name'
            defaultValue={profile.name}
            onChange={(e) => {
              handleChange(e.target.name, e.target.value)
            }}
          />
        </div>
        <div>
          <label>Profession</label>
          <input
            name='profession'
            defaultValue={profile.profession}
            onChange={(e) => {
              handleChange(e.target.name, e.target.value)
            }}
          />
        </div>
        <div>
          <label>ImageURL</label>
          <input
            name='profileImageURL'
            defaultValue={profile.profileImageURL}
            onChange={(e) => {
              handleChange(e.target.name, e.target.value)
            }}
          />
        </div>
        <div>
          <label>About</label>
          <input
            name='about'
            defaultValue={profile.about}
            onChange={(e) => {
              handleChange(e.target.name, e.target.value)
            }}
          />
        </div>
      </div>
      <Preview profile={profile} />
    </div>
  )
}

export default App

Enter fullscreen mode Exit fullscreen mode

Preview.js
This will let us render a preview on half of the page and embed the Template document that we are about to create.
We also have PDFDownloadLink which can be used to download pdf without the need of rendering it in the DOM.

import React from 'react'
import { Document, Page, PDFViewer, PDFDownloadLink } from '@react-pdf/renderer'
import LeftSection from './LeftSection'
import { RightSection } from './RightSection'
import styles from '../styles'

const Preview = ({ profile }) => {
  return (
    <div style={{ flexGrow: 1 }}>
      <PDFViewer
        showToolbar={false}
        style={{
          width: '100%',
          height: '95%',
        }}
      >
        <Template profile={profile} />
      </PDFViewer>
      <PDFDownloadLink
        document={<Template profile={profile} />}
        fileName='somename.pdf'
      >
        {({ loading }) => (loading ? 'Loading document...' : 'Download now!')}
      </PDFDownloadLink>
    </div>
  )
}
// Create Document Component
const Template = ({ profile }) => {
  return (
    <Document>
      <Page size='A4' style={styles.page}>
        // We will divide our document into 2 columns
        <LeftSection profile={profile} />
        <RightSection about={profile.about} />
      </Page>
    </Document>
  )
}

export default Preview



Enter fullscreen mode Exit fullscreen mode

We will also create folder with styles where we will keep stylesSheet for react-render primitives.

mkdir styles && cd styles && mkdir index.js
Enter fullscreen mode Exit fullscreen mode

styles

import { StyleSheet } from '@react-pdf/renderer'

export default StyleSheet.create({
  page: {
    display: 'flex',
    flexDirection: 'row',
  },
  section_right: {
    margin: 10,
    padding: 10,
    paddingTop: 20,
    width: '75%',
  },
  section_left: {
    width: '25%',
    height: '100%',
    backgroundColor: '#084c41',
  },
  profile_container: {
    display: 'flex',
    flexDirection: 'column',
    alignItems: 'center',
    marginTop: '20',
    marginBottom: '20px',
    height: '150',
    fontFamily: 'Helvetica-Bold',
  },
  name_text: {
    paddingTop: '10px',
    paddingBottom: '5px',
    fontSize: '14px',
    fontWeight: '900',
    color: 'white',
  },
  profession_text: {
    color: '#d1d5db',
    fontSize: '11px',
  },
  profile_img: {
    width: '60px',
    height: '60px',
    borderRadius: '90',
  },
  profile_line: {
    marginTop: '10px',
    width: '10%',
    height: '1px',
    backgroundColor: '#FFF',
    textAlign: 'center',
  },
})
Enter fullscreen mode Exit fullscreen mode

LeftSection.js

import { View, Text, Image } from '@react-pdf/renderer'
import styles from '../styles'

export const Profile = ({ profile }) => {
  return (
    <View style={styles.profile_container}>
      <Image style={styles.profile_img} src={profile.profileImageURL} />

      <View
        style={{
          justifyContent: 'center',
        }}
      >
        <Text style={styles.name_text}>{profile.name}</Text>
      </View>
      <Text style={styles.profession_text}>{profile.profession}</Text>
      <View style={styles.profile_line} />
    </View>
  )
}

const LeftSection = ({ profile }) => {
  return (
    <View style={styles.section_left}>
      <Profile profile={profile} />
    </View>
  )
}

export default LeftSection

Enter fullscreen mode Exit fullscreen mode

RightSection.js

import styles from '../styles'
import { View, Text } from '@react-pdf/renderer'

export const RightSection = ({ about }) => {
  return (
    <View style={styles.section_right}>
      <Text>{about}</Text>
    </View>
  )
}
Enter fullscreen mode Exit fullscreen mode

Now you know it works you could create something yourself.

More functional example of a resume builder that I built is here.
Resume builder

To sum up, this is only a simple demo to demonstrate how the pdf renderer can be used with react. React pdf package very cool tool that could be used to create things like resume builders, invoicing templates or tickets or receipts, etc. These could be either generated based on the existing data or dynamically updated on user input like in the case of our simple demo.

I hope this article was helpful to some of you guys. Thanks for reading!
Github repo

Discussion (6)

Collapse
souravojha profile image
sourav-ojha

Could not resolve dependency:
npm ERR! peer react-scripts@"^4.0.0" from @craco/craco@6.4.3

Collapse
przpiw profile image
Damian Piwowarczyk Author

You need to update your react-script package. As craco@6.4.3 needs at least react-script@4.0.0 to run.
create-react-app.dev/docs/updating...
Hope that helped.

Collapse
pedrinho_force profile image
Pedro Henrique Cardoso Lima

Hi @przpiw,

Thanks for the article.I have been trying to play around with React-PDF... and I keep receiving the following error:

Module not found: Error: Can't resolve 'stream' in '/Users/pedrolima/Desktop/React_Projects/app/node_modules/@react-pdf/pdfkit/lib'

BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "stream": require.resolve("stream-browserify") }'
- install 'stream-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "stream": false }

I have looked the error online and I am not yet what I am doing wrong. I would appreciate so much your help.

My settings:
"@craco/craco": "^6.4.3",
"@react-pdf/renderer": "^2.1.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-scripts": "5.0.0"

Thread Thread
pedrinho_force profile image
Pedro Henrique Cardoso Lima

Hi @przpiw ,

When I run your project this is what i get (img below). I don't think that's expected to happen. Right?

Thread Thread
rdani95 profile image
Rausz Dániel

Craco doesn't support CRA 5 yet, but there is an open issue about it
github.com/gsoft-inc/craco/issues/378

In the meantime, there is an alpha version (v7.0.0-alpha.0). I could install it with CRA5 app, withouth ejecting.

npm install @craco/craco@v7.0.0-alpha.0

react-pdf-renderer works fine with this version.

CC @souravojha

Collapse
jeetiss profile image
Ivakhnenko Dmitry

Your article is so helpful, thanks 🙌🏻