How to Translate PDF Text with JavaScript in NodeJS

Learn how to use the pdfRest Translate PDF API with JavaScript to translate PDF text to a different language
Share this page

Why Translate PDF Text with JavaScript?

The pdfRest Translate PDF API Tool is a powerful resource that allows developers to translate text within PDF documents into different languages using JavaScript. This tutorial will guide you through the process of sending an API call to the Translate PDF endpoint using JavaScript, making it easier to integrate translation capabilities into your applications.

Imagine a scenario where a multinational corporation needs to distribute product manuals in various languages to their global customer base. By using the Translate PDF API, they can automate the translation of these manuals directly from PDF files, ensuring consistency and accuracy while saving time and resources.

Translate PDF Text with JavaScript Code Example

// This request demonstrates how to translate 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"));
// Translates text to American English. Format the output_language as a 2-3 character ISO 639 code, optionally with a region/script (e.g., 'en', 'es', 'zh-Hant', 'eng-US').
data.append("output_language", "en-US");

// define configuration options for axios request
var config = {
  method: "post",
  maxBodyLength: Infinity,
  url: apiUrl + "/translated-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 importing necessary modules: axios for making HTTP requests, form-data for handling form submissions, and fs for file system operations.

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

The apiUrl variable is set to the US-based API endpoint by default. An alternative EU-based endpoint is provided for GDPR compliance, which can be used by uncommenting the respective line.

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

A new FormData instance is created, and the PDF file is appended using fs.createReadStream. The output_language parameter specifies the target language for translation, formatted as a 2-3 character ISO 639 code.

var data = new FormData();
data.append("file", fs.createReadStream("/path/to/file"));
data.append("output_language", "en-US");

The config object defines the request parameters for axios, including the HTTP method, URL, headers (with API key), and the form data.

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

The axios request is executed, and the response or error is logged to the console.

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 use JavaScript to send an API call to the pdfRest Translate PDF endpoint, enabling you to translate text within PDF documents. This capability can be integrated into various applications to enhance multilingual support.

To explore more, try out all the pdfRest API Tools in the API Lab. For further information, refer to the API Reference Guide. Note that this example demonstrates a multipart API call; for JSON payload examples, visit the GitHub Repository.

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