Skip to content
pdfconversionapi

Guide6 min read

Convert DOCX to PDF in Node.js: the free options, tested

libreoffice-convert needs a 384 MB install, mammoth with Puppeteer loses the page layout, docx-pdf crashed. Or fetch and PDFConversionAPI. Tested on Node.js 26.

Written and tested by Zeel Shekhaliya, Founder

Contents (7 sections)

The usual answers for turning a Word file into a PDF in Node.js each cost something in our tests. libreoffice-convert, the most used package for it, converted all three files after a 384 MB LibreOffice install, and printed the contract's tracked changes into the PDF. mammoth with Puppeteer kept the words but not the page: no margins, no table borders, one font, and the 6-page handbook came out as 2 pages. docx-pdf ended the process looking for PhantomJS. PDFConversionAPI, our API, needs fetch, which ships with Node.js: send the .docx, get the PDF back. Every option it takes, such as UpdateFields, is listed on the Word to PDF API page.

With PDFConversionAPI: fetch only

pdfconversionapi.mjs
// Word to PDF with pdfconversionapi.com. Node.js 18 or later, no npm packages.
const API = "https://api.pdfconversionapi.com/v2/convert/docx/to/pdf";

/** Sends a .docx (a Buffer) and returns the PDF as a Buffer. Options are API parameters, such as { UpdateFields: "true" }. */
export async function docxToPdf(docx, fileName = "document.docx", options = {}) {
  const form = new FormData();
  form.append("File", new Blob([docx]), fileName);
  for (const [name, value] of Object.entries(options)) form.append(name, value);

  const response = await fetch(API, {
    method: "POST",
    headers: { Authorization: `Bearer ${process.env.PDFCONVERSIONAPI_KEY}` },
    body: form,
    signal: AbortSignal.timeout(240_000),
  });
  const body = await response.json();
  if (!response.ok) throw new Error(`${response.status} ${body.Message}`);
  return Buffer.from(body.Files[0].FileData, "base64");
}

Set PDFCONVERSIONAPI_KEY to your key (you get one by signing in at app.pdfconversionapi.com), then:

JavaScript
import { readFile, readdir, writeFile } from "node:fs/promises";
import { docxToPdf } from "./pdfconversionapi.mjs";

const report = await docxToPdf(await readFile("quarterly-report.docx"), "quarterly-report.docx");
await writeFile("quarterly-report.pdf", report);

const handbook = await docxToPdf(await readFile("field-service-handbook.docx"), "field-service-handbook.docx", { UpdateFields: "true" });
await writeFile("field-service-handbook.pdf", handbook);

for (const name of await readdir("contracts")) {
  if (!name.endsWith(".docx")) continue;
  const pdf = await docxToPdf(await readFile(`contracts/${name}`), name);
  await writeFile(`contracts/${name.replace(/\.docx$/, ".pdf")}`, pdf);
}
  • No npm package: fetch, FormData and Blob are built into Node.js 18 and later.
  • The function takes and returns a Buffer, so the same call works for a file on disk, an upload your server just received, or an object read from storage.
  • The PDF arrives as base64 in the JSON response, in Files[0].FileData.
  • { UpdateFields: "true" } rebuilds the table of contents before the PDF is written: the handbook's contents list, saved before its last edits, came out with the new section names and page numbers. Table of contents not updating has the details.
  • An option DOCX to PDF can't apply is refused, not ignored: { PageRange: "1-2" } threw Error: 400 PageRange is not supported yet for DOCX → PDF.
  • AbortSignal.timeout(240_000) leaves room for a conversion of up to 120 seconds plus the upload (Limits).
  • The loop sends one file at a time, which is what the Free plan allows: a second request while one is running gets 429 with Retry-After: 5. Paid plans run 4 to 30 at once (Pricing), so Promise.all over a few files works there.

Against the container on our own machine, a conversion took 0.03 to 0.13 seconds after the first. Over the internet you add the upload and download; from Google's servers to the hosted API, a call took 1.0 to 3.7 seconds in our Apps Script test.

