How to Rasterize PDF Files with JavaScript in NodeJS
Why Rasterize PDF Files with JavaScript?
The pdfRest Rasterize PDF API Tool offers a powerful way to convert PDF files into rasterized images using JavaScript. This tutorial will guide you through the process of sending an API call to the Rasterize PDF endpoint using JavaScript. By understanding this process, developers can integrate PDF rasterization into their applications, enabling the conversion of vector-based PDFs into pixel-based images for various use cases.
Rasterizing a PDF can be useful for ensuring consistent rendering across different devices and platforms. For instance, a graphic designer might need to convert a PDF into a raster image format to ensure that complex vector graphics are displayed correctly on web pages or mobile apps, where vector rendering might not be supported or could lead to performance issues.
Rasterize PDF with JavaScript Code Example
// This request demonstrates how to rasterize a PDF file. var axios = require("axios"); var FormData = require("form-data"); var fs = require("fs"); // 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("output", "pdfrest_rasterized"); // define configuration options for axios request var config = { method: "post", maxBodyLength: Infinity, // set maximum length of the request body url: "https://api.pdfrest.com/rasterized-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
Breaking Down the Code
The code begins by requiring necessary modules: axios
for making HTTP requests, form-data
for handling form submissions, and fs
for file system operations.
var axios = require("axios"); var FormData = require("form-data"); var fs = require("fs");
Next, a new instance of FormData
is created, and the PDF file to be rasterized is appended along with the output parameter, which specifies the desired output format.
var data = new FormData(); data.append("file", fs.createReadStream("/path/to/file")); data.append("output", "pdfrest_rasterized");
The configuration for the axios request is then defined. The method
is set to "post", and the url
is the endpoint for the Rasterize PDF API. The headers
include the API key for authentication and any additional headers generated by the FormData
instance.
var config = { method: "post", maxBodyLength: Infinity, url: "https://api.pdfrest.com/rasterized-pdf", headers: { "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", ...data.getHeaders(), }, data: data, };
The request is then sent using axios
, and the response is logged to the console. If an error occurs, it is caught and logged as well.
axios(config) .then(function (response) { console.log(JSON.stringify(response.data)); }) .catch(function (error) { console.log(error); });
Beyond the Tutorial
This tutorial demonstrated how to use JavaScript to send an API call to the pdfRest Rasterize PDF endpoint, allowing you to flatten each page of a PDF into a rasterized image. By following these steps, you can integrate similar functionality into your own applications.
For further exploration, you are encouraged to try out all the pdfRest API Tools in the API Lab. Additionally, the API Reference Guide provides comprehensive documentation on all available endpoints and parameters.
Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub.