File converter for various types, tested on linux and windows.
- Node.js >= 18
- Linux or Windows (a LibreOffice bundle is downloaded on install). On other
platforms document conversion is unavailable, but
convertWordFileToHTMLandconvertToBase64still work.
| Accepted file formats | Output |
|---|---|
| doc | docx |
| doc | |
| doc | odt |
| docx | doc |
| docx | odt |
| docx | |
| odt | doc |
| odt | docx |
| odt | |
| docx | html |
| all | base64 |
npm i convert-multiple-files
import { convertWordFiles } from 'convert-multiple-files';
import * as path from 'path';
async test() {
// Return promise => convertWordFiles(path of the file to be converted, convertTo, outputDir)
const pathOutput = await convertWordFiles(path.resolve(__dirname, 'teste.doc'), 'pdf', path.resolve(__dirname));
console.log(pathOutput);
}
test();The returned path is verified to exist; if LibreOffice fails to produce it, the promise rejects instead of returning a path to a missing file.
An optional fourth argument controls the conversion timeout (default 60000 ms):
await convertWordFiles(input, 'pdf', outputDir, { timeout: 120000 });Conversions run against a private LibreOffice profile cloned from a shared
template kept in the system temp directory. Set CMF_LO_PROFILE_DIR to relocate
it if the temp directory is not writable.
LibreOffice startup dominates conversion time, so converting a list in one call
is far faster than calling convertWordFiles in a loop.
import { convertWordFilesBatch } from 'convert-multiple-files';
async test() {
// Return promise => convertWordFilesBatch(paths of the files, convertTo, outputDir)
const pathsOutput = await convertWordFilesBatch(['a.docx', 'b.docx', 'c.docx'], 'pdf', outputDir);
console.log(pathsOutput);
}
test();import { convertWordFileToHTML } from 'convert-multiple-files';
import * as path from 'path';
async test() {
// Return promise => convertWordFileToHTML(path of the file to be converted, outputDir, outputPrefix)
const infoOutput = await convertWordFileToHTML(path.resolve(__dirname, 'file2.docx'), path.resolve(__dirname), 'filehtml-151412');
console.log(infoOutput);
}
test();Resolves undefined (and writes no file) when the document produces no content.
import { convertToBase64 } from 'convert-multiple-files';
import * as path from 'path';
async test() {
// Return promise => convertToBase64(path of the file to be converted)
const base64 = await convertToBase64(path.resolve(__dirname, 'file2.docx'));
console.log(base64);
}
test();The three original functions keep their names and positional arguments, so existing calls continue to work. What changed:
- Node >= 18 is now required, and the DOCX-to-HTML backend moved from the
unmaintained
mammoth-stylefork tomammoth, which can render slightly different HTML for the same document. - Conversion no longer blocks the event loop. It previously ran through
execSync, which froze the whole process for the duration of the conversion. - Arguments are passed to LibreOffice as an argument array instead of being interpolated into a shell command string.
- Each conversion runs with a private LibreOffice profile, so concurrent conversions no longer collide.
- Unsupported platforms now raise a clear error instead of silently returning a path to a file that was never created.
- The output format is validated strictly: values like
xdocused to slip through and now raiseFormat to be converted not accepted. - Errors keep their original messages and now carry the underlying failure in
error.cause.