How to Convert BMP to PDF with JavaScript in NodeJS

Learn how to use JavaScript in NodeJS to turn BMP images into PDF documents by calling the Convert to PDF API Tool from pdfRest.
Share this page

Why Convert BMP to PDF with JavaScript?

The pdfRest Convert to PDF API Tool is a powerful resource for developers who need to convert various document formats into PDFs programmatically. This API provides a seamless way to integrate PDF conversion into web applications or services, enabling automated document workflows.

This tutorial will guide you through the process of sending an API call to Convert to PDF using JavaScript, which is a popular language for web development and can be easily included in Node.js scripts or server-side applications.

In the real world, a user might need to convert documents to PDF for consistent viewing across different platforms and devices, ensuring that the formatting remains intact. For instance, a business might need to convert invoices, reports, or contracts into PDFs before sending them to clients. By automating this process with JavaScript, the business can save time and reduce the potential for human error, while maintaining a professional standard for their documents.

Convert BMP 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'));
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.

Reference to the code: GitHub Repository

Breaking Down the Code

The code above demonstrates how to create a multipart request to the pdfRest API for converting a document to PDF. Let's break it down:

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

This snippet initializes a new FormData object and appends a file to it for conversion. The 'fs.createReadStream' function reads the file from the specified path, which should be replaced with the actual file path.

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

These lines add additional parameters to the form data:

  • compression: The type of compression to be applied to the PDF. 'lossy' allows for smaller file sizes at the cost of some quality.
  • downsample: The resolution to which images should be downsampled, specified in dots per inch (DPI).
  • tagged_pdf: Whether to create a tagged PDF ('on' or 'off'). Tagged PDFs are useful for accessibility purposes.
  • output: The name of the output parameter, which in this case is set to 'pdfrest_pdf'.
var config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://api.pdfrest.com/pdf', 
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    ...data.getHeaders()
  },
  data : data
};

This configuration object includes the request method, URL, headers (including the API key which you must replace with your own), and the data payload. The 'maxBodyLength' is set to Infinity to allow for large files.

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. Upon success, the response data is logged to the console. In case of an error, it is also logged to the console.

Beyond the Tutorial

By following the steps in this tutorial, you've learned how to convert documents to PDF using the pdfRest API and JavaScript. This can be integrated into your applications to automate the process of document conversion and streamline your document management workflows.

Feel free to demo all of the pdfRest API Tools in the API Lab. For more detailed information, check out the API Reference Guide.

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

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