Convert MS Office files to PDF in Node.js

Nutrient Node.js SDK is a library for converting Office documents to PDF directly in Node.js applications. To convert Office documents such as DOCX, XLSX, and PPTX to PDF, Nutrient Node.js SDK relies entirely on its own technology built from the ground up, and it doesn’t depend on third-party tools such as LibreOffice or Microsoft Office. For more information on the supported Office formats, see the list of supported file types.

Converting Office documents to PDFs

The example below loads an Office document and exports it to a PDF:

import fs from "node:fs";
import { load } from "@pspdfkit/nodejs";

const docxFile = fs.readFileSync("source.docx");

const instance = await load({ document: docxFile });
const buffer = await instance.exportPDF();

fs.writeFileSync("converted.pdf", Buffer.from(buffer));
await instance.close();

Converting Office documents with custom fonts to PDFs

When you convert an Office document with custom fonts to a PDF, Nutrient Node.js SDK might not have access to these fonts due to licensing constraints. In this case, Nutrient replaces unavailable fonts with their equivalents — like Arial with Noto — by default. To make sure the output PDF uses the same fonts as the original Office document, provide the path to the custom font files to Nutrient.

The example below loads an Office document specifying some custom fonts and exports the document to a PDF:

import fs from "node:fs";
import { load } from "@pspdfkit/nodejs";

const docxFile = fs.readFileSync("source.docx");
const arial = {
  name: "arial.ttf",
  data: fs.readFileSync("./arial.ttf")
};
const tahoma = {
  name: "tahoma.ttf",
  data: fs.readFileSync("./tahoma.ttf")
};

const instance = await load({
  document: docxFile,
  fonts: [arial, tahoma]
});
const buffer = await instance.exportPDF();

fs.writeFileSync("converted.pdf", Buffer.from(buffer));
instance.close();