How to Convert PDF to JPG with JavaScript in NodeJS

Learn how to convert PDF documents to JPG image files using pdfRest PDF to Images API Tool with JavaScript
Share this page

Why Convert PDF to JPG with JavaScript?

The pdfRest PDF to Images API Tool is a powerful resource for developers who need to convert PDF documents into image files programmatically. This tutorial will guide you through the process of sending an API call to the PDF to Images tool using JavaScript.

This functionality is particularly useful in scenarios such as creating thumbnails for document previews, processing documents for web publication, or extracting visual content from PDFs for further use.

Convert PDF to JPG with JavaScript Code Example

// This request demonstrates how to convert a PDF into JPG image files, one per PDF page.
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');

// Create a new form data instance and append the file and parameters to it
var data = new FormData();
data.append('file', fs.createReadStream('/path/to/file')); 
data.append('pages', '1-last');
data.append('resolution', '300');
data.append('color_model', 'rgb');
data.append('jpeg_quality', '75');
data.append('output', 'pdfrest_jpg');

// define configuration options for axios request
var config = {
  method: 'post',
  maxBodyLength: Infinity, // set maximum length of the request body
  url: 'https://api.pdfrest.com/jpg', 
  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 code reference: pdfRest API Samples on GitHub

Breaking Down the Code

The provided code is a JavaScript example that uses the Axios library to send a multipart/form-data POST request to the pdfRest API to convert a PDF file into JPG images.

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

These lines import the required modules: Axios for HTTP requests, FormData to construct the multipart/form-data payload, and fs to interact with the file system.

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

This snippet creates a new FormData instance and appends the file and various parameters such as 'pages', 'resolution', 'color_model', 'jpeg_quality', and 'output'.

var config = {
  ...
  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
};

The configuration object for Axios includes the API endpoint URL, the method (POST), headers including the API key, and the FormData instance as the request body.

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

Finally, the Axios instance sends the request and handles the response or error. If successful, the response data is logged to the console.

Beyond the Tutorial

By following this tutorial, you've learned how to convert a PDF to JPG images using the pdfRest API with JavaScript. You can now integrate this functionality into your own applications and automate document processing tasks.

For further exploration and to demo all of the pdfRest API Tools, visit the API Lab and the API Reference documentation.

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

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