Blog Post

How to Build a Next.js PDF Viewer with PSPDFKit

Illustration: How to Build a Next.js PDF Viewer with PSPDFKit

In this post, we provide you with a step-by-step guide outlining how to deploy PSPDFKit’s Next.js PDF viewer.

Next.js is a popular React framework for building user interfaces. It’s maintained by Meta and a community of individual developers and companies.

What Is a Next.js PDF Viewer?

A Next.js PDF viewer lets you render and view PDF documents in a web browser without the need to download it to your hard drive or use an external application like a PDF reader.

PSPDFKit Next.js PDF Viewer

We offer a commercial Next.js PDF viewer library that can easily be integrated into your web application. It comes with 30+ features that let you view, annotate, edit, and sign documents directly in your 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

Example of Our Next.js PDF Viewer

To demo our Next.js PDF viewer, upload a PDF, JPG, PNG, or TIFF file by clicking Open Document under the Standalone option (if you don’t see this option, select Choose Example from the dropdown). Once your document is displayed in the viewer, try drawing freehand, adding a note, or applying a crop or an e-signature.

Requirements to Get Started

To get started, you’ll need:

Creating a New Next.js Project

  1. Create a new Next.js app using the Create Next App tool:

npx create-next-app@latest pspdfkit-demo
  1. Change to the created project directory:

cd pspdfkit-demo

Adding PSPDFKit to Your Project

  1. Install pspdfkit as a dependency of the project:

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

You can do this by running the following command:

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

The above code 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 PDF

  1. Add the PDF document you want to display to the public directory. You can use our demo document as an example.

  2. Add the following code to your pages/index.js file:

import React, { useEffect, useRef } from 'react';

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

	useEffect(() => {
		const container = containerRef.current;
		let PSPDFKit;

		(async function () {
			PSPDFKit = await import('pspdfkit');

			if (PSPDFKit) {
				PSPDFKit.unload(container);
			}

			await PSPDFKit.load({
				container,
				document: '/document.pdf',
				baseUrl: `${window.location.protocol}//${window.location.host}/`,
			});
		})();

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

	return <div ref={containerRef} style={{ height: '100vh' }} />;
}
  1. Now, start the app and run it in your default browser:

npm run dev

Navigate to http://localhost:3000/ in your browser. You can see that all the features you expect from a PDF viewer are present by default.

pspdfkit demo

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 Next.js guides:

Conclusion

You should now have our Next.js PDF viewer up and running in your web application. If you hit any snags, don’t hesitate to reach out to our support team for help.

You can also deploy our vanilla JavaScript PDF viewer or use one of our many web framework deployment options like React.js, Vue.js, jQuery, and Angular.

To see a list of all web frameworks, start your free trial. Or, launch our demo to see our viewer in action.

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

Related Articles

Explore more
TUTORIALS  |  Web • Python • How To • wkhtmltopdf

How to Convert HTML to PDF Using wkhtmltopdf and C#

TUTORIALS  |  Web • Node.js • Express.js • How To • PDF Viewer

How to Build an Express.js PDF Viewer

TUTORIAL  |  Web • JavaScript • How To • PDF Viewer

Using a Webcam as an Image Input Source