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 theArrayBuffer
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
.
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:
-
15+ out-of-the-box annotations to mark up, draw on, and add comments to documents.
-
PDF editing to merge, split, rotate, and crop documents.
-
PDF forms to create, fill, and capture PDF form data.
-
Digital signatures to validate the authenticity and integrity of a PDF.
-
And much more!
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.
FAQ
How can I install PSPDFKit in my Next.js app?
You can install PSPDFKit usingnpm install pspdfkit
and configure the public
folder to hold the library files.
Where should I place the PDF file in my Next.js app?
Place the PDF file in thepublic
folder and reference it in the PSPDFKit.load
function using the document
option.
Can I load PDFs asynchronously in Next.js?
Yes, use dynamic imports within auseEffect
hook to load PSPDFKit asynchronously for better performance.
Does PSPDFKit support loading PDFs from local files?
Yes, PSPDFKit can load local PDFs using theArrayBuffer
format with a file picker and the FileReader
API.