Add PDF functionality with Node.js

This guide walks you through the steps necessary to integrate Nutrient dependency into a Node.js project.

Requirements

Setting up a Node.js project

  1. First, you need to have Node.js and npm (Node Package Manager) installed on your system. You can download Node.js from the official website(opens in a new tab), and npm will be included.

  2. Once you have Node.js and npm installed, you can verify the installation by running the following commands in your terminal:

    Terminal window
    node -v
    npm -v
  3. Now, navigate to the directory where you want to set up your project. Once you’re in the desired directory, initialize a new Node.js project by running:

    Terminal window
    npm init -y

This command will create a new package.json file in your directory. The -y flag is used to accept the default values proposed by the questionnaire that usually follows npm init. If instead, you’d like to further define details of your project, you can omit the -y flag from the npm init command.

Adding the Nutrient dependency

Open a terminal and move to the root directory of your Node.js project. Install the @nutrient-sdk/node library into your project by running the following command in your terminal:

Terminal window
npm install @nutrient-sdk/node

After running this command, @nutrient-sdk/node will be added to the list of dependencies in your package.json file.

At this point, you’re ready to develop your Node.js application using Nutrient Node.js SDK.

Converting a document to PDF

  1. Add an Office document or a PNG or JPEG image to your project directory. For this example, you’ll be converting a Word document. You can name it anything you want, but for the sake of this tutorial, call it sample.docx. Remember to update the file name in the following steps if you choose a different name.

  2. Create a new JavaScript file in your project root directory. You can name it anything, but for this example, name it index.js.

  3. In the index.js file, add the following code to import the @nutrient-sdk/node library and convert the document to PDF:

    const { load } = require('@nutrient-sdk/node');
    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();

    At this point, the resulting PDF is converted but shows a Nutrient watermark. To convert documents without showing a watermark, specify a license property as part of the object passed as an argument to the load() function call containing key and appName properties — for example:

    async function convertToPDF() {
    ...
    const instance = await load({
    ...
    license: {
    key: "LICENSE_KEY",
    appName: "APP_NAME"
    }
    });
    ...
    };

Contact Sales for more information about obtaining a trial license key.

Running the project

To run the script, navigate to your project directory in the terminal and run the following command:

Terminal window
node index.js

This will execute the index.js script, and a new converted.pdf file will appear in your filesystem.