How to Extract Images from PDF Files with JavaScript in NodeJS
Extract Embedded PDF Images with Node.js
A PDF can contain photographs, logos, diagrams, screenshots, and other image assets that need to be reused or analyzed separately from the document. Rendering each PDF page as a new image is not the same operation: page rendering creates a picture of the entire page, while the pdfRest Extract Images API Tool retrieves the images embedded inside the PDF without altering the source document.
The /extracted-images endpoint preserves the native image format and original image properties whenever possible, including common JPEG, PNG, and TIFF assets. This avoids an unnecessary conversion step and helps retain resolution, color accuracy, and compression properties for digital asset management, content repurposing, publishing, or image-analysis workflows.
For example, a Node.js product-ingestion service can receive a supplier's PDF catalog, extract the embedded product photographs, and associate the returned files with catalog records. The team avoids manually opening every catalog and exporting each image, while the original PDF remains available as the source document.
This Node.js example streams a local PDF into a multipart request with Axios and FormData, processes all pages, and prints the JSON response.
JavaScript Extract Images Code Example
Install axios and form-data, then replace the PDF path and API-key placeholders. The built-in fs module supplies a readable stream, so the application does not have to load the complete PDF into memory before sending it.
// This request demonstrates how to extract embedded images from a PDF.
var axios = require('axios');
var FormData = require('form-data');
var fs = require('fs');
// 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 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: apiUrl + '/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: pdfRest Extract Images multipart JavaScript sample
If the source PDF already has a pdfRest resource ID, send a JSON payload instead of uploading it again as multipart data. See the Extract Images JSON-payload sample for JavaScript.
Select the PDF Pages to Process
The pages field determines where pdfRest looks for embedded images. The sample uses 1-last to process the entire PDF. Applications can target only the relevant pages with individual numbers, ranges, or a combination such as 1,2,5-10,12-last. Limiting the request is useful when a long document contains appendices or other sections that do not need image extraction.
The optional output field supplies the base name for the extracted files. pdfRest appends image and page numbers so each result remains distinct. For example, an output value of test produces names such as test-img001-page002. Including both values makes it easier to connect each extracted asset with the page where it first appeared.
Work with Multiple Output Files
One PDF can produce many extracted images, so the success response uses output collections rather than a single file value. Inspect the returned JSON and iterate over the available output URLs or resource IDs. Resource IDs can be passed to other pdfRest tools without downloading and uploading each intermediate file, while URLs can be used when the application needs to retrieve and store the images elsewhere.
A PDF may also contain no embedded images. In that case, the endpoint can return 200 OK with a warning and empty outputUrl and outputId arrays. Treat that as a valid no-results outcome rather than assuming every successful HTTP response contains at least one file.
Make the Axios Request Production-Ready
data.getHeaders() supplies the correct multipart Content-Type value and boundary. The sample adds the API key and sets maxBodyLength to Infinity so Axios does not reject the upload based on its client-side body limit. Keep the API key in an environment variable or secret manager rather than source code.
The example logs either the response or the error. In an application, set an appropriate timeout, distinguish HTTP errors from connection failures, and avoid logging document content or credentials. Validate file type and size before submission, and confirm that each expected asset was returned before advancing the workflow.
Use the configurable base URL to select the US or EU Cloud API. You can try different page ranges in API Lab and review the current parameters and response schema in the Extract Images API reference.