doc-docx-converter
v1.0.0
Published
WASM-powered DOC/DOCX converter for Node.js.
Maintainers
Readme
doc-docx-converter
WASM-powered DOC/DOCX conversion for Node.js.
doc-docx-converter converts older Microsoft Word documents (.doc) and modern Word documents (.docx) into common output formats such as PDF, DOCX, ODT, RTF, TXT, HTML, PNG, JPG, and SVG.
It uses LibreOffice compiled to WebAssembly, so your application does not need a native LibreOffice installation.
Features
- Converts
.docand.docxinputs. - Outputs
pdf,docx,doc,odt,rtf,txt,html,png,jpg, andsvg. - Uses a reusable worker-backed LibreOffice WASM converter for server workloads.
- Provides a one-shot helper for simple scripts.
- Includes TypeScript types.
- Normalizes and validates formats before conversion.
- Supports optional PDF/image settings.
Requirements
- Node.js 18 or newer.
- Enough disk space for the LibreOffice WASM assets.
- Enough memory for LibreOffice WASM initialization and document processing.
No native soffice or LibreOffice installation is required.
Installation
npm install doc-docx-converterQuick Start
import { convertDocument } from "doc-docx-converter";
import fs from "node:fs";
const input = fs.readFileSync("contract.doc");
const result = await convertDocument(input, {
inputFormat: "doc",
outputFormat: "docx",
filename: "contract.doc"
});
fs.writeFileSync(result.filename, result.data);Reuse a Converter
For servers and batch jobs, reuse one converter. The first conversion initializes LibreOffice WASM, so reuse is much faster than creating a converter for every file.
import { createDocDocxConverter } from "doc-docx-converter";
import fs from "node:fs";
const converter = await createDocDocxConverter();
try {
const input = fs.readFileSync("contract.doc");
const result = await converter.convert(input, {
inputFormat: "doc",
outputFormat: "pdf",
filename: "contract.doc"
});
fs.writeFileSync(result.filename, result.data);
} finally {
await converter.destroy();
}Express Example
import express from "express";
import multer from "multer";
import { createDocDocxConverter } from "doc-docx-converter";
const app = express();
const upload = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: 20 * 1024 * 1024
}
});
const converter = await createDocDocxConverter({
maxInputBytes: 20 * 1024 * 1024
});
app.post("/convert", upload.single("file"), async (req, res, next) => {
try {
if (!req.file) {
res.status(400).json({ message: "Upload a .doc or .docx file." });
return;
}
const result = await converter.convert(req.file.buffer, {
inputFormat: req.file.originalname.endsWith(".doc") ? "doc" : "docx",
outputFormat: "pdf",
filename: req.file.originalname
});
res.setHeader("Content-Type", result.mimeType);
res.setHeader("Content-Disposition", `attachment; filename="${result.filename}"`);
res.send(Buffer.from(result.data));
} catch (error) {
next(error);
}
});
app.listen(3000);API
convertDocument(input, options, converterOptions?)
Creates a converter, converts one document, destroys the converter, and returns the result.
Best for scripts, CLIs, and low-volume jobs.
const result = await convertDocument(inputBuffer, {
inputFormat: "doc",
outputFormat: "pdf",
filename: "document.doc"
});createDocDocxConverter(options?)
Creates a reusable converter.
const converter = await createDocDocxConverter();
const result = await converter.convert(inputBuffer, options);
await converter.destroy();new DocDocxConverter(options?)
Constructs a converter immediately and starts initialization in the background.
const converter = new DocDocxConverter();
await converter.waitUntilReady();converter.convert(input, options)
Converts a .doc or .docx document.
const result = await converter.convert(input, {
inputFormat: "docx",
outputFormat: "html",
filename: "proposal.docx"
});converter.destroy()
Stops the converter worker and releases LibreOffice WASM resources.
Always call this when your script, job, or server is shutting down.
Types
type DocDocxInputFormat = "doc" | "docx";
type DocDocxOutputFormat =
| "pdf"
| "docx"
| "doc"
| "odt"
| "rtf"
| "txt"
| "html"
| "png"
| "jpg"
| "svg";
interface DocDocxConversionOptions {
outputFormat: DocDocxOutputFormat;
inputFormat?: DocDocxInputFormat;
filename?: string;
password?: string;
pdf?: {
pdfaLevel?: "PDF/A-1b" | "PDF/A-2b" | "PDF/A-3b";
quality?: number;
};
image?: {
width?: number;
height?: number;
dpi?: number;
pageIndex?: number;
pages?: number[];
};
maxInputBytes?: number;
}
interface DocDocxConversionResult {
data: Uint8Array;
mimeType: string;
filename: string;
duration: number;
inputFormat: DocDocxInputFormat;
outputFormat: DocDocxOutputFormat;
}PDF Options
const result = await convertDocument(input, {
inputFormat: "docx",
outputFormat: "pdf",
filename: "report.docx",
pdf: {
pdfaLevel: "PDF/A-2b",
quality: 90
}
});Image Output
Image outputs render document pages through the LibreOffice WASM engine.
const result = await convertDocument(input, {
inputFormat: "docx",
outputFormat: "png",
filename: "report.docx",
image: {
pageIndex: 0,
dpi: 150
}
});Error Handling
Known errors are thrown as DocDocxConverterError.
import { DocDocxConverterError } from "doc-docx-converter";
try {
await convertDocument(input, {
inputFormat: "doc",
outputFormat: "pdf"
});
} catch (error) {
if (error instanceof DocDocxConverterError) {
console.error(error.code, error.message);
} else {
console.error(error);
}
}Error codes:
INVALID_INPUTUNSUPPORTED_INPUT_FORMATUNSUPPORTED_OUTPUT_FORMATINPUT_TOO_LARGECONVERTER_NOT_READYCONVERSION_FAILED
Performance Notes
- The first conversion is slower because LibreOffice WASM must initialize.
- Reuse a converter for servers and batch processing.
- Large
.docfiles can consume significant memory. - For API servers, set upload limits and
maxInputBytes. - Run conversions outside request-critical paths if you expect high concurrency.
Security Notes
Documents can be complex untrusted binary inputs. For public upload APIs:
- Enforce file size limits.
- Restrict accepted extensions and MIME types.
- Use authentication/rate limits.
- Run conversion workers with appropriate process isolation for your deployment.
- Do not trust text or HTML extracted from documents without sanitization.
Supported Formats
Input:
docdocx
Output:
pdfdocxdocodtrtftxthtmlpngjpgsvg
Build From Source
npm install
npm run typecheck
npm run test
npm run buildLicense
MIT
