How to display a PDF in React

Today we will show you how to display a PDF in React. Here we will use the NPM package to display a PDF as an image in React application. You can also render the PDF via the iFrame element or open it in the new tab and the browser will handle it manually.

Demo Application

Output - How to display a PDF in React - Clue Mediator
Output – How to display a PDF in React – Clue Mediator

Steps to display a PDF in React

  1. Install NPM package
  2. Render sample PDF in React component
  3. Output

1. Install NPM package

In this article, we will use the react-pdf to display a PDF. Run the following command to install the package.

2. Render sample PDF in React component

Let’s use the package and create a component to display a PDF.

As you can see in the above code, we have to follow three steps to render the PDF.

  • Import from the react-pdf.
  • Use pdf.worker.js from the external CDN.
  • Render PDF via Document element and page number via Page element.

Let’s combine all things together and see how it works.

App.js

3. Output

Run the application and check the output in the browser.

I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂

Demo & Source Code

GitHub Repository
If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *