Add PDF functionality with Vue
Nutrient Web SDK is a JavaScript PDF library for viewing, annotating, and editing PDFs directly in the browser. Use it to add PDF capabilities to any web app.
This guide walks you through the steps to integrate Nutrient Web SDK into your project. By the end, you’ll be able to render a PDF document in the user interface (UI).
You can test the SDK capabilities in our playground.
Prefer to jump straight into code? View the example repo on GitHub.
Installation
You can load Nutrient Web SDK directly from Nutrient’s content delivery network (CDN). Nutrient maintains the CDN for customers, and it’s our recommended way to get started. For more control and flexibility, use the local installation option.
Add the following in your
index.html
file:index.html <!DOCTYPE html><html lang=""><head><meta charset="UTF-8" /><link rel="icon" href="/favicon.ico" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Vite App</title></head><body><div id="app"></div><script src="https://cdn.cloud.pspdfkit.com/pspdfkit-web@1.8.0/nutrient-viewer.js"></script><script type="module" src="/src/main.ts"></script></body></html>You’re now ready to use Nutrient Web SDK and reference
window.NutrientViewer
in the client-side code.
Add the Nutrient Web SDK (
@nutrient-sdk/viewer
) dependency:If you tried CDN installation first, make sure to remove the script tag:
index.html <!DOCTYPE html><html lang=""><head><meta charset="UTF-8" /><link rel="icon" href="/favicon.ico" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Vite App</title></head><body><div id="app"></div><script src="https://cdn.cloud.pspdfkit.com/pspdfkit-web@1.8.0/nutrient-viewer.js"></script><script type="module" src="/src/main.ts"></script></body></html>Add a configuration in Vite to copy the SDK assets to the
public
directory. Install therollup-plugin-copy
package:Update your Vite configuration:
vite.config.ts import { fileURLToPath, URL } from "node:url";import { defineConfig } from "vite";import vue from "@vitejs/plugin-vue";import vueDevTools from "vite-plugin-vue-devtools";import copy from "rollup-plugin-copy";// https://vite.dev/config/export default defineConfig({plugins: [copy({targets: [{// Nutrient Web SDK requires its assets to be in the `public` directory so it can load them.src: "node_modules/@nutrient-sdk/viewer/dist/nutrient-viewer-lib",dest: "public/",},],hook: "buildStart",}),vue(),vueDevTools(),],resolve: {alias: {"@": fileURLToPath(new URL("./src", import.meta.url)),},},});Update
App.vue
or any other component where you’re usingNutrientViewer
:App.vue <script setup lang="ts">import { onMounted, onUnmounted, useTemplateRef } from "vue";const containerRef = useTemplateRef("container");let NutrientViewer:| typeof import("@nutrient-sdk/viewer").default| undefined;onMounted(async () => {NutrientViewer = (await import("@nutrient-sdk/viewer")).default;const container = containerRef.value;if (container && NutrientViewer) {NutrientViewer.load({container,// You can also specify a file in public directory — for example `/nutrient-web-demo.pdf`.document: "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",// `baseUrl` where the SDK should load its assets from (copied by `rollup-plugin-copy`).baseUrl: `${window.location.protocol}//${window.location.host}/${import.meta.env.PUBLIC_URL ?? "" // Usually empty for Vite, but supports custom deployments.}`,});}});onUnmounted(() => {const container = containerRef.value;if (container && NutrientViewer) {NutrientViewer.unload(container);}});</script><template><div ref="container" class="container" /></template><style scoped>/* we also need to set the container height and width */.container {width: 100vw;height: 100vh;}</style>You’re now ready to use Nutrient Web SDK locally in your Vue + Vite app.
Rendering a PDF
Load the PDF file in
App.vue
:App.vue <script setup lang="ts">import { onMounted, onUnmounted, useTemplateRef } from "vue";const containerRef = useTemplateRef("container");const { NutrientViewer } = window;onMounted(() => {const container = containerRef.value;if (container && NutrientViewer) {NutrientViewer.load({container,document: "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",});}});onUnmounted(() => {const container = containerRef.value;if (container && NutrientViewer) {NutrientViewer.unload(container);}});</script><template><div ref="container" class="container" /></template><style scoped>/* we also need to set the container height and width */.container {width: 100vw;height: 100vh;}</style>Start the development server:
You should see the PDF rendered in the Nutrient Web SDK UI.
Next steps
This section outlines additional steps for setting up your project.
TypeScript with the CDN installation
Nutrient Web SDK comes with built-in support for TypeScript. This should work out of the box for local installation. For the CDN installation, follow the steps below.
Add the Nutrient Web SDK dependency, if you haven’t already. You need the package to be installed locally to reference the types:
Create a module for custom typings (say
global.d.ts
in thesrc
directory) to reference the built-in typings for the SDK:src/global.d.ts import NutrientViewer from "@nutrient-sdk/viewer";declare global {interface Window {// Nutrient Web SDK will be available on `window.NutrientViewer` once loaded.NutrientViewer?: typeof NutrientViewer;}}Restart the TypeScript server or your editor if needed.
Optimizing the CDN installation
If you use the CDN installation approach in production, Nutrient recommends optimizations such as prefetching(opens in a new tab).
Troubleshooting
If you encounter issues, refer to the common issues guide.
Note for developers using AI coding assistants: To get accurate troubleshooting help, copy the content from the troubleshooting guide and include it in your prompt, along with your specific error.
You can test the SDK capabilities in our playground.
Prefer to jump straight into code? View the example repo on GitHub.