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