Blog Post

How to Build a React.js File Viewer: PDF, Image, MS Office

Illustration: How to Build a React.js File Viewer: PDF, Image, MS Office

In this blog post, learn how to build a React file viewer using the PSPDFKit for Web SDK. You’ll open and view PDF, image, and Office files directly in your web browser using client-side processing (no server required).

You can check out the demo to see it in action.

Opening and Rendering Multiple File Formats in the Browser

PSPDFKit for Web brings support for PDF, image, and Office formats to your application, without you or your users needing any MS Office software, MS Office licenses, or third-party open source software. The technology works by converting an image (JPG, PNG, and TIFF) or Office document (Word, Excel, and PowerPoint) to PDF directly in the browser. The resulting PDF is then rendered in our JavaScript viewer.

Unlocking More Capabilities

By converting an image or Office document to PDF using client-side JavaScript, you have the option to support a rich array of additional document functionality, such as:

  • Text editing — Edit text directly in the displayed Office document.

  • Page manipulation — Organize documents by adding, removing, or rearranging pages.

  • Annotations — Boost collaboration by adding text highlights, comments, or stamps.

  • Adding signatures — Draw, type, or upload a signature directly to an Office document.

Explore Demo

Requirements to Get Started

To get started, you’ll need:

  • The latest version of Node.js.

  • A package manager compatible with npm. This post contains usage examples for Yarn and the npm client (installed with Node.js by default).

Creating a New React Project

  1. Create a new React app using the Create React App tool:

yarn create react-app pspdfkit-react-example
npx create-react-app pspdfkit-react-example
  1. Change to the created project directory:

cd pspdfkit-react-example

Adding PSPDFKit to Your Project

  1. Add the PSPDFKit dependency:

yarn add pspdfkit
npm install --save pspdfkit
  1. Copy the PSPDFKit for Web library assets to the public directory:

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

The code above will copy the pspdfkit-lib directory from within node_modules/ into the public/ directory to make it available to the SDK at runtime.

  1. Make sure your public directory contains a pspdfkit-lib directory with the PSPDFKit library assets.

Displaying a Document

PSPDFKit supports the following file formats:

  • PDF, PDF/A (1, 2, 3)

  • DOCX, DOC, DOTX, DOCM

  • XLSX, XLS, XLSM

  • PPTX, PPT, PPTM

  • TIFF, TIF (including multipage)

  • PNG, JPEG, JPG

  1. Add your document to the public directory. You can use our demo PowerPoint document as an example.

  2. Add a component wrapper for the PSPDFKit library and save it as components/PdfViewerComponent.js:

import { useEffect, useRef } from 'react';

export default function PdfViewerComponent(props) {
	const containerRef = useRef(null);

	useEffect(() => {
		const container = containerRef.current;
		let instance, PSPDFKit;
		(async function () {
			PSPDFKit = await import('pspdfkit');
			PSPDFKit.unload(container);

			instance = await PSPDFKit.load({
				// Container where PSPDFKit should be mounted.
				container,
				// The document to open.
				document: props.document,
				// Use the public directory URL as a base URL. PSPDFKit will download its library assets from here.
				baseUrl: `${window.location.protocol}//${window.location.host}/${process.env.PUBLIC_URL}`,
			});
		})();

		return () => PSPDFKit && PSPDFKit.unload(container);
	}, []);

	return (
		<div
			ref={containerRef}
			style={{ width: '100%', height: '100vh' }}
		/>
	);
}
  1. Include the newly created component in App.js:

// src/App.js

import PdfViewerComponent from './components/PdfViewerComponent';

function App() {
	return (
		<div className="App">
			<div className="PDF-viewer">
				<PdfViewerComponent document={'slides.pptx'} />
			</div>
		</div>
	);
}

export default App;
  1. Your project structure will now look like this:

pspdfkit-react-example
├── public
│   ├── pspdfkit-lib
│   └── slides.pptx
├── src
│   ├── components
│   |   └── PdfViewerComponent.js
|   └── App.js
├── package.json
└── yarn.lock
  1. Start the app and run it in your default browser:

yarn start
npm start
Information

Interact with the sandbox by clicking the left rectangle icon and selecting Editor > Show Default Layout. To edit, sign in with GitHub — click the rectangle icon again and choose Sign in. To preview the result, click the rectangle icon once more and choose Editor > Embed Preview. For the full example, click the Open Editor button. Enjoy experimenting with the project!

A Note about Fonts

In client-side web applications for Microsoft Office-to-PDF conversion, PSPDFKit addresses font licensing constraints through font substitutions, typically replacing unavailable fonts with their equivalents — like Arial with Noto. For precise font matching, you can provide your own fonts, embed them into source files, or designate paths to your .ttf fonts for custom solutions.

Adding Even More Capabilities

Once you’ve deployed your viewer, you can start customizing it to meet your specific requirements or easily add more capabilities. To help you get started, here are some of our most popular React guides:

Conclusion

In this blog post, you learned how to create a React file viewer using PSPDFKit for Web Standalone. It enables opening and viewing PDF, image, and Office files directly in the browser using client-side processing. No server is required.

If you’re looking for a way to render your documents in your web application, then PSPDFKit for Web Standalone is a great option. It’s a powerful and flexible library that can help you provide your users with a seamless and enjoyable experience.

To get started, you can either:

  • Start your free trial to test out the library and see how it works in your application.

  • Launch our demo to see the viewer in action.

Related Products
Share Post
Free 60-Day Trial Try PSPDFKit in your app today.
Free Trial

Related Articles

Explore more
PRODUCTS  |  Web

Now Available for Public Preview: New Document Authoring Experience Provides Glimpse into the Future of Editing

PRODUCTS  |  Web • Releases • Components

PSPDFKit for Web 2024.1 Adds LTV Support for Digital Signatures and Improves the Document Editor UI

TUTORIALS  |  Web • JavaScript • How To • html2pdf • React

How to Convert HTML to PDF in React Using html2pdf