How to build a JavaScript PDF viewer with Nutrient

Table of contents

    This post provides you with a step-by-step guide on how you can deploy Nutrient’s JavaScript PDF viewer.
    How to build a JavaScript PDF viewer with Nutrient
    TL;DR

    Learn how to embed Nutrient’s feature‑rich JavaScript PDF viewer into any webpage in five quick steps: Install the package, copy the viewer artifacts, add a mount element, call NutrientViewer.load(), and serve your page.

    What is a JavaScript PDF viewer?

    A JavaScript PDF viewer uses JavaScript to 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.

    Nutrient JavaScript PDF viewer

    We offer a commercial JavaScript 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 JavaScript PDF viewer

    To demo our JavaScript 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 eSignature.

    If you prefer a video tutorial, you can watch our step-by-step guide.

    Requirements to get started

    To get started, you’ll need:

    Adding Nutrient to your project

    1. Install the @nutrient-sdk/viewer package via npm:

      Terminal window
      npm i @nutrient-sdk/viewer
    2. Copy the required viewer artifacts to your assets directory:

      Terminal window
      cp -R ./node_modules/@nutrient-sdk/viewer/dist/ ./assets/

    Make sure the assets directory contains:

    • nutrient-viewer.js (or an equivalent main script)
    • A nutrient-viewer-lib/ directory with supporting library files

    Integrating into your project

    1. Add the PDF document you want to display to your project’s directory. You can use our demo document as an example.

    2. Add a mounting <div> and a script reference to your HTML:

      <div id="nutrient" style="width: 100%; height: 100vh;"></div>
      <script type="module" src="index.js"></script>
    3. Import the viewer in your JavaScript entry file:

      import "./assets/nutrient-viewer.js";
    4. Initialize the viewer using NutrientViewer.load():

      const baseUrl = `${window.location.protocol}//${window.location.host}/assets/`;
      NutrientViewer.load({
      baseUrl,
      container: "#nutrient",
      document: "document.pdf",
      })
      .then((instance) => {
      console.log("Nutrient loaded", instance);
      })
      .catch((error) => {
      console.error(error.message);
      });

    Serving your website

    1. Install the serve package globally:

      Terminal window
      npm install --global serve
    2. Start a local server from the current directory:

      Terminal window
      serve -l 8080 .
    3. Open your browser and go to http://localhost:8080 to view your website.

      nutrient demo

    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 JavaScript guides:

    Conclusion

    You should now have our JavaScript 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 JavaScript PDF viewer using web frameworks like Angular, 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

    What is Nutrient?

    Nutrient is a powerful JavaScript library that allows developers to integrate PDF viewing, editing, and annotation capabilities into web applications.

    How do I install Nutrient?

    You can install Nutrient via npm using the command npm install pspdfkit.

    Can I use Nutrient with frameworks like React or Angular?

    Yes, Nutrient supports integration with various frameworks, including React, Angular, and Vue.js.

    What file formats does Nutrient support?

    Nutrient supports a variety of file formats, including PDF, JPG, PNG, and TIFF.

    Is there any support available for developers using Nutrient?

    Yes, Nutrient offers dedicated support from engineers to assist with integration and troubleshooting.

    Hulya Masharipov

    Hulya Masharipov

    Technical Writer

    Hulya is a frontend web developer and technical writer at Nutrient who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

    Explore related topics

    FREE TRIAL Ready to get started?