Convert MS Office Files to PDF in Node.js

PSPDFKit for Node.js 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, PSPDFKit for Node.js 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, PSPDFKit for Node.js might not have access to these fonts due to licensing constraints. In this case, PSPDFKit 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 PSPDFKit.

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();