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

Table of contents

    Convert an Excel workbook to PDF using libreoffice-convert with a local LibreOffice installation, or Nutrient Node.js SDK without an Office installation. Both examples use the same sample workbook so the results can be compared.
    Convert Excel (XLS/XLSX) to PDF in Node.js: Step-by-step guide
    Summary

    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

    ToolRecommended versionPurpose
    Node.js(opens in a new tab)Supported long-term support (LTS) releaseRuntime
    npm(opens in a new tab)Comes with Node.jsPackage manager
    LibreOffice(opens in a new tab)Current release (Approach 1 only)Headless export engine

    Verify the 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).

    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

    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.

    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:

    Terminal window
    node libreoffice.mjs

    The 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

    Requirementlibreoffice-convertNutrient Node.js SDK
    Office installationRequires LibreOfficeNo LibreOffice or Microsoft Office installation
    Headless conversionRuns LibreOffice with --headlessRuns in the Node.js process
    FontsMake fonts available to LibreOfficeSupply 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

    1. Initialize a new Node.js project:

      Terminal window
      npm init -y

      This command creates a package.json file in the project directory for managing the project’s dependencies.

    2. Install the Nutrient Node.js SDK package:

      Terminal window
      npm install @nutrient-sdk/node
    3. Save chart.xlsx in the project directory, keeping its filename.

    4. Create nutrient.mjs in 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. The finally block 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.

    5. 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

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

    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.

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

    Use libreoffice-convert with a LibreOffice installation, or @nutrient-sdk/node for conversion without one.

    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?

    Without a license, output includes a watermark. Removing it requires a valid license key, available 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 converts the workbook without a LibreOffice installation.

    Hulya Masharipov

    Hulya Masharipov

    Technical Writer

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

    Explore related topics

    Nutrient SDK Convert and process PDFs at scale