Blog Post

How to convert Word (DOC/DOCX) to PDF using Node.js

Illustration: How to convert Word (DOC/DOCX) to PDF using Node.js

In the first part of this blog post, you’ll learn how to use the libreoffice-convert library with Node.js to convert a Word file to PDF. This post will also cover its features and when it’s a suitable choice.

In the second part, you’ll delve into integrating [Nutrient Node.js SDK][]. You’ll learn how to convert documents step by step and leverage its capabilities for efficient document handling.

Requirements

Before you begin, make sure you have the following installed:

  • Node.js — It’s best to use the latest long-term support (LTS) version or a stable version.

  • npm — Node Package Manager comes bundled with Node.js, so it’ll be installed by default.

  • LibreOffice — This enables you to convert Word documents to PDF. LibreOffice is a free and open source Office suite that provides a range of features for document handling. It’s available for Windows, macOS, and Linux.

Verify your Node.js and npm installations by running these commands in your terminal:

node -v
npm -v

Setting up the project

  1. Navigate to your desired project directory and initialize a new Node.js project:

npm init -y
  1. Install the libreoffice-convert package:

npm i libreoffice-convert

Converting Word documents to PDF using libreoffice-convert

  1. Create a new file named index.js and import the required modules:

const path = require('path');
const fs = require('fs').promises;
const libre = require('libreoffice-convert');
libre.convertAsync = require('util').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 main() {
	const ext = 'pdf'; // Output extension.
	const inputPath = path.join(__dirname, '/example.doc');
	const outputPath = path.join(__dirname, `/example.${ext}`);

	// Read the input file.
	const docxBuf = await fs.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 Word document.

  1. Convert to PDF format:

async function main() {
	// ....

	// Convert to PDF format with an undefined filter.
	let pdfBuf = await libre.convertAsync(docxBuf, ext, undefined);
}

Here, you used the libre.convertAsync method to convert the Word document’s contents (in docxBuf) 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 main() {
	// ...

	// Save the converted PDF.
	await fs.writeFile(outputPath, pdfBuf);
}

main().catch(function (err) {
	console.log(`Error converting file: ${err}`);
});

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

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.

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, PPTX, and 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:

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:

npm install @pspdfkit/nodejs
  1. Place your Office document (e.g. sample.docx) in your project directory.

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

const { load } = require('@pspdfkit/nodejs');
const fs = require('node:fs');

async function convertToPDF() {
	const docx = fs.readFileSync('sample.docx');
	const instance = await load({
		document: docx,
	});
	const buffer = await instance.exportPDF();
	fs.writeFileSync('converted.pdf', Buffer.from(buffer));
	await instance.close();
}

convertToPDF();

In this script, the load() function from the @pspdfkit/nodejs 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.

Information

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, please get in touch with Sales.

  1. Execute the script in your terminal to initiate the conversion process:

node index.js

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 Word-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

Here are a few frequently asked questions about converting Word to PDF.

What is libreoffice-convert and how does it work?

libreoffice-convert is a Node.js library that leverages LibreOffice for document conversion. It provides a simple API to convert Word documents (DOC/DOCX) to PDF by reading the input file and using LibreOffice to process and output the PDF.

How do I convert Word documents to PDF using libreoffice-convert?

To convert Word documents to PDF with libreoffice-convert:

  1. Initialize a Node.js project and install the libreoffice-convert package.

  2. Create a script that imports the required modules, reads the Word document, and converts it to PDF using the convertAsync method.

  3. Save the converted PDF to your desired location.

What is Nutrient Node.js SDK?

Nutrient Node.js SDK is a commercial SDK designed for handling document conversions and manipulations. Unlike libreoffice-convert, Nutrient does not rely on third-party software and offers features such as converting between various formats (PDF, Word, Excel, etc.), handling custom fonts, and advanced rendering.

How do I convert Word documents to PDF using Nutrient Node.js SDK?

To convert Word documents to PDF with Nutrient Node.js SDK:

  1. Initialize a Node.js project and install the @pspdfkit/nodejs package.

  2. Create a script that loads the Word document using Nutrient’s API and exports it as a PDF.

  3. Execute the script to convert the document and save the resulting PDF.

What are the benefits of using Nutrient Node.js SDK over libreoffice-convert?

Nutrient Node.js SDK offers several benefits:

  • Versatile conversion — Supports a broader range of formats beyond DOC/DOCX to PDF.

  • Custom font support — Ensures that custom fonts are correctly handled during conversion.

  • Advanced rendering — Provides additional rendering capabilities, such as exporting PDF pages to PNG or WebP formats.

Author
Hulya Masharipov Technical Writer

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

Related products
Share post
Free trial Ready to get started?
Free trial