Extract Images from PDF in Node.js
Contents
[
Hide
]
Extract images from PDF files in the Node.js environment
In case you want to extract images from PDF document, you can use AsposePdfExtractImage function. We must pass three arguments to this function: input and output file name and resolution. Please check the following code snippet to extract images from a PDF file using Node.js.
CommonJS:
- Call
require
and importasposepdfnodejs
module asAsposePdf
variable. - Specify the name for the PDF file from which the image will be extracted.
- Call
AsposePdf
as Promise and perform the operation for extracting image. Receive the object if successful. - Call the function AsposePdfExtractImage.
- Extract images from the PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfExtractImage{0:D2}.jpg”. Where {0:D2} represents the page number with a two-digit format. The images are saved with a resolution of 150 DPI. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.
const AsposePdf = require('asposepdfnodejs');
const pdf_file = 'Aspose.pdf';
AsposePdf().then(AsposePdfModule => {
/*Extract image from a PDF-file with template "ResultPdfExtractImage{0:D2}.jpg" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
const json = AsposePdfModule.AsposePdfExtractImage(pdf_file, "ResultPdfExtractImage{0:D2}.jpg", 150);
console.log("AsposePdfExtractImage => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);
});
ECMAScript/ES6:
- Import the
asposepdfnodejs
module. - Specify the name for the PDF file from which the image will be extracted.
- Initialize the AsposePdf module. Receive the object if successful.
- Call the function AsposePdfExtractImage.
- Extract images from the PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfExtractImage{0:D2}.jpg”. Where {0:D2} represents the page number with a two-digit format. The images are saved with a resolution of 150 DPI. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.
import AsposePdf from 'asposepdfnodejs';
const AsposePdfModule = await AsposePdf();
const pdf_file = 'Aspose.pdf';
/*Extract image from a PDF-file with template "ResultPdfExtractImage{0:D2}.jpg" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
const json = AsposePdfModule.AsposePdfExtractImage(pdf_file, "ResultPdfExtractImage{0:D2}.jpg", 150);
console.log("AsposePdfExtractImage => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);