Convert Excel (XLS/XLSX) to PDF in Node.js — Step‑by‑step guide

Converting Excel spreadsheets to PDF is a common requirement in report generation, compliance, and archiving workflows. In this tutorial, you’ll explore two production‑ready approaches: libreoffice-convert, an open source wrapper around LibreOffice’s headless export engine, and Nutrient Node.js SDK, a fully self‑contained approach that ships with no external dependencies.
Convert Excel (XLS/XLSX) to PDF in Node.js — Step‑by‑step guide

Prerequisites

ToolRecommended versionPurpose
Node.js(opens in a new tab)Current LTS (v18+)Runtime
npm(opens in a new tab)Comes with Node.jsPackage manager
LibreOffice(opens in a new tab)v7.6+ (only for Approach 1)Headless export engine

Verify your Node.js and npm versions:

Terminal window
node -v
npm -v

Install LibreOffice (only for the first approach)

OSInstall command
macOS (Homebrew)brew install --cask libreoffice
Ubuntu / Debiansudo 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).

After installation, verify that the soffice binary is on the PATH:

Terminal window
libreoffice --version

Approach 1 — Using libreoffice-convert

Why choose libreoffice-convert

  • Free and open source (MPL 2.0).
  • Supports a broad range of Office file types out of the box.
  • Great for batch jobs or migrations when LibreOffice is already installed.

Installation

Terminal window
mkdir excel-to-pdf && cd excel-to-pdf
npm init -y
npm i libreoffice-convert

This command creates a new directory named excel-to-pdf, initializes a new Node.js project, and installs the libreoffice-convert package.

Converting Excel documents to PDF using libreoffice-convert

  1. Create a new file named index.mjs, and import the required modules:
import path from 'node:path';
import { readFile, writeFile } from 'node:fs/promises';
import libre from 'libreoffice-convert';
import { promisify } from 'node:util';
libre.convertAsync = promisify(libre.convert);

In the code above, you imported the necessary modules: path for working with file paths, fs.promises for file system operations, and libreoffice-convert, along with its asynchronous conversion method.

  1. Define the file paths and read the input file:
async function convertExcelWithLibre() {
const inputPath = path.resolve('example.xlsx');
const outputPath = path.resolve('example.pdf');
// Read the input file.
const xlsxBuf = await readFile(inputPath);
}

Here, you defined the output extension as 'pdf' and set the input and output paths. Then, you used fs.readFile to asynchronously read the input Excel document.

  1. Convert to PDF format:
async function convertExcelWithLibre() {
// ....
// Convert to PDF format with an undefined filter.
let pdfBuf = await libre.convertAsync(xlsxBuf, ext, undefined);
}

Here, you used the libre.convertAsync method to convert the Excel document’s contents (in xlsxBuf) to PDF format. The third argument is the filter, which is left undefined. LibreOffice automatically selects a suitable filter based on the input and output formats.

  1. Save the converted PDF:
async function convertExcelWithLibre() {
// ...
// Save the converted PDF.
await writeFile(outputPath, pdfBuf);
console.log('Conversion finished:', outputPath);
}
convertExcelWithLibre();

You saved the converted PDF data (in pdfBuf) to the specified output path using fs.writeFile.

Here’s the complete code:

import path from 'node:path';
import { readFile, writeFile } from 'node:fs/promises';
import libre from 'libreoffice-convert';
import { promisify } from 'node:util';
libre.convertAsync = promisify(libre.convert);
async function convertExcelWithLibre() {
try {
const inputPath = path.resolve('document.xlsx');
const outputPath = path.resolve('example.pdf');
const xlsxBuf = await readFile(inputPath);
const pdfBuf = await libre.convertAsync(
xlsxBuf,
'pdf',
undefined,
);
await writeFile(outputPath, pdfBuf);
console.log('Conversion finished:', outputPath);
} catch (err) {
console.error('Conversion failed:', err);
}
}
convertExcelWithLibre();

Run it with:

Terminal window
node index.mjs

Approach 2 — Using Nutrient Node.js SDK

Unlike libreoffice-convert, Nutrient Node.js SDK doesn’t rely on third-party software like LibreOffice to convert documents. It relies on its own technology built from the ground up.

Why choose Nutrient Node.js SDK

