Blog Post

How to Build a Next.js Image Viewer with PSPDFKit

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

In this post, we provide you with a step-by-step guide outlining how to deploy PSPDFKit’s Next.js image 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 Image Viewer?

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

PSPDFKit Next.js Image Viewer

We offer a commercial Next.js image 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 Image Viewer

To see our image viewer in action, upload a JPG, PNG, or TIFF file by selecting Choose Example > Open Document (if you don’t see this, switch to the Standalone option). Once your image is displayed in the viewer, you can 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 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 an Image Document

  1. Add the image you want to display to the public directory. You can use our demo image 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: '/image.jpg',
					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: '/document.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 an image viewer are present by default.

Resulting page

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!

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 image 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 integrate our JavaScript image viewer using web frameworks like Angular and React.js. 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
PRODUCTS  |  Web • Releases • Components

PSPDFKit for Web 2024.3 Features New Stamps and Signing UI, Export to Office Formats, and More

PRODUCTS  |  Web • Releases • Components

PSPDFKit for Web 2024.2 Features New Unified UI Icons, Shadow DOM, and Tab Ordering

PRODUCTS  |  Web

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