Convert Excel (XLS/XLSX) to PDF in Node.js: Step-by-step guide
Table of contents
Use libreoffice-convert to run LibreOffice’s headless export from Node.js, or use Nutrient Node.js SDK to convert without installing LibreOffice. The examples read chart.xlsx and write separate PDF files. Check the resulting pages, fonts, and charts before using either approach with other workbooks.
Prerequisites
| Tool | Recommended version | Purpose |
|---|---|---|
| Node.js(opens in a new tab) | Supported long-term support (LTS) release | Runtime |
| npm(opens in a new tab) | Comes with Node.js | Package manager |
| LibreOffice(opens in a new tab) | Current release (Approach 1 only) | Headless export engine |
Verify the Node.js and npm versions:
node -vnpm -vInstall LibreOffice (only for the first approach)
| OS | Install command |
|---|---|
| macOS (Homebrew) | brew install --cask libreoffice |
| Ubuntu/Debian | sudo apt update && sudo apt install libreoffice |
| Windows (winget) | winget install --id TheDocumentFoundation.LibreOffice -e |
Prefer a GUI installer? Download it from the LibreOffice website(opens in a new tab).
Check that LibreOffice starts before running the Node.js example. On macOS, use /Applications/LibreOffice.app/Contents/MacOS/soffice --version; on Linux, use libreoffice --version. The package’s installation instructions(opens in a new tab) describe where it looks for the executable.
Approach 1: Using libreoffice-convert
Why choose libreoffice-convert
- The Node.js wrapper is MIT-licensed(opens in a new tab); LibreOffice has its own license.
- Supports a broad range of Office file types out of the box.
- Great for batch jobs or migrations when LibreOffice is already installed.
Installation
mkdir excel-to-pdf && cd excel-to-pdfnpm init -ynpm i libreoffice-convertThis command creates a new directory named excel-to-pdf, initializes a new Node.js project, and installs the libreoffice-convert package.
Convert Excel to PDF with libreoffice-convert
Download chart.xlsx into the excel-to-pdf directory. Keep that filename, and save the following complete example as libreoffice.mjs.
import path from "node:path";import { readFile, writeFile } from "node:fs/promises";import libre from "libreoffice-convert";
async function convertExcelWithLibre() { try { const inputPath = path.resolve("chart.xlsx"); const outputPath = path.resolve("libreoffice.pdf");
const xlsxBuf = await readFile(inputPath); const pdfBuf = await new Promise((resolve, reject) => { libre.convert(xlsxBuf, "pdf", undefined, (error, result) => { if (error) { reject(error); } else { resolve(result); } }); });
await writeFile(outputPath, pdfBuf); console.log("Conversion finished:", outputPath); } catch (err) { console.error("Conversion failed:", err); process.exitCode = 1; }}
convertExcelWithLibre();Run it from the directory containing chart.xlsx:
node libreoffice.mjsThe script passes the workbook bytes and the "pdf" output format to LibreOffice and writes libreoffice.pdf. If the executable is missing or conversion fails, it reports the error and exits with a nonzero status.
Approach 2: Using Nutrient Node.js SDK
Nutrient Node.js SDK converts Office documents to PDF inside a Node.js application without installing LibreOffice or Microsoft Office. The Office conversion guide documents the load() and exportPDF() methods used below.
Comparing the two approaches
| Requirement | libreoffice-convert | Nutrient Node.js SDK |
|---|---|---|
| Office installation | Requires LibreOffice | No LibreOffice or Microsoft Office installation |
| Headless conversion | Runs LibreOffice with --headless | Runs in the Node.js process |
| Fonts | Make fonts available to LibreOffice | Supply custom font files when needed |
Test the workbook’s print areas, page breaks, and fonts in the chosen runtime. Installing a package alone doesn’t establish PDF/A conformance or serverless compatibility.
Convert Excel to PDF with Nutrient Node.js SDK
Initialize a new Node.js project:
Terminal window npm init -yThis command creates a
package.jsonfile in the project directory for managing the project’s dependencies.Install the Nutrient Node.js SDK package:
Terminal window npm install @nutrient-sdk/nodeSave chart.xlsx in the project directory, keeping its filename.
Create
nutrient.mjsin the same directory:import { load } from "@nutrient-sdk/node";import { readFileSync, writeFileSync } from "node:fs";async function convertExcelWithNutrient() {const excel = readFileSync("chart.xlsx");const instance = await load({document: excel,});try {const pdf = await instance.exportPDF();writeFileSync("nutrient.pdf", Buffer.from(pdf));console.log("Done! File written to nutrient.pdf");} finally {await instance.close();}}convertExcelWithNutrient().catch((error) => {console.error("Conversion failed:", error);process.exitCode = 1;});The script loads the workbook and exports
nutrient.pdf. Thefinallyblock closes the document even if export or writing the file fails.Trial output includes a watermark. To configure a license, follow the Node.js licensing guide.
Run the script to start the conversion:
Terminal window node nutrient.mjs
Open nutrient.pdf and compare it with the workbook. Check each sheet’s print area, page count, and values, plus any chart labels. If fonts change, follow the custom font instructions and convert again.
The sample exported as one tall page in a macOS test with Node.js 24.14.1 and Nutrient Node.js SDK 1.3.0. One long museum name was clipped. Review column widths and pagination before distributing the output.
Conclusion
Use libreoffice-convert when the application can run and maintain a LibreOffice installation. Use Nutrient Node.js SDK when Office conversion needs to run within the Node.js application without that installation.
Follow the Node.js conversion guide to try Nutrient with a representative workbook, or request a trial. Check the exported PDF before distributing it or passing it to another document workflow.
FAQ
Use the libreoffice-convert package to read the file, convert it with libre.convert, and save the output. Alternatively, use @nutrient-sdk/node to load the document and export it as a PDF.
Use libreoffice-convert with a LibreOffice installation, or @nutrient-sdk/node for conversion without one.
Install the libraries using npm with npm install libreoffice-convert or npm install @nutrient-sdk/node.
Without a license, output includes a watermark. Removing it requires a valid license key, available from Nutrient’s Sales team.
libreoffice-convert and @nutrient-sdk/node?libreoffice-convert relies on LibreOffice for conversion and requires it to be installed, while @nutrient-sdk/node converts the workbook without a LibreOffice installation.