Skip to content

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 UI.

Installation

You can load Nutrient Web SDK directly from Nutrient's content delivery network (CDN). Nutrient maintains the CDN for customers and it's a good way to get started. For more control and flexibility, you can use local installation.

  1. Add the following in your index.html:

    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/[email protected]/nutrient-viewer.js"></script>
    <script type="module" src="/src/main.ts"></script>
    </body>
    </html>
  2. You're now ready to use Nutrient Web SDK and reference window.PSPDFKit in the client side code.

Rendering a PDF

This guide uses the new composition API.

  1. Load the PDF file in App.vue or any other component of your choice using NutrientViewer:

    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,
    // you can also specify a file in public directory, for example /document.pdf
    document: "https://www.nutrient.io/downloads/pspdfkit-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>
  2. Start the development server:

    Terminal window
    npm run dev
  3. You should see the PDF rendered in the Nutrient Web SDK UI.

Next steps

Further steps to set up your project.

TypeScript with 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 these steps:

  1. Add the Nutrient Web SDK dependency, if not done previously. You need the package to be installed locally in order to reference the types:

    Terminal window
    npm i @nutrient-sdk/viewer
  2. Create a module for custom typings (say global.d.ts in the src 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;
    }
    }
  3. Restart TS server or your editor if needed.

Optimize CDN installation

If you use the CDN installation approach in production, Nutrient recommends optimizations such as prefetching.

Troubleshooting