How to Convert Microsoft Word to PDF with JavaScript in NodeJS

Learn how to use pdfRest Convert to PDF with JavaScript to convert Word docs to PDF files
Share this page

Why Convert Word to PDF with JavaScript?

The pdfRest Convert to PDF API Tool provides a powerful and flexible way to convert various document formats into PDFs programmatically. This tutorial will guide you through the process of sending an API call to Convert to PDF using JavaScript. By leveraging this tool, developers can integrate PDF conversion features directly into their web applications or services, enhancing the functionality and user experience.

Imagine a scenario where a business needs to convert batches of invoices, reports, or contracts from their native formats into a standardized PDF format for archival, sharing, or printing purposes. By using JavaScript to call the Convert to PDF API, developers can build systems that handle these conversions on-the-fly, without the need for manual intervention, thus streamlining business processes and increasing productivity.

Convert Word to PDF with JavaScript Code Example

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.docx'));
data.append('compression', 'lossy'); 
data.append('downsample', '300'); 
data.append('tagged_pdf', 'off'); 
data.append('output', 'pdfrest_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/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 of the provided code: pdfRest API Samples

Breaking Down the Code

The provided code snippet is a JavaScript example for making an API call to the pdfRest Convert to PDF endpoint using the Axios library and FormData to handle multipart form data.

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

This section imports the required modules: Axios for HTTP requests, FormData for constructing multipart/form-data payloads, and fs for file system operations.

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

Here, a new FormData instance is created, and the file to be converted is appended to the form data. The file path should be replaced with the actual path to the file you wish to convert.

data.append('compression', 'lossy');
data.append('downsample', '300');
data.append('tagged_pdf', 'off');
data.append('output', 'pdfrest_pdf');

Additional parameters for the conversion process are appended to the form data. These include compression settings, downsampling resolution, whether to create a tagged PDF, and the output format.

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

The Axios request configuration is defined, including the request method, URL, headers (including the API key which must be replaced with your own), and the data payload.

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

The Axios library is used to send the request. The response is logged to the console if successful, or an error is logged if the request fails.

Beyond the Tutorial

This tutorial walked you through the process of making a multipart API call to the pdfRest Convert to PDF endpoint using JavaScript. By understanding how to construct and send such requests, you can now integrate PDF conversion functionality into your applications.

To explore and demo all of the pdfRest API Tools, visit the API Lab. For more detailed information on the API, 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 pdfRest API Samples.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us