Blog Post

Open PDFs in a Next.js App with PSPDFKit

Illustration: Open PDFs in a Next.js App with PSPDFKit

Next.js is a JavaScript framework created by Vercel that lets you build server-side rendered and static web applications using React. It has many great features and advantages, which might make Next.js your first option for building your next web application.

In this blog post, we’ll show how to use our Next.js PDF library to open a PDF in your application in a matter of minutes.

Setting Up Next.js

Use create-next-app to start a new Next.js project by running the following:

npm i -g create-next-app
create-next-app

It’ll ask for the project name and create a directory with that name. 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.

Once inside that directory, you can run npm run dev to start the development server, and you can load the page on http://localhost:3000. Now, your next goal is to integrate PSPDFKit into your application.

Integrating PSPDFKit

Before you integrate PSPDFKit, you need to install the PSPDFKit package using npm:

npm install pspdfkit

To open the PDF, you also need to integrate PSPDFKit for Web (our JavaScript PDF library). For PSPDFKit for Web to work, you have to copy the directory containing all the required library files (artifacts) to the public folder. Use the following command to do this:

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

Alternatively, you can add the copy command as a postinstall hook in package.json:

{
	"scripts": {
		"dev": "next dev",
		"start": "next start",
		"build": "next build",
		"postinstall": "cp -R ./node_modules/pspdfkit/dist/pspdfkit-lib ./public"
	}
}

This means every time you install the pspdfkit package from npm, the pspdfkit-lib directory will automatically get copied from the node_modules folder to the public folder.

By default, PSPDFKit assumes that the assets folder is present in the same folder of your application module, but in your case, you kept it inside the public folder, so you’ll have to pass a baseUrl option while initializing PSPDFKit. In app/page.js, you’ll load PSPDFKit with the required options (to view all the options, visit our API documentation). Make sure you put your PDF file (in this case, document.pdf) in the public folder too. Now you’re ready to initialize PSPDFKit in app/page.js:

'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' }} />;
}

In the snippet above, PSPDFKit.load is called with a configuration object where you define:

  • baseUrl, which is where your assets are available.

  • document, which is the relative URL for your example PDF file. You can also pass the ArrayBuffer of your file.

  • container, which is where you mount PSPDFKit.

The above code uses Hooks. If you aren’t familiar with Hooks, you can read about them on the official React website.

You asynchronously import PSPDFKit inside a useEffect hook after the page has mounted. This way, you ensure your initial bundle size isn’t large, which results in a better perceived load time and the library loading only on the browser (since PSPDFKit is a client-side library).

Opening a Local PDF

PSPDFKit for Web can also open local PDF files. In such a case, the document configuration option should be an ArrayBuffer of your file.

To open a file, you need to create a file picker and, when selecting a file, convert it to ArrayBuffer using the FileReader API (see an example here).

Once you have the PDF in the ArrayBuffer format, you can call PSPDFKit.load with it.

Wrapping Up

You can now start the server by running npm run dev, and if everything works as expected, you’ll be able to see PSPDFKit running at http://localhost:3000.

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!

Conclusion

With just a few lines of code, you were able to utilize the full power of our JavaScript PDF library in a Next.js application. In addition to opening and displaying a PDF in your application, PSPDFKit for Web can help you add advanced PDF capabilities to your app, including:

At PSPDFKit, we offer a commercial, feature-rich, and completely customizable JavaScript PDF library that’s easy to integrate and comes with well-documented APIs. Try it for free, or visit our web demo to see it 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