How to Convert JPG to PDF with JavaScript in NodeJS

Learn how to convert JPG images to PDF with pdfRest Convert to PDF API Tool using JavaScript
Share this page

Why Convert JPG to PDF with JavaScript?

The pdfRest Convert to PDF API Tool is a powerful resource for developers who need to programmatically convert documents into PDF format. This tool can take various file types and convert them to a standardized PDF, which is essential for consistent document presentation, archiving, and sharing. This tutorial will demonstrate how to send an API call to Convert JPG to PDF using JavaScript, leveraging the capabilities of pdfRest to streamline document processing workflows.

A user might need to convert batches of JPGs, such as scans of invoices, reports, or contracts, into PDFs for electronic distribution. For instance, a web application could allow users to upload documents in different formats and automatically convert them to PDFs before sending them to clients or storing them in a digital archive. This ensures that the documents are easily accessible and viewable across different platforms and devices.

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

This code is sourced from the pdfRest API samples repository on GitHub.

Breaking Down the Code

The provided JavaScript code uses the Axios library to make an HTTP POST request to the pdfRest Convert to PDF API endpoint. The FormData object is utilized to construct the multipart/form-data payload required for file uploads.

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

This snippet creates a new FormData instance and appends a JPG file to it. The 'file' field should contain the path to the document you wish to convert.

data.append('output', 'pdfrest_pdf');

The 'output' parameter defines the prefix of the output file name, without extension.

var config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://api.pdfrest.com/pdf', 
  headers: { 
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
    ...data.getHeaders()
  },
  data : data
};

The 'config' object contains the request configuration, including the method, URL, headers, and data. The 'Api-Key' header must be replaced with your actual API key from pdfRest.

Beyond the Tutorial

By following the steps above, you have learned how to make a multipart API call to convert JPG images to PDF using JavaScript and the pdfRest API. This capability can be integrated into various applications, enabling automated document conversions within your workflows.

To explore more possibilities and try out all of the pdfRest API Tools, visit the API Lab. For comprehensive documentation and additional details on the API, refer to the API Reference Guide.

Note that this is an example of a multipart API call. For code samples using JSON payloads, you can find them at the pdfRest API samples repository on 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