Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @nativedocuments/docx-wasm with all npm packages installed. Try it out:

var docxWasm = require("@nativedocuments/docx-wasm")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@nativedocuments/docx-wasm v2.2.8-1547344486

Convert Microsoft Word documents (docx or binary .doc) to PDF; and doc to docx.

docx-wasm

Description

High quality docx to PDF conversion, at last! (and also, binary .doc to docx)

docx-wasm is a World first, production ready commercial grade solution for handling Microsoft Word documents in pure javascript + wasm, proudly brought to you by Native Documents. PDF output is achieved using the Word compatible page layout we developed for our web-based document editing/viewing components (more information: https://nativedocuments.com).

In this release, we provide:

  • docx to PDF conversion (and also binary .doc to PDF)
  • binary .doc to docx conversion

(Coming soon: doc/docx to text conversion, suitable for NLP/AI applications)

Conversion is performed locally where you are running Node, so your sensitive documents remain safely under your control. All available alternatives rely on heavy platform specific code or remote conversion (or both).

Installation

npm install docx-wasm

Registration

You'll need a ND_DEV_ID, ND_DEV_SECRET pair to use this module. We have a generous free tier, you can get your keys at https://developers.nativedocuments.com/

Copy these into the docx.init call in the sample below (or alternatively, you can set these as environment vars).

Docx to PDF sample code

const fs = require('fs');
const docx = require("@nativedocuments/docx-wasm");

// init docx engine
docx.init({
    // ND_DEV_ID: "XXXXXXXXXXXXXXXXXXXXXXXXXX",    // goto https://developers.nativedocuments.com/ to get a dev-id/dev-secret
    // ND_DEV_SECRET: "YYYYYYYYYYYYYYYYYYYYYYYYYY", // you can also set the credentials in the enviroment variables
    ENVIRONMENT: "NODE", // required
    LAZY_INIT: true      // if set to false the WASM engine will be initialized right now, usefull pre-caching (like e.g. for AWS lambda)
}).catch( function(e) {
    console.error(e);
});

async function convertHelper(document, exportFct) {
    const api = await docx.engine();
    await api.load(document);
    const arrayBuffer = await api[exportFct]();
    await api.close();
    return arrayBuffer;
}

convertHelper("sample.docx", "exportPDF").then((arrayBuffer) => {
    fs.writeFileSync("sample.pdf", new Uint8Array(arrayBuffer));
}).catch((e) => {
    console.error(e);
});

convertHelper("sample.doc", "exportDOCX").then((arrayBuffer) => {
    fs.writeFileSync("out.docx", new Uint8Array(arrayBuffer));
}).catch((e) => {
    console.error(e);
});

// you can also load the document from an array
convertHelper(new Uint8Array(fs.readFileSync("sample.docx")), "exportPDF").then((arrayBuffer) => {
    fs.writeFileSync("out.pdf", new Uint8Array(arrayBuffer));
}).catch((e) => {
    console.error(e);
});

Troubleshooting

If you are having trouble with the sample code, please check the error message.

Here is what may be going wrong:

  • Network error A network connection is required to validate your ND_DEV_ID, ND_DEV_SECRET (but not to perform the actual conversion)

  • TokenValidationError mean an invalid ND_DEV_ID, ND_DEV_SECRET pair. Did you get these from https://developers.nativedocuments.com/ and copy them into the sample code?

  • OperationFailedError Mainly thrown when loading a document. Is this a Word (doc/docx) document? Please verify it opens correctly in Microsoft Word, or drag/drop it to https://canary.nativedocuments.com/ If you continue to have issues, please try a simple "Hello World" document.

  • EngineExceptionError An exception in the document engine occured. Please let us know about this!

  • EngineTerminatedError The engine has termined.

Getting Help

If you continue to have problems, please ask a question on StackOverflow, using tags #docx-wasm, #node.js, #ms-word, and #pdf as appropriate.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free