How to Watermark PDF with JavaScript in NodeJS

Learn how to apply an watermarked image in a PDF using JavaScript to call the Watermark PDF API Tool from pdfRest.
Share this page

Why Watermark PDF with JavaScript?

The pdfRest Watermark PDF API Tool is a powerful resource for adding watermarks to PDF documents programmatically. This tutorial will guide you through the process of sending an API call to Watermark PDF using JavaScript.

A common use case for watermarking PDFs is to protect intellectual property or to mark documents as confidential, draft, or for review. For instance, a company might watermark a business proposal with the word "DRAFT" before it's finalized, or a photographer might watermark their portfolio to prevent unauthorized use of their images.

Watermark PDF with JavaScript Code Example

/**
 * This request demonstrates how to apply a text watermark to a PDF.
 * Horizontal and vertical offsets of the watermark are measured in PDF units. (1 inch = 72 PDF units)
 */
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

// Create a new form data instance and append the PDF file and parameters to it
var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));
data.append('watermark_text', 'Hello, watermarked world!');
data.append('font', 'Arial');
data.append('text_size', '72');
data.append('text_color_rgb', '255,0,0');
data.append('opacity', '0.5');
data.append('x', '0');
data.append('y', '0');
data.append('rotation', '0');
data.append('output', 'pdfrest_watermarked_pdf');

// define configuration options for axios request
var config = {
  method: 'post',
  maxBodyLength: Infinity, // set maximum length of the request body
  url: 'https://api.pdfrest.com/watermarked-pdf', 
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', // Replace with your API key
    ...data.getHeaders() // set headers for the request
  },
  data : data // set the data to be sent with the request
};

// send request and handle response or error
axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error); 
});

// If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.

Source: GitHub

Breaking Down the JavaScript Code

The provided code block uses the Axios library to perform an HTTP POST request, sending a multipart form-data payload to the pdfRest API to watermark a PDF. Here's a breakdown of the key parts:

var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

This initializes the required modules: Axios for making HTTP requests, FormData to construct the multipart form-data payload, and fs to interact with the file system.

var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file'));

A new FormData instance is created, and the PDF file is appended as a stream. Replace '/path/to/file' with the actual file path.

The other data.append lines add parameters for the watermark, such as the text, font, size, color, opacity, position, rotation, and output filename. Each parameter customizes the watermark according to your needs.

var config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://api.pdfrest.com/watermarked-pdf',
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    ...data.getHeaders()
  },
  data : data
};

The configuration object for Axios includes the request method, URL, headers (including the API key), and the data payload. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key.

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error); 
});

The Axios library sends the request and handles the response or error. The response data is logged to the console.

Beyond the Tutorial

In this tutorial, we've demonstrated how to watermark a PDF using JavaScript and the pdfRest Watermark PDF API Tool. By following the steps outlined, you can apply a custom watermark to your PDF documents programmatically. To explore more features and capabilities, you can demo all of the pdfRest API Tools in the API Lab and refer to the API Reference documentation.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.