Blog Post

How to Build a Nuxt.js PDF Viewer with PSPDFKit

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

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

Nuxt.js is a framework built on top of Vue.js that offers server-side rendering for Vue applications. This allows the creation of universal Vue applications, where the HTML content is generated on the server and served to the client. This affects SEO optimization positively and can speed up your page.

It also supports automatic routing with zero configuration for every page and simplifies the development of Vue.js applications.

What Is a Nuxt.js PDF Viewer?

A Nuxt.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 Nuxt.js PDF Viewer

We offer a commercial Nuxt.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 Nuxt.js PDF Viewer

To demo our Nuxt.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:

  • Node.js (this post uses version 16.13.0)
  • A package manager for installing packages — you can use npm or Yarn

Creating the Project

Now you’ll see how to integrate PSPDFKit into your Nuxt.js project. Open a terminal and navigate to the place where you want to create the project.

  1. Create a new Nuxt.js project with create-nuxt-app:

npm init nuxt-app pspdfkit-app
  1. This will ask you some questions. Choose the default options by pressing Enter. Once you answer all the questions, it’ll install the dependencies and create a new Nuxt.js project.

Nuxt.js project creation

  1. Now, change your directory to pspdfkit-app:

cd pspdfkit-app

Adding PSPDFKit

  1. Install pspdfkit as a dependency with npm:

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

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

This will copy the pspdfkit-lib directory from within node_modules/ into the static/ directory to make it available to the SDK at runtime.

Displaying the PDF

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

  2. Add a component wrapper for the PSPDFKit library and save it as components/PSPDFKitContainer.vue:

// components/PSPDFKitContainer.vue

<template>
	<div class="pdf-container"></div>
</template>

<script>
/**
 * PSPDFKit for Web example component.
 */
export default {
	name: 'PSPDFKit',
	/**
	 * The component receives `pdfFile` as a prop, which is a type of `String` and is required.
	 */
	props: {
		pdfFile: {
			type: String,
			required: true,
		},
	},
	PSPDFKit: null,
	/**
	 * Wait until the template has been rendered to load the document into the library.
	 */
	mounted() {
		this.loadPSPDFKit().then((instance) => {
			this.$emit('loaded', instance);
		});
	},
	/**
	 * Watch for `pdfFile` prop changes and trigger unloading and loading when there's a new document to load.
	 */
	watch: {
		pdfFile(val) {
			if (val) {
				this.loadPSPDFKit();
			}
		},
	},
	/**
	 * Our component has the `loadPSPDFKit` method. This unloads and cleans up the component and triggers document loading.
	 */
	methods: {
		async loadPSPDFKit() {
			import('pspdfkit')
				.then((PSPDFKit) => {
					this.PSPDFKit = PSPDFKit;

					PSPDFKit.unload('.pdf-container');

					return PSPDFKit.load({
						document: this.pdfFile,
						container: '.pdf-container',
						baseUrl: 'http://localhost:3000/',
					});
				})
				.catch((error) => {
					console.error(error);
				});
		},
	},
};
</script>

<style scoped>
.pdf-container {
	height: 100vh;
}
</style>

Here’s what’s happening in the component:

  • The template section is rendering a div with the pdf-container class. This will help you declaratively bind the rendered DOM to the underlying component instance’s data.

  • The script section is defining a Nuxt.js instance named PSPDFKit and creating methods for mounting and loading PDF files into the pdf-container.

  • The style section is defining the height of the container.

  1. Include the newly created component in pages/index.vue:

// pages/index.vue

<template>
	<div id="app">
		<label for="file-upload" class="custom-file-upload">
			Open PDF
		</label>
		<input
			id="file-upload"
			type="file"
			@change="openDocument"
			class="btn"
		/>
		<PSPDFKitContainer :pdfFile="pdfFile" @loaded="handleLoaded" />
	</div>
</template>

<script>
import PSPDFKitContainer from '../components/PSPDFKitContainer.vue';

export default {
	name: 'app',
	/**
	 * Render the `PSPDFKitContainer` component.
	 */
	components: {
		PSPDFKitContainer,
	},

	data() {
		return {
			pdfFile: this.pdfFile || '/document.pdf',
		};
	},

	/**
	 * Our component has two methods — one to check when the document is loaded, and the other to open the document.
	 */
	methods: {
		handleLoaded(instance) {
			console.log('PSPDFKit has loaded: ', instance);
			// Do something.
		},

		openDocument(event) {
			if (this.pdfFile && this.pdfFile.startsWith('blob:')) {
				window.URL.revokeObjectURL(this.pdfFile);
			}
			this.pdfFile = window.URL.createObjectURL(
				event.target.files[0],
			);
		},
	},
};
</script>

<style>
#app {
	font-family: Avenir, Helvetica, Arial, sans-serif;
	text-align: center;
	color: #2c3e50;
}

body {
	margin: 0;
}

input[type='file'] {
	display: none;
}

.custom-file-upload {
	border: 1px solid #ccc;
	border-radius: 4px;
	display: inline-block;
	padding: 6px 12px;
	cursor: pointer;
	background: #4a8fed;
	padding: 10px;
	color: #fff;
	font: inherit;
	font-size: 16px;
	font-weight: bold;
}
</style>
  • In the template section, you have a file upload input and the PSPDFKitContainer component.

Similar to the input field, for the PSPDFKitContainer component, you’re using the v-bind directive to bind the pdfFile property to the pdfFile property of the component and attaching an event listener for the loaded event:

<PSPDFKitContainer :pdfFile="pdfFile" @loaded="handleLoaded" />
  • In the script section, you can see the implementation of the handleLoaded and openDocument methods. Also, there’s a data function that returns the pdfFile property.

  • In the style section, there are styles for custom file input and general styles for the app.

  1. Start the app:

npm run dev

You can see the application running on http://localhost:3000.

If you can’t see your PDF file rendered in the browser, make sure you actually uploaded a PDF file inside the static directory.

In the demo application, you can open different PDF files by clicking the Open PDF button. You can add signatures, annotations, stamps, and more.

Information

You can find the example on GitHub.

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

Conclusion

You should now have our Nuxt.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 integrate our PDF viewer using web frameworks like Angular, jQuery, Vue.js, 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.

FAQ

How do I integrate PSPDFKit with Nuxt.js?

To integrate PSPDFKit into your Nuxt.js project, install the PSPDFKit package, copy the necessary assets to your static directory, and create a component to display the PDF.

What are the prerequisites for building a Nuxt.js PDF viewer?

You’ll need Node.js (v16.13.0 or higher) and a package manager like npm or Yarn to install the required dependencies for your Nuxt.js project.

Can I customize the PSPDFKit PDF viewer in Nuxt.js?

Yes, PSPDFKit’s PDF viewer is highly customizable. You can modify the UI, add tools like annotations, and implement additional features like signatures directly within the viewer.

How do I display a PDF file in my Nuxt.js app?

After installing PSPDFKit, place the PDF file in the static directory and use a custom component to load and display the PDF in the viewer.

What file formats are supported by PSPDFKit in a Nuxt.js project?

PSPDFKit supports viewing and interacting with PDFs, as well as image formats like JPG, PNG, and TIFF. You can also extend support to Office documents like Word and Excel files.

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