How to Convert PDF to BMP with JavaScript in NodeJS

Learn how to convert PDF pages to BMP image files using JavaScript in NodeJS with pdfRest PDF to Images API Tool
Share this page

Why Use PDF to Images with JavaScript?

The pdfRest PDF to Images API Tool allows developers to convert PDF documents into image files programmatically. This tutorial will demonstrate how to make an API call to the PDF to Images tool using JavaScript.

As an example, a web application might need to display thumbnails of PDF pages to users without requiring them to download or open the entire document. By converting PDF pages to images, the application can quickly render previews for a smoother user experience.

PDF to Images with JavaScript Code Example

// This request demonstrates how to convert a PDF into BMP 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('output', 'pdfrest_bmp'); 

// define configuration options for axios request
var config = {
  method: 'post',
  maxBodyLength: Infinity, // set maximum length of the request body
  url: 'https://api.pdfrest.com/bmp', 
  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); 
});

Source code reference: GitHub

Breaking Down the Code

The code above performs a multipart/form-data POST request to the pdfRest API to convert a PDF to BMP images. Let's break down the key components:

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

This snippet creates a new FormData object and appends the PDF file to be converted. The file is read from the specified path using Node.js's fs module.

data.append('pages', '1-last');
data.append('resolution', '300');
data.append('color_model', 'rgb');
data.append('output', 'pdfrest_bmp');

These lines append additional parameters to the request:

  • pages: Specifies the range of pages to convert. '1-last' means all pages.
  • resolution: Sets the resolution of the output images in DPI (dots per inch).
  • color_model: Determines the color model of the output images. 'rgb' is for full color.
  • output: The prefix for the output image files.
var config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://api.pdfrest.com/bmp',
  headers: {
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    ...data.getHeaders()
  },
  data : data
};

This configuration object sets up the request details for axios, including the API endpoint URL, request headers, and the data payload. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key.

Beyond the Tutorial

In this tutorial, we've learned how to use JavaScript to call the pdfRest API to convert a PDF document into BMP images. By sending the request and handling the response, we can integrate this functionality into our applications. To explore all of the pdfRest API Tools, you can demo them in the API Lab. For more detailed information, refer to the API Reference documentation.

Note that this is an example of a multipart API call. Code samples using JSON payloads for other endpoints can be found at GitHub.

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