Blog Post

How to build a Next.js PDF viewer with Nutrient

Illustration: How to build a Next.js PDF viewer with Nutrient

In this post, we provide you with a step-by-step guide outlining how to deploy Nutrient’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.

Nutrient 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 for an improved user experience
  • 15+ prebuilt annotation tools to enable document collaboration
  • Support for more file types with client-side PDF, MS Office, and image viewing
  • Dedicated support from engineers to speed up integration

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 eSignature.

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

During the setup process, Next.js will prompt you with a series of questions, allowing you to customize your project. One of the questions will ask if you want to use TypeScript. Respond with your preference (No or Yes) to set up your project accordingly.

  1. Change to the created project directory:

cd pspdfkit-demo

Adding Nutrient to your project

  1. Install pspdfkit as a dependency of the project:

npm install pspdfkit
  1. Copy the Nutrient Web SDK 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 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 PDF

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

  2. If you chose TypeScript during the setup of your project, add the following code to your app/page.tsx file:

'use client';
import { useEffect, useRef } from 'react';

const App: React.FC = () => {
	const containerRef = (useRef < HTMLDivElement) | (null > null);

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

		if (container && typeof window !== 'undefined') {
			import('pspdfkit').then((PSPDFKit) => {
				if (PSPDFKit) {
					PSPDFKit.unload(container);
				}

				PSPDFKit.load({
					container,
					document: '/pspdfkit-web-demo.pdf',
					baseUrl: `${window.location.protocol}//${window.location.host}/`,
				});
			});
		}
	}, []);

	return <div ref={containerRef} style={{ height: '100vh' }} />;
};

export default App;
  1. If you chose JavaScript during the setup of your project, add the following code to your app/page.js file:

'use client';
import { useEffect, useRef } from 'react';

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

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

		if (typeof window !== 'undefined') {
			import('pspdfkit').then((PSPDFKit) => {
				if (PSPDFKit) {
					PSPDFKit.unload(container);
				}

				PSPDFKit.load({
					container,
					document: '/pspdfkit-web-demo.pdf',
					baseUrl: `${window.location.protocol}//${window.location.host}/`,
				});
			});
		}
	}, []);

	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.

FAQ

Here are a few frequently asked questions about building a Next.js PDF viewer with Nutrient.

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.

How do I add Nutrient to my Next.js project?

Install pspdfkit as a dependency using npm install pspdfkit, and copy the Nutrient library assets to the public directory using cp -R ./node_modules/pspdfkit/dist/pspdfkit-lib public/pspdfkit-lib.

How do I display a PDF in a Next.js project using Nutrient?

Add the PDF document to the public directory, update the app/page.tsx or app/page.js file with the provided code, and start the app using npm run dev.

Author
Hulya Masharipov Technical Writer

Hulya is a frontend web developer and technical writer at PSPDFKit who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

Related products
Share post
Free trial Ready to get started?
Free trial