npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nativedocuments/docx-wasm

v2.2.13-1561490777

Published

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

Downloads

9,134

Readme

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).

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);
});

Deployment Client-side

docx-wasm can be deployed client-side (including on Android), reducing server workloads and opening the way to offline operation. Sample code can be found on GitHub.

Deployment on AWS lambda - Serverless

docx-wasm is ideally suited to serverless workloads. Sample code for converting to PDF can be found on GitHub. That Lambda is designed to be invoked from an AWS Step Function, or in response to an S3 "created" event, but could easily be modified to support other triggers.

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.