Featurelibreoffice-convertNutrient SDK
External dependenciesRequires LibreOfficeNone
Headless friendly
PDF/A and ISO conformance
Serverless ready⚠️ Large binaries
Custom font embeddingBasicAdvanced

Features of Nutrient Node.js SDK

Nutrient Node.js SDK offers a range of features that make it a powerful choice for document conversion within Node.js applications:

  • Versatile conversion — Convert PDF, Word, Excel, PowerPoint, TIFF, JPG, and PNG files seamlessly, without relying on third-party tools or MS Office licenses.

  • Custom font support — Preserve document integrity by handling custom fonts intelligently during conversion, ensuring consistent visual representation.

  • Diverse file support — Handle a wide array of formats, including DOCX, XLSX, and PPTX, as well as image types like PNG and JPEG.

  • Advanced rendering — Render PDF pages into PNG or WebP formats for versatile visualization options.

Integrating Nutrient Node.js SDK

  1. Initialize a new Node.js project:
Terminal window
npm init -y

This command creates a package.json file in your project directory, which is essential for managing your project’s dependencies.

  1. Install the Nutrient Node.js SDK package:
Terminal window
npm install @nutrient-sdk/node
  1. Place your Office document (e.g. example.xlsx) in your project directory.

  2. Create a new JavaScript file named index.mjs in your project directory. This script will handle the conversion process:

import { load } from '@nutrient-sdk/node';
import { readFileSync, writeFileSync } from 'node:fs';
async function convertExcelWithNutrient() {
const excel = readFileSync('document.xlsx');
const instance = await load({
document: excel,
});
const pdf = await instance.exportPDF({});
writeFileSync('converted.pdf', Buffer.from(pdf));
await instance.close();
console.log('Done! File written to converted.pdf');
}
convertExcelWithNutrient().catch(console.error);

In this script, the load() function from the @nutrient-sdk/node package is used to load the Office document and initiate the conversion process. The converted PDF is then saved as converted.pdf in your project directory.

By default, the resulting PDF will include a Nutrient watermark. To exclude the watermark, you can specify the license property with a key and appName when calling the load() function. For details on obtaining a trial license key, contact Sales.

  1. Execute the script in your terminal to initiate the conversion process:
Terminal window
node index.mjs

Once executed, the script will convert the Office document to PDF and save the resulting PDF as converted.pdf in your project directory.

Conclusion

In this post, you explored two approaches to document conversion in Node.js applications. The first part covered using the libreoffice-convert library for seamless Excel-to-PDF conversion.

In the second part, you learned about Nutrient Node.js SDK, a robust SDK offering versatile document conversion features. Whether you prefer libreoffice-convert’s simplicity or Nutrient’s power, you can now integrate efficient document conversion into your Node.js projects. Choose the solution that fits your needs, and enhance your document management and conversion capabilities.

To get started with Nutrient Node.js SDK, you can either:

  • Start your free trial to test the library and see how it works in your application.
  • Launch our demo to see the viewer in action.

FAQ

How do I convert an Excel (XLS/XLSX) file to PDF using Node.js?

You can use the libreoffice-convert package to convert Excel files by reading the file, converting it with libre.convertAsync, and saving the output. Alternatively, use @nutrient-sdk/node to load the document and export it as a PDF.

What libraries are required for converting Excel to PDF in Node.js?

You need libreoffice-convert for using LibreOffice, or @nutrient-sdk/node for using Nutrient without needing external software.

How do I install the required libraries for this conversion?

Install the libraries using npm with npm install libreoffice-convert or npm install @nutrient-sdk/node.

Can I use Nutrient Node.js SDK without a license?

Nutrient includes a watermark by default; to remove it, you need a valid license key, which you can obtain from Nutrient’s Sales team.

What is the difference between using libreoffice-convert and @nutrient-sdk/node?

libreoffice-convert relies on LibreOffice for conversion and requires it to be installed, while @nutrient-sdk/node uses its own technology and doesn’t need external software.

Hulya Masharipov

Hulya Masharipov

Technical Writer

Hulya is a frontend web developer and technical writer at Nutrient who enjoys creating responsive, scalable, and maintainable web experiences. She’s passionate about open source, web accessibility, cybersecurity privacy, and blockchain.

Explore related topics

FREE TRIAL Ready to get started?