How to Redact PDF Text with Javascript in NodeJS

Learn how to use pdfRest Redact PDF API tool with JavaScript to redact text on a PDF document.
Share this page

Why Redact PDF Text with JavaScript?

The pdfRest Redact PDF API Tool is a powerful solution for developers looking to automate the redaction of sensitive information from PDF documents. This tutorial will guide you through the process of sending an API call to Redact PDF using JavaScript, allowing you to efficiently redact text based on specific criteria.

In real-world scenarios, businesses and individuals often need to share documents that contain confidential information, such as email addresses, phone numbers, or specific keywords. Using the Redact PDF API, you can automatically identify and redact this information, ensuring privacy and compliance with data protection regulations.

Redact PDF Text with JavaScript Code Example

/**
 * This request demonstrates how to apply text redaction previews to a PDF.
 * This is step 1 of 2 toward applying redactions to text.
 */
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'));
var redaction_option_array = [];
var redaction_options1 = {
    "type": "preset",
    "value": "email",
};
var redaction_options2 = {
    "type": "regex",
    "value": "(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]\\d{3}[\\s.-]\\d{4}",
};
var redaction_options3 = {
    "type": "literal",
    "value": "word",
};
redaction_option_array.push(redaction_options1);
redaction_option_array.push(redaction_options2);
redaction_option_array.push(redaction_options3);
data.append('redactions', JSON.stringify(redaction_option_array));
data.append('output', 'pdfrest_pdf_with_redacted_text_preview');

// define configuration options for axios request
var config = {
  method: 'post',
  maxBodyLength: Infinity, // set maximum length of the request body
  url: 'https://api.pdfrest.com/pdf-with-redacted-text-preview', 
  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 Code

The code begins by requiring necessary modules: axios for making HTTP requests, form-data for handling form data, and fs for file system operations.

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

Here, a new FormData instance is created, and a PDF file is appended to it. The file path should be replaced with the actual path of the PDF you wish to redact.

var redaction_options1 = {
    "type": "preset",
    "value": "email",
};

This snippet defines a redaction option to remove email addresses using a preset type. Other options include regex for regular expressions and literal for specific words.

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

The configuration for the axios request is set here, including the HTTP method, URL, headers (with an API key), and the form data to be sent.

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

This section sends the request and handles the response or any errors that may occur. The response data is logged to the console.

Beyond the Tutorial

In this tutorial, you learned how to make an API call to redact text from a PDF using JavaScript and the pdfRest API. You can explore more API tools in the API Lab. For further details, refer to the API Reference Guide.

Note: This example demonstrates a multipart API call. For JSON payload samples, visit GitHub.

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