How to convert DOCX to PDF in JavaScript

Table of contents

    How to convert DOCX to PDF in JavaScript
    Summary
    • Client-side conversion — Convert Word documents to PDF directly in the browser. No server required.
    • No MS Office dependency — Nutrient Web SDK handles conversion without Microsoft Office or LibreOffice.
    • Beyond conversion — The same SDK supports text editing, annotations, and digital signatures on Office files.

    In this tutorial, you’ll learn how to convert Word documents (DOC and DOCX) to PDF using JavaScript. The conversion happens entirely in the browser — no server-side processing needed.

    Why convert Word to PDF in the browser?

    Server-side Word-to-PDF conversion typically requires MS Office or LibreOffice installed on your server. That adds deployment complexity, licensing costs, and potential security concerns.

    Nutrient Web SDK runs entirely in the browser:

    • No server dependencies — Deploy to any static host. No Office installation, no LibreOffice, no server-side processing.
    • No licensing headaches — Skip MS Office licenses for your servers.
    • Works offline — Once loaded, conversion works without network requests.
    • User privacy — Documents never leave the user’s browser.

    What you can build

    • Document portals — Let users convert and download Office files as PDFs.
    • Form workflows — Convert filled Word templates to PDF for archiving.
    • Review systems — Add annotations to Word docs. Then export as PDF.
    • E-signature flows — Load a Word contract, add signatures, export as PDF.
    Explore Demo

    Prerequisites

    Adding Nutrient to your project

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

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

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

    Your assets directory should contain:

    • nutrient-viewer.js
    • nutrient-viewer-lib/ directory

    Make sure your server has the Content-Type: application/wasm MIME type set. See the troubleshooting guide if you encounter issues.

    Converting Word to PDF

    1. Add a Word document to your project directory. You can use our demo document.

    2. Create your HTML file:

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Word to PDF Converter</title>
      </head>
      <body>
      <div id="nutrient" style="width: 100%; height: 100vh;"></div>
      <script type="module" src="index.js"></script>
      </body>
      </html>
    3. Create index.js:

      import "./assets/nutrient-viewer.js";
      const baseUrl = `${window.location.protocol}//${window.location.host}/assets/`;
      NutrientViewer.load({
      baseUrl,
      container: "#nutrient",
      document: "document.docx",
      })
      .then((instance) => {
      // Convert to PDF.
      return instance.exportPDF();
      })
      .then((buffer) => {
      // Download the PDF.
      const blob = new Blob([buffer], { type: "application/pdf" });
      const url = URL.createObjectURL(blob);
      const a = document.createElement("a");
      a.href = url;
      a.download = "output.pdf";
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      URL.revokeObjectURL(url);
      })
      .catch((error) => {
      console.error("Error:", error.message);
      });
    4. Serve your project:

      Terminal window
      npx serve -l 8080 .
    5. Open http://localhost:8080. The PDF downloads automatically.

    Exporting as PDF/A

    For archival purposes, export as PDF/A:

    instance.exportPDF({
    outputFormat: {
    conformance: NutrientViewer.Conformance.PDFA_4F,
    },
    });

    See the PDF/A conversion guide for more options.

    Beyond conversion

    Once you’ve loaded a Word document, you can do more than convert it:

    • Text editing — Edit text directly in the Word document.
    • Page manipulation — Add, remove, or rearrange pages.
    • Annotations — Add highlights, comments, or stamps.
    • Digital signatures — Draw, type, or upload signatures.

    A note about fonts

    When converting Word documents with custom fonts, Nutrient substitutes unavailable fonts with equivalents (e.g. Arial with Noto). This ensures consistent rendering without requiring font licenses on the client.

    Conclusion

    Converting Word to PDF in the browser removes server-side complexity. No MS Office installation, no LibreOffice, no server to maintain. Nutrient Web SDK handles the conversion client-side, and the same SDK supports editing, annotations, and signatures if you need them later.

    To get started:

    FAQ

    Does this require Microsoft Office on my server?

    No. Nutrient Web SDK converts Word documents entirely in the browser. No server-side Office installation needed.

    What Word formats are supported?

    Both DOC and DOCX formats are supported.

    Can I customize the output PDF?

    Yes. You can specify PDF/A conformance levels for archival, control output quality, and more. See the configuration options.

    What happens with custom fonts?

    Nutrient substitutes unavailable fonts with equivalents to ensure consistent rendering without requiring font licenses.

    Can I edit the Word document before converting?

    Yes. Nutrient Web SDK supports text editing, annotations, and digital signatures on Word documents before export.

    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?