With libreoffice-convert

libreoffice-convert is the npm package most answers point to, with about 330,000 downloads a week. It runs LibreOffice for you, so LibreOffice has to be on the server. In the Node.js slim image, apt-get install --no-install-recommends libreoffice-writer-nogui added 139 packages: 110 MB to download and 384 MB on disk. The package and its two dependencies take 1.2 MB. This is the code we ran:

libreoffice-pdf.mjs
// Word to PDF with libreoffice-convert. Needs LibreOffice installed (apt install libreoffice-writer-nogui).
import { readFile, writeFile } from "node:fs/promises";
import libre from "libreoffice-convert";

/** Converts a .docx Buffer to a PDF Buffer, and gives up after 120 seconds. */
const toPdf = (docx) => new Promise((resolve, reject) =>
  libre.convertWithOptions(docx, ".pdf", undefined, { execOptions: { timeout: 120_000 } },
    (err, pdf) => (err ? reject(err) : resolve(pdf))));

await writeFile("quarterly-report.pdf", await toPdf(await readFile("quarterly-report.docx")));
  • A file took 0.55 to 0.93 seconds. The package gives each call a LibreOffice profile of its own, so 4 conversions at once (0.87 seconds) and 8 at once (1.35 seconds) wrote every PDF. In our Python test, four calls sharing one profile wrote only two.
  • It sets no time limit of its own. execOptions.timeout above stops a stuck conversion; set to 200 ms as a test, the call failed with Command failed: /usr/bin/libreoffice ….
  • The README's promisify(libre.convert) also works, but on Node.js 26 it prints DeprecationWarning: Calling promisify on a function that returns a Promise is likely a mistake. The Promise wrapper above doesn't.
  • The image had none of the fonts these files name, so LibreOffice drew every page in DejaVu, and the report's table headings broke mid-word ("Shipme nts"). On Linux, install the fonts your documents use (for Word's defaults, fonts-crosextra-carlito and fonts-crosextra-caladea).
  • It printed the contract's tracked changes as markup: "The buyer may pay by check" and "twelve", both deleted in the draft, are in the PDF's text.
  • Without the package, calling soffice through execFile with a profile per call gave the same PDFs in 0.56 to 0.86 seconds; that version is in the test files.

mammoth and Puppeteer: the words, not the page

mammoth turns a .docx into plain HTML, and Puppeteer prints HTML to PDF with a headless browser. It's a common answer for doing the job in JavaScript alone. We ran this for each file:

JavaScript
// mammoth makes HTML from the .docx; Puppeteer prints it.
import { readFile, writeFile } from "node:fs/promises";
import mammoth from "mammoth";
import puppeteer from "puppeteer";

const { value: html } = await mammoth.convertToHtml({ buffer: await readFile("quarterly-report.docx") });
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(`<!doctype html><meta charset="utf-8">${html}`);
await writeFile("quarterly-report.pdf", await page.pdf({ format: "Letter" }));
await browser.close();
Page 1 of the quarterly report. Our API: margins, the report's serif body text, a small chart and a bordered table. mammoth with Puppeteer: text running to the page edges in one sans-serif font, and the chart filling the page width
The quarterly report, page 1. Left: our API. Right: mammoth 1.13 with Puppeteer 25.19.
  • mammoth keeps headings, lists, tables and images, and leaves out the page itself: size, margins, headers and footers, fonts and table borders. It warned for each style it dropped, such as Unrecognised paragraph style: 'Title'.
  • The PDF is the HTML with the browser's defaults: the 3-page report came out as 2 pages, and the 6-page handbook as 2, with no page breaks before its sections and no bookmarks. Getting the Word look back means writing the CSS yourself.
  • On the contract it kept the agreed text (deleted words out, inserted words in) and left out the comments and the hidden note.
  • Puppeteer needs a browser. When you install it, it downloads Chrome for Testing and a headless shell: 340 MB of archives on x86-64 Linux, 336 MB on arm64. In our containers the download finished but never unpacked, and we didn't find why, so we ran it with the browser from the system's packages, which added 167 packages and 765 MB.
  • Run as root in a container, puppeteer.launch() failed with Failed to launch the browser process; it started with args: ["--no-sandbox"], which turns off the browser's sandbox.
  • Once the browser was running, a file took 0.06 to 0.15 seconds.

