How to Convert PNG to PDF with JavaScript in NodeJS

Learn how to turn PNG images into PDF documents by calling the Convert to PDF API Tool from pdfRest.
Share this page

Why Use Convert to PDF with JavaScript?

The pdfRest Convert to PDF API Tool is a powerful resource for developers looking to automate the conversion of various file formats into PDFs. This API allows for seamless integration into applications, enabling users to convert documents without the need for complex software.

A real-world example of why a user might use Convert to PDF is to convert user-uploaded images into a standardized PDF format for easier distribution, archiving, viewing, or printing.

Convert 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.

Source code reference: GitHub

Breaking Down the Code

This code snippet demonstrates how to use the pdfRest Convert to PDF API with JavaScript:

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

These lines import the required modules: axios for making HTTP requests, FormData for constructing form data payloads, and fs for file system operations.

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

Here, a new FormData instance is created, and the file to be converted is appended to it. Replace '/path/to/file' with the actual file path.

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

Additional parameters 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
};

This config object includes the HTTP method, URL, headers (including the API key), and the data payload. Replace 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' with your actual API key.

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, or an error is logged if the request fails.

Beyond the Tutorial

By following this tutorial, you have learned how to send an API call to the pdfRest Convert to PDF API using JavaScript. This enables you to integrate PDF conversion capabilities into your applications. To explore and demo all of the pdfRest API Tools, visit the API Lab at https://pdfrest.com/apilab/, and for more detailed information, refer to the API Reference documentation at https://pdfrest.com/documentation/.

Note: This is an example of a multipart API call. Code samples using JSON payloads 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