How to Display a PDF in React | PSPDFKit (2024)

How to Display a PDF in React | PSPDFKit (1)

This article was first published in December 2022 and was updated in August 2024.

In this post, you’ll learn how to display a PDF in a React app using Vite and PSPDFKit. Vite is a modern build tool that provides a faster and leaner development experience for modern web projects, and PSPDFKit is a powerful library that allows you to view, annotate, and manipulate PDFs directly in the browser.

Benefits of Using Vite with PSPDFKit

Utilizing Vite with PSPDFKit offers several advantages:

  • Faster development — Vite’s instant server start and Hot Module Replacement (HMR) provide immediate feedback, speeding up the development and testing of PSPDFKit features.

  • Optimized performance — Vite’s efficient bundling and modern JavaScript output ensure quicker load times and smoother operation for applications integrating PSPDFKit.

  • Seamless integration — With native support for TypeScript and WebAssembly, Vite simplifies the integration of PSPDFKit’s SDKs, enhancing scalability and client-side security.

PSPDFKit React.js PDF Library

We offer a commercial React.js PDF library that’s easy to integrate. It comes with 30+ features that allow your users to view, annotate, edit, and sign documents directly in the browser. Out of the box, it has a polished and flexible UI that you can extend or simplify based on your unique use case.

  • A prebuilt and polished UI
  • 15+ annotation tools
  • Support for multiple file types
  • Dedicated support from engineers

PSPDFKit has developer-friendly documentation and offers a beautiful UI for users to work with PDF files easily. Web applications such as Autodesk, Disney, UBS, Dropbox, IBM, and Lufthansa use the PSPDFKit library to manipulate PDF documents.

Requirements

  • Node.js: Make sure you have the latest stable version installed. You can get it from the official Node.js website.

  • Package Manager: You can use either npm or Yarn. This guide will include commands for both.

How to Display a PDF in React | PSPDFKit (2)

No additional setup is required to use npm, as it’s included with your Node.js installation.

Note: PSPDFKit does not collect any data during your evaluation. You can start without a trial license, but there will be a watermark on the documents. To remove this limitation, you can get a trial key.

Setting Up a New React Project with Vite

  1. To get started, create a new React project using Vite:

# Using Yarnyarn create vite pspdfkit-react-example --template react# Using npmnpm create vite@latest pspdfkit-react-example -- --template react
  1. Navigate to your project directory:

cd pspdfkit-react-example

Adding PSPDFKit to Your Project

  1. First, install PSPDFKit as a dependency:

# Using Yarnyarn add pspdfkit# Using npmnpm install pspdfkit
  1. Next, copy the PSPDFKit Web library assets to the public directory:

cp -R ./node_modules/pspdfkit/dist/pspdfkit-lib public/pspdfkit-lib

This ensures PSPDFKit can access its assets from the public/pspdfkit-lib directory.

How to Display a PDF in React | PSPDFKit (3)

Make sure your development server supports the application/wasm MIME type. This can be crucial for WebAssembly to function correctly.

Displaying a PDF in Your React App

  1. Rename your PDF document to document.pdf and place it in the public directory.

  2. Create a new component to handle PDF rendering. In the src/components/ directory, create a file named PdfViewerComponent.jsx with the following content:

import { useEffect, useRef } from 'react';export default function PdfViewerComponent(props) {const containerRef = useRef(null);useEffect(() => {const container = containerRef.current;let PSPDFKit, instance;(async function () {PSPDFKit = await import('pspdfkit');PSPDFKit.unload(container);instance = await PSPDFKit.load({container,document: props.document,baseUrl: `${window.location.protocol}//${window.location.host}/${import.meta.env.BASE_URL}`,});})();return () => PSPDFKit && PSPDFKit.unload(container);}, []);return (<divref={containerRef}style={{ width: '100%', height: '100vh' }}/>);}
  1. Integrate this component into your app by updating the src/App.jsx file:

import PdfViewerComponent from './components/PdfViewerComponent';function App() {return (<div className="App" style={{ width: '100vw' }}><div className="PDF-viewer"><PdfViewerComponent document={'document.pdf'} /></div></div>);}export default App;

Ensure your project structure looks like this:

pspdfkit-react-example/├── public/│ ├── pspdfkit-lib/│ └── document.pdf├── src/│ ├── components/│ │ └── PdfViewerComponent.jsx│ └── App.jsx├── package.json└── yarn.lock
  1. Start your development server:

# Using Yarnyarn dev# Using npmnpm run dev

Once the server is running, you can view and interact with your PDF directly in the browser.

Try PSPDFKit for yourself and explore its extensive features for your next project!

How to Display a PDF in React | PSPDFKit (4)