docx-pdf: needs PhantomJS

docx-pdf, about 4,000 downloads a week and last published in June 2022 as version 0.0.1, is mammoth plus html-pdf, which drives PhantomJS, a browser that is no longer developed. npm installed it without PhantomJS, and the first call ended the Node.js process with html-pdf: Failed to load PhantomJS module. You have to set the path to the PhantomJS binary using 'options.phantomPath'.

What the PDFs contain

Our APIlibreoffice-convert (LibreOffice 25.2)mammoth + Puppeteer
Pages: report, handbook3 and 6, as in Word3 and 62 and 2
Bookmarks from headingsYesYesNo
The contract's tracked changesPrinted as acceptedPrinted as markupPrinted as accepted
Page layout: margins, table bordersKeptKeptLost
Fonts, with nothing installed on your sideLiberation for the report, Carlito and Caladea for the contractDejaVuOne sans-serif font
What your server needsNothing but Node.js139 packages, 384 MBA browser: Puppeteer downloads 340 MB of archives

docx-pdf wrote no PDF.

Which one to use

  • libreoffice-convert: when you control the server image, can add about 384 MB to it, install the fonts your documents name, and accept tracked changes in the PDF or clean them out first. Set a time limit.
  • mammoth with Puppeteer: when you want the document's text in your own HTML design, such as a web page or an email, not a copy of the Word page.
  • Our API: when the files are ordinary business documents, the code runs in a function or a small container, or you'd rather not keep an office suite or a browser up to date on your server. The Free plan includes 250 conversions a month, files up to 10 MB and 1 conversion at a time; files are converted and returned, and nothing is kept unless you ask for a download link.

How we tested

Date
2026-11-26. Every request and result on this page ran that day.
System
Containers from the official Node.js slim image (node:lts-slim, Node.js 26.13.1), on an arm64 Mac, with no extra fonts installed. LibreOffice 25.2.3.2 from the image's package manager, apt, with libreoffice-convert 1.9.1 from npm. mammoth 1.13.0 and puppeteer 25.19.0 from npm. Puppeteer's own download of Chrome for Testing finished but never unpacked in our containers, on Node.js 24 or 26, so those runs used the browser from the image's own packages. docx-pdf 0.0.1 from npm.
Server
Our API running locally in a container, built from the same source as the hosted API; on 2026-11-25 the same build's PDF of the contract draft was byte for byte the PDF the hosted API returned. The code shows the hosted address; we ran it with only the address changed. The API times on this page therefore leave out the trip over the internet.
Files
The three Word files from our Python guide (download them above): a 3-page quarterly report that names Liberation Serif and Liberation Sans; a contract draft that names Calibri and Cambria, with tracked changes, two comments and a hidden note; and a 6-page handbook whose table of contents was saved before its last edits.
Runs
Our API three times per file, the handbook with UpdateFields=true, the usage lines on this page, and one request with an option DOCX to PDF doesn't support. libreoffice-convert twice per file, then 4 and 8 conversions at once, the README's promisify line, and a 200 ms time limit. mammoth with Puppeteer once per file, printed on US Letter. docx-pdf once.
Checked with
Page count, page size, fonts, bookmarks, document properties and page text read with PyMuPDF 1.27.2; first pages rendered with PyMuPDF and compared by eye; install sizes as apt and du reported them.
Not checked
These routes with extra fonts installed, Puppeteer with its own Chrome, Windows, macOS, Microsoft Word itself, and files larger than 26 KB.

Run your own files through the API.