docx-to-pdf-lite
v1.0.0
Published
DOCX to PDF conversion for Node.js with Microsoft Word, LibreOffice, and docx-preview/PlutoPrint engines.
Maintainers
Readme
docx-to-pdf-lite
Convert .docx files to .pdf from Node.js.
This package gives the best results when Microsoft Word is installed locally. On Windows, the default auto engine uses Word's own PDF export first, which is the closest result to Word Print Preview / Save as PDF.
If Microsoft Word is not available, it falls back to LibreOffice when installed. If neither Word nor LibreOffice is available, it uses a pure Node fallback based on docx-preview and plutoprint.
Installation
npm install docx-to-pdf-liteBasic Usage
const { convertDocxToPdf } = require('docx-to-pdf-lite');
await convertDocxToPdf('input.docx', 'output.pdf');API
convertDocxToPdf(inputPath, outputPath, options?)
Converts a .docx file to a .pdf file.
Parameters
inputPath(string): Path to the source.docxfile.outputPath(string): Path where the generated.pdffile should be saved.options(object, optional): Conversion options.
Options
{
engine: 'auto', // 'auto', 'msword', 'libreoffice', or 'preview'
format: 'A4', // 'A4' or 'Letter'; used by the preview engine
margin: null, // optional preview-engine margin object
timeout: 120000,
libreOfficePath: 'C:\\Program Files\\LibreOffice\\program\\soffice.exe'
}Engines
auto: Default. Tries Microsoft Word on Windows, then LibreOffice, then the preview fallback.msword: Uses Microsoft Word COM automation. This gives the most accurate output, but only works on Windows with Microsoft Word installed locally.libreoffice: Uses local LibreOffice /sofficeheadless conversion. This is the best open-source fallback when Word is unavailable.preview: Usesdocx-previewandplutoprint. This is the lightest fallback, but it is not pixel-perfect for complex Word documents.
Returns
Promise<void>
Best Fidelity
For perfect or near-perfect rendering, use:
await convertDocxToPdf('input.docx', 'output.pdf', {
engine: 'msword'
});This requires Microsoft Word to be installed on the same Windows machine running Node.js.
LibreOffice Fallback
LibreOffice is used automatically by engine: 'auto' when Word is unavailable.
You can force it:
await convertDocxToPdf('input.docx', 'output.pdf', {
engine: 'libreoffice'
});If LibreOffice is not on PATH, pass the executable path:
await convertDocxToPdf('input.docx', 'output.pdf', {
engine: 'libreoffice',
libreOfficePath: 'C:\\Program Files\\LibreOffice\\program\\soffice.exe'
});Preview Fallback
The preview engine does not require Microsoft Word or LibreOffice:
await convertDocxToPdf('input.docx', 'output.pdf', {
engine: 'preview'
});This path is cross-platform, but complex spacing, indentation, fonts, headers, footers, and page breaks may differ from Microsoft Word.
Limitations
- Perfect Word-like output requires Microsoft Word installed locally on Windows.
- LibreOffice output is usually good, but can still differ from Microsoft Word for complex documents.
- The preview engine is a fallback only and is not intended to be pixel-perfect.
plutoprintincludes prebuilt PlutoBook binaries on Windows and Linux x86_64. macOS may require PlutoBook to be installed separately with Homebrew.- Password-protected or corrupted DOCX files are not supported.