Displaying a PDF in React from Any Source

PSPDFKit’s web library can open PDFs from various input sources, including:

  • Remote URLs

  • Blobs

  • Array buffers

  • Local storage

  • Base64 data

Conclusion

In this post, you learned how to display a PDF in React using PSPDFKit’s Web library. You successfully set up a React project using Vite and displayed a PDF using PSPDFKit. This setup provides a fast, efficient, and serverless solution for rendering PDFs in your web application. If you encounter any issues or need further assistance, feel free to reach out to our Support team.

At PSPDFKit, we offer a commercial, feature-rich, and completely customizable web PDF library that’s easy to integrate and comes with well-documented APIs to handle advanced use cases. Try it for free, or visit our demo to see it in action.

FAQ

Here are a few frequently asked questions about displaying PDFs in React.

What is PSPDFKit for React?

PSPDFKit for React is a commercial library that allows developers to display and interact with PDF documents in React applications. It comes with a prebuilt UI and supports various features like viewing, annotating, editing, and signing documents.

How do I display a PDF in React using PSPDFKit?

To display a PDF in React using PSPDFKit, you need to:

  1. Install the PSPDFKit library via npm or yarn.

  2. Copy the necessary PSPDFKit assets to the public directory.

  3. Create a component to load and display the PDF document using PSPDFKit’s API.

What are the benefits of using PSPDFKit in a React application?

PSPDFKit offers a polished, ready-to-use UI with more than 30 features, including annotation tools and support for multiple file types. It also provides robust documentation and dedicated support, making it easy to integrate and use in web applications.

Can I customize the PSPDFKit UI in my React project?

Yes, PSPDFKit’s UI is highly customizable. You can extend or simplify the UI according to your use case, adding custom toolbars, buttons, and other elements.

What types of input sources can PSPDFKit handle in a React application?

PSPDFKit can handle various input sources for PDFs, including remote URLs, blobs, array buffers, local storage, and base64 data.

How to Display a PDF in React | PSPDFKit (2024)
Top Articles
Redeem Codes alert! | Fandom
FINTRAC fines Binance $4.4M over AML/CFT violations
Kathleen Hixson Leaked
Devon Lannigan Obituary
Bashas Elearning
Lifewitceee
T Mobile Rival Crossword Clue
Polyhaven Hdri
The Many Faces of the Craigslist Killer
Hello Alice Business Credit Card Limit Hard Pull
Edgar And Herschel Trivia Questions
Everything You Need to Know About Holly by Stephen King
Razor Edge Gotti Pitbull Price
Costco Gas Foster City
Kirksey's Mortuary - Birmingham - Alabama - Funeral Homes | Tribute Archive
‘The Boogeyman’ Review: A Minor But Effectively Nerve-Jangling Stephen King Adaptation
683 Job Calls
Mini Handy 2024: Die besten Mini Smartphones | Purdroid.de
Airtable Concatenate
Belledelphine Telegram
Star Wars Armada Wikia
12657 Uline Way Kenosha Wi
Riverstock Apartments Photos
Superhot Free Online Game Unblocked
Skepticalpickle Leak
Yu-Gi-Oh Card Database
Rainfall Map Oklahoma
Log in or sign up to view
Emily Katherine Correro
Jambus - Definition, Beispiele, Merkmale, Wirkung
RFK Jr., in Glendale, says he's under investigation for 'collecting a whale specimen'
#scandalous stars | astrognossienne
1400 Kg To Lb
Montrose Colorado Sheriff's Department
Otter Bustr
Dr Adj Redist Cadv Prin Amex Charge
Wayne State Academica Login
If You're Getting Your Nails Done, You Absolutely Need to Tip—Here's How Much
Parent Portal Pat Med
Yakini Q Sj Photos
Ucla Basketball Bruinzone
Phmc.myloancare.com
9294027542
Food and Water Safety During Power Outages and Floods
Steam Input Per Game Setting
60 Second Burger Run Unblocked
Great Clips Virginia Center Commons
sin city jili
O.c Craigslist
Tamilyogi Cc
Latest Posts
Article information

Author: Kieth Sipes

Last Updated:

Views: 5515

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kieth Sipes

Birthday: 2001-04-14

Address: Suite 492 62479 Champlin Loop, South Catrice, MS 57271

Phone: +9663362133320

Job: District Sales Analyst

Hobby: Digital arts, Dance, Ghost hunting, Worldbuilding, Kayaking, Table tennis, 3D printing

Introduction: My name is Kieth Sipes, I am a zany, rich, courageous, powerful, faithful, jolly, excited person who loves writing and wants to share my knowledge and understanding with you.