Extract Images from PDF in Node.js

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:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name for the PDF file from which the image will be extracted.
  3. Call AsposePdf as Promise and perform the operation for extracting image. Receive the object if successful.
  4. Call the function AsposePdfExtractImage.
  5. 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:

  1. Import the asposepdfnodejs module.
  2. Specify the name for the PDF file from which the image will be extracted.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfExtractImage.
  5. 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);