How to Convert PDF to Word with JavaScript in NodeJS
Why Convert PDF to Word with JavaScript?
The pdfRest PDF to Word API Tool is a powerful resource that allows users to convert PDF documents to Word format. This tutorial will guide you through the process of making an API call to the PDF to Word endpoint using JavaScript.
This capability is particularly useful in scenarios where one needs to edit the contents of a PDF document in a word processor, or when extracting text from PDFs for data processing tasks.
Convert PDF to Word JavaScript Code Example
// This request demonstrates how to convert a PDF to a Word file. 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('output', 'pdfrest_word_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/word', 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: pdf-rest-api-samples GitHub repository
Breaking Down the Code
The code starts by requiring necessary modules:
var axios = require('axios'); var FormData = require('form-data'); var fs = require('fs');
axios
is used for making HTTP requests, FormData
is for building form data payloads, and fs
is for file system operations.
Next, a FormData
object is created and the PDF file is appended to it along with the output parameter:
var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file')); data.append('output', 'pdfrest_word_pdf');
The file
field contains the file stream of the PDF, and output
is the desired name for the converted Word file.
The configuration object for the axios request is defined:
var config = { method: 'post', maxBodyLength: Infinity, url: 'https://api.pdfrest.com/word', headers: { 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', ...data.getHeaders() }, data : data };
The method
is set to POST, and maxBodyLength
is set to Infinity to allow for large file sizes. The url
points to the API endpoint. The headers
include the API key and the headers from the form data. The data
property holds the form data object.
Finally, the request is sent, and the response or error is handled:
axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
Beyond the Tutorial
In this tutorial, we have demonstrated how to make an API call to the pdfRest PDF to Word endpoint using JavaScript. By following the steps outlined, you can convert PDF documents to Word format programmatically. To explore more 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 pdf-rest-api-samples GitHub repository.