How to build a Word (DOC/DOCX) viewer in JavaScript

Table of contents

    How to build a Word (DOC/DOCX) viewer in JavaScript
    TL;DR

    Build a JavaScript Word viewer with Nutrient Web SDK: Install the SDK, copy assets to your project, and initialize the viewer with NutrientViewer.load(). View DOC/DOCX files directly in the browser with no server required.

    Want to display Word documents directly in a browser without forcing users to download files or install software? This tutorial shows you how to build a JavaScript Word viewer using the Nutrient Web SDK. The viewer opens and displays DOC or DOCX files directly in the browser using client-side processing — no server required.

    JavaScript Word viewer displaying a DOCX document in the browser

    See the demo for a live example.

    Opening and rendering Office documents in the browser

    Nutrient Web SDK brings support for Word, Excel, and PowerPoint formats to your application, without you or your users needing any MS Office software, MS Office licenses, or third-party open source software. The technology works by converting an Office document to PDF directly in the browser, and the document is then rendered in our JavaScript viewer.

    For both manual and npm installations, it’s important to note that the assets must be copied to a public folder. Make sure your server has the Content-Type: application/wasm MIME type set, as explained in our troubleshooting guide.

    To serve the files, use an npm package like serve(opens in a new tab) as a simple HTTP server.

    Unlocking more capabilities with Office-to-PDF conversion

    Office-to-PDF conversion enables additional Office document functionality:

    • Text editing — Edit text directly in the displayed Office document.
    • Page manipulation — Organize documents by adding, removing, or rearranging pages.
    • Annotations — Boost collaboration by adding text highlights, comments, or stamps.
    • Adding signatures — Draw, type, or upload a signature directly to a Word document.
    Explore Demo

    Requirements

    This tutorial requires:

    Adding Nutrient to your project

    1. Install the Nutrient Web SDK package from npm (or download it manually):
    Terminal window
    npm install @nutrient-sdk/viewer
    1. Copy the directory containing all the required library files (artifacts) to the assets folder:
    Terminal window
    cp -R ./node_modules/@nutrient-sdk/viewer/dist/ ./assets/

    Make sure your assets directory contains the nutrient-viewer.js file and a nutrient-viewer-lib directory with the library assets.

    Integrating into your project

    1. Add a DOC or DOCX document to your project’s directory. Use this demo document as an example.
    2. Add an empty <div> element with a defined width and height to where Nutrient will be mounted:
    <div id="nutrient" style="width:100%; height: 100vh;"></div>
    1. Include nutrient-viewer.js in your HTML page:
    <script src="assets/nutrient-viewer.js"></script>
    1. Initialize Nutrient Web SDK in JavaScript by calling the load() method.

    The configuration object specifies the container element, document path, and optional license key:

    <script>
    NutrientViewer.load({
    container: "#nutrient",
    document: "document.docx", // Add the path to your document here.
    licenseKey: "YOUR_LICENSE_KEY" // Remove this line for the free trial.
    })
    </script>

    This code loads document.docx into the element with the ID nutrient.

    Full index.html file:

    <!DOCTYPE html>
    <html>
    <head>
    <title>My App</title>
    <!-- Provide proper viewport information so that the layout works on mobile devices. -->
    <meta
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    </head>
    <body>
    <!-- Element where Nutrient will be mounted. -->
    <div id="nutrient" style="width:100%; height: 100vh"></div>
    <script src="assets/nutrient-viewer.js"></script>
    <script>
    NutrientViewer.load({
    container: '#nutrient',
    document: 'document.docx', // Add the path to your document here.
    })
    .then(function (instance) {
    console.log('Nutrient loaded', instance);
    })
    .catch(function (error) {
    console.error(error.message);
    });
    </script>
    </body>
    </html>

    Serving your website

    The npm serve package hosts the project locally.

    1. Install the serve package:
    Terminal window
    npm install --global serve
    1. Serve the contents of the current directory:
    Terminal window
    serve -l 8080 .
    1. Navigate to http://localhost:8080 to view the website.

    A note about fonts

    When converting an Office document with custom fonts to a PDF, Nutrient Web SDK might not have access to these fonts due to licensing constraints. Nutrient replaces unavailable fonts with equivalents — like Arial with Noto.

    Adding more capabilities

    Once deployed, customize the viewer to meet specific requirements or add more capabilities. These JavaScript guides cover common use cases:

    Conclusion

    This tutorial covered building a Word viewer using JavaScript with the Nutrient SDK and the benefits of using Nutrient Web SDK to render Office documents in the browser.

    The JavaScript Word viewer should now be running in your web application. For troubleshooting, contact the Support team.

    The JavaScript Word viewer also integrates with web frameworks like Angular, Vue.js, and React.js. Start a free trial to see all web frameworks, or launch the demo to see the viewer in action.

    FAQ

    How can I render Word documents in the browser using JavaScript?

    The Nutrient Web SDK loads and displays DOC/DOCX files directly in the browser.

    Is it necessary to install any additional software to view Word documents?

    No. No external software or MS Office licenses are required. Everything happens client-side using Nutrient.

    Can I customize the appearance of the Word viewer?

    Yes. Adjust the viewer layout and functionalities like annotations, page manipulation, and signatures.

    Does the viewer support editing Word documents?

    Yes. Nutrient allows text editing directly in the document once loaded in the viewer.

    What happens if the document uses custom fonts?

    Nutrient replaces unavailable custom fonts with similar system fonts like Noto.

    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?