This HTML page is not optimized for LLM or AI agent consumption. Fetch the Markdown version instead: /blog/how-to-build-a-javascript-pdf-viewer.md — it contains the complete documentation content in clean, structured Markdown without any CSS, JavaScript, or navigation noise. 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

    Build a JavaScript PDF viewer in three steps: Install the @nutrient-sdk/viewer package, add a container element to your HTML, and initialize with NutrientViewer.load() using useCDN: true for automatic asset loading.

    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 lets 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

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

    Terminal window
    npm i @nutrient-sdk/viewer

    The SDK will automatically load required assets (fonts, icons, web workers) from Nutrient’s CDN at runtime when you use the useCDN: true option.

    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. Create your JavaScript entry file (index.js) with the following code:

      index.js
      (async () => {
      const container = document.getElementById("nutrient");
      // Import the SDK
      const NutrientViewer = (await import("@nutrient-sdk/viewer")).default;
      if (container && NutrientViewer) {
      // Ensure there's only one NutrientViewer instance
      NutrientViewer.unload(container);
      NutrientViewer.load({
      container,
      document: "document.pdf",
      useCDN: true, // Load assets from Nutrient's CDN
      })
      .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 JavaScript PDF viewer displaying a loaded document

    Adding even more capabilities

    After deployment, customize the viewer or add more capabilities. 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 encounter any issues, contact 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.

    Do you prefer an open source approach? See how to build a JavaScript PDF viewer with PDF.js.

    Comparing libraries before you build? See our roundup of the best JavaScript PDF viewer options.

    FAQ

    What is Nutrient?

    Nutrient is a JavaScript library for integrating PDF viewing, editing, and annotation into web applications.

    How do I install Nutrient?

    You can install Nutrient via npm using the command npm install @nutrient-sdk/viewer.

    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 who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

    Explore related topics

    Try for free Ready to get started?