How to Create a Blank PDF with JavaScript in NodeJS

Learn how to generate an empty PDF that can be filled with content using the pdfRest Create Blank PDF API with JavaScript.
Share this page

Why Create Blank PDF with JavaScript?

The pdfRest Create Blank PDF API Tool allows developers to generate blank PDF documents programmatically. This tutorial will guide you through the process of sending an API call to create a blank PDF using JavaScript. By leveraging this API, you can automate the creation of PDF documents with specified parameters such as page size, count, and orientation, making it a versatile tool for various applications.

A real-world example of using the Create Blank PDF API might involve a document management system where users need to generate standardized forms or templates for further data entry. For instance, a company might use this API to create blank invoices or reports that are later filled with specific data, ensuring consistency and efficiency in document creation.

Create Blank PDF with JavaScript Code Example

// This request demonstrates how to generate a blank PDF.
var axios = require('axios');
var FormData = require('form-data');

// 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 required parameters to it
var data = new FormData();
data.append('page_size', 'letter');
data.append('page_count', '3');
data.append('page_orientation', 'portrait');

// define configuration options for axios request
var config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: apiUrl + '/blank-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: GitHub Repository

Breaking Down the Code

The code begins by importing the necessary modules, axios and form-data, which are essential for making HTTP requests and handling form data, respectively.

var apiUrl = "https://api.pdfrest.com";

This line sets the API endpoint to the US-based service. For European users, the EU-based endpoint can be used by uncommenting the alternative URL.

var data = new FormData();
data.append('page_size', 'letter');
data.append('page_count', '3');
data.append('page_orientation', 'portrait');

A FormData instance is created to hold the parameters for the API request. The parameters include page_size (set to 'letter'), page_count (set to '3'), and page_orientation (set to 'portrait'). These parameters define the characteristics of the blank PDF to be created.

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

The config object defines the request configuration, including the HTTP method ('post'), the URL for the API call, and the headers. The 'Api-Key' must be replaced with your actual API key. The data property holds the form data to be sent.

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

The axios function sends the request using the defined configuration. The response is logged to the console if successful, or an error is logged if the request fails.

Beyond the Tutorial

In this tutorial, you learned how to make an API call to create a blank PDF using JavaScript and the pdfRest API. This example demonstrates a multipart API call, which is useful for sending form data. To explore more, you can demo all of the pdfRest API Tools in the API Lab. For further details, refer to the API Reference Guide.

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

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.