How to Convert PDF to BMP with JavaScript in NodeJS
Render PDF Pages as BMP Images with Node.js
Some imaging systems, desktop workflows, and legacy integrations require BMP files rather than PDF pages. The pdfRest PDF to Images API Tool renders selected PDF pages through the /bmp endpoint, producing one bitmap image for each requested page while leaving the source PDF unchanged.
BMP is useful when a downstream system explicitly expects a straightforward bitmap format, but the files can be much larger than compressed JPEG or PNG output. For browser previews, email, or general web delivery, another supported image format may be more efficient. Choosing BMP should reflect the receiving system's requirement rather than an assumption that all raster formats behave the same way.
For example, a Node.js document-inspection service may need to send a specific PDF page to an established Windows imaging component that accepts BMP input. The service can render only the relevant page, store the returned image in the component's work queue, and avoid installing a local PDF renderer in every application environment.
This example uses Axios, FormData, and a file stream to upload the PDF, select all pages, and request RGB BMP output at 300 DPI.
JavaScript PDF-to-BMP Code Example
Install axios and form-data, then replace the API-key and source-path placeholders. The current sample keeps the US and EU API base URLs configurable so deployment can select the appropriate processing region.
// 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');
// 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 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: apiUrl + '/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);
});
// If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.js' sample.
Source: pdfRest PDF-to-BMP multipart JavaScript sample
Control Pages, Resolution, and Color
The pages field accepts individual page numbers and ranges. Use 1-last to render the entire PDF, or narrow the request to values such as 1, 2-4, or 1,5,8-last. Rendering only the pages the application needs reduces processing and avoids generating unused files.
The resolution field controls raster detail in dots per inch. Higher values produce more pixels and larger outputs. pdfRest supports a broad range of resolutions, but the best value depends on the downstream task. A screen-only integration may need less detail than print inspection, image analysis, or OCR. Test actual documents instead of selecting the highest available value by default.
The color_model field can produce RGB, RGBA, CMYK, or grayscale output where supported. Match the selection to the receiving system and validate how transparency or page backgrounds should be represented in BMP.
Handle One Output per Page
A multipage PDF produces multiple images. Parse the response collections and associate each output with its source page rather than assuming the endpoint returns one file. Output URLs support downloading the BMPs, while resource IDs can be used by another compatible pdfRest operation. If the workflow needs a single package, the generated resources can be passed to the Zip Files API Tool.
Production code should check the HTTP status, validate the returned JSON, apply suitable timeouts, and distinguish API errors from network failures. Keep the API key in an environment variable or secret manager and avoid logging document data. Validate image dimensions and output count before handing files to the downstream system.
If the PDF has already been uploaded, submit its resource ID in JSON rather than uploading the file again. The official PDF-to-BMP JSON-payload JavaScript sample demonstrates this request. Use API Lab to compare settings and consult the PDF to Images API reference for current page, resolution, and color options.