How to Extract Images from PDF Files with JavaScript in NodeJS
Why Extract PDF Images with JavaScript?
The pdfRest Extract Images API Tool is a powerful utility that allows developers to programmatically extract images from PDF files. This tutorial will guide you through the process of sending an API call to extract images using JavaScript, providing a practical example of how to implement this functionality in your projects.
Extracting images from PDFs can be incredibly useful. For instance, a graphic designer might need to extract high-quality images from a portfolio PDF to use in a new project. Similarly, a researcher might need to extract charts or diagrams from academic papers for analysis. This tool simplifies the extraction process, making it accessible and efficient.
Extract PDF Images with JavaScript Code Example
// This request demonstrates how to reduce the file size of a PDF. 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('pages', '1-last'); data.append('output', 'pdfrest_extracted_images'); // define configuration options for axios request var config = { method: 'post', maxBodyLength: Infinity, // set maximum length of the request body url: 'https://api.pdfrest.com/extracted-images', 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 necessary modules: axios
for making HTTP requests, form-data
for handling form submissions, and fs
for file system operations. These modules are essential for setting up the API request.
var data = new FormData(); data.append('file', fs.createReadStream('/path/to/file')); data.append('pages', '1-last'); data.append('output', 'pdfrest_extracted_images');
Here, a new FormData
instance is created. The PDF file is appended using fs.createReadStream('/path/to/file')
, which reads the file from the specified path. The pages
parameter is set to '1-last'
, indicating that images should be extracted from all pages. The output
parameter specifies the desired name for the extracted images.
var config = { method: 'post', maxBodyLength: Infinity, url: 'https://api.pdfrest.com/extracted-images', headers: { 'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', ...data.getHeaders() }, data : data };
The configuration object for the axios
request is defined here. The method
is set to 'post'
, and the url
is the endpoint for the Extract Images API. The headers
include an API key, which must be replaced with your own, and the headers from the FormData
instance. The data
field contains 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
request is executed, and the response is handled. If successful, the response data is logged in JSON format. If an error occurs, it is logged to the console.
Beyond the Tutorial
In this tutorial, you learned how to use JavaScript to call the pdfRest Extract Images API, extracting images from a PDF file efficiently. This example demonstrates how to handle multipart API calls using JavaScript.
To further explore the capabilities of pdfRest, you can demo all the API Tools in the API Lab. For more detailed information, refer to 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.