How to Summarize PDF Text with JavaScript in NodeJS

Learn how to use pdfRest Summarize PDF API with JavaScript to summarize PDF text content.
Share this page

Why Summarize PDF with JavaScript?

The pdfRest Summarize PDF API Tool is a powerful utility that allows developers to extract and condense the text from PDF documents using JavaScript. This tutorial will guide you through the process of sending an API call to the Summarize PDF endpoint using JavaScript. By leveraging this tool, you can efficiently handle PDF documents and extract meaningful summaries, which can be particularly useful for applications that require text analysis or content management.

A user might utilize the Summarize PDF tool to quickly extract key points from lengthy documents, such as research papers, legal contracts, or business reports. For instance, a legal professional could use this tool to summarize lengthy legal documents, making it easier to identify crucial information without manually sifting through pages of text.

Summarize PDF with JavaScript Code Example

// This request demonstrates how to summarize text from a PDF document.
var axios = require("axios");
var FormData = require("form-data");
var fs = require("fs");

// By default, we use the US-based API service. This is the primary endpoint for global use.
var apiUrl = "https://api.pdfrest.com";

/* For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
 * For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
 */
//var apiUrl = "https://eu-api.pdfrest.com";

// 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("target_word_count", "100");

// define configuration options for axios request
var config = {
  method: "post",
  maxBodyLength: Infinity,
  url: apiUrl + "/summarized-pdf-text",
  headers: {
    "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key
    ...data.getHeaders(),
  },
  data: data,
};

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

Source: GitHub Repository

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 axios = require("axios");
var FormData = require("form-data");
var fs = require("fs");

The apiUrl variable defines the endpoint URL for the API call. By default, it uses the US-based service, but an EU-based option is available for GDPR compliance.

var apiUrl = "https://api.pdfrest.com";

A FormData instance is created to append the PDF file and parameters. The file path is specified, and the target_word_count parameter sets the desired word count for the summary.

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

The config object defines the request's configuration, including the HTTP method, URL, headers, and data. The Api-Key must be replaced with a valid API key.

var config = {
  method: "post",
  maxBodyLength: Infinity,
  url: apiUrl + "/summarized-pdf-text",
  headers: {
    "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // Replace with your API key
    ...data.getHeaders(),
  },
  data: data,
};

The axios function sends the request, handling the response or any errors encountered during the process.

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

Beyond the Tutorial

In this tutorial, you learned how to make an API call to the pdfRest Summarize PDF endpoint using JavaScript. This allows you to extract concise summaries from PDF documents efficiently. To explore more functionalities, you can demo all of the pdfRest API Tools in the API Lab. For more detailed information, refer to the API Reference Guide.

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

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