Convert PDF to Image Formats in Node.js

Node.js Convert PDF to Image

In this article, we will show you the options for converting PDF to image formats.

Previously scanned documents are often saved in the PDF file format. However, do you need to edit it in a graphic editor or send it further in image format? We have a universal tool for you to convert PDF to images using The most common task is when you need to save an entire PDF document or some specific pages of a document as a set of images. Aspose for Node.js via C++ allows you to convert PDF to JPG and PNG formats to simplify the steps required to get your images from a specific PDF file.

Aspose.PDF for Node.js via C++ supports various PDF to image formats conversion. Please checks the section Aspose.PDF Supported File Formats.

Convert PDF to JPEG

In case you want to convert PDF document, you can use AsposePdfPagesToJpg function. Please check the following code snippet in order to convert in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file that will be converted.
  3. Call AsposePdf as Promise and perform the operation for converting file. Receive the object if successful.
  4. Call the function AsposePdfPagesToJpg.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToJpg{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 => {
      /*Convert a PDF-file to JPG with template "ResultPdfToJpg{0:D2}.jpg" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
      const json = AsposePdfModule.AsposePdfPagesToJpg(pdf_file, "ResultPdfToJpg{0:D2}.jpg", 150);
      console.log("AsposePdfPagesToJpg => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file that will be converted
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfPagesToJpg.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToJpg{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';
  /*Convert a PDF-file to JPG with template "ResultPdfToJpg{0:D2}.jpg" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
  const json = AsposePdfModule.AsposePdfPagesToJpg(pdf_file, "ResultPdfToJpg{0:D2}.jpg", 150);
  console.log("AsposePdfPagesToJpg => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);

Convert PDF to TIFF

In case you want to convert PDF document, you can use AsposePdfPagesToTiff function. Please check the following code snippet in order to convert in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file that will be converted.
  3. Call AsposePdf as Promise and perform the operation for converting file. Receive the object if successful.
  4. Call the function AsposePdfPagesToTiff.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToTiff{0:D2}.tiff”. 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 => {
      /*Convert a PDF-file to TIFF with template "ResultPdfToTiff{0:D2}.tiff" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
      const json = AsposePdfModule.AsposePdfPagesToTiff(pdf_file, "ResultPdfToTiff{0:D2}.tiff", 150);
      console.log("AsposePdfPagesToTiff => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file that will be converted
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfPagesToTiff.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToTiff{0:D2}.tiff”. 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';
  /*Convert a PDF-file to TIFF with template "ResultPdfToTiff{0:D2}.tiff" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
  const json = AsposePdfModule.AsposePdfPagesToTiff(pdf_file, "ResultPdfToTiff{0:D2}.tiff", 150);
  console.log("AsposePdfPagesToTiff => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);

Convert PDF to PNG

In case you want to convert PDF document, you can use AsposePdfPagesToPng function. Please check the following code snippet in order to convert in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file that will be converted.
  3. Call AsposePdf as Promise and perform the operation for converting file. Receive the object if successful.
  4. Call the function AsposePdfPagesToPng.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToPng{0:D2}.png”. 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 => {
      /*Convert a PDF-file to PNG with template "ResultPdfToPng{0:D2}.png" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
      const json = AsposePdfModule.AsposePdfPagesToPng(pdf_file, "ResultPdfToPng{0:D2}.png", 150);
      console.log("AsposePdfPagesToPng => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file that will be converted
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfPagesToPng.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToPng{0:D2}.png”. 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';
  /*Convert a PDF-file to PNG with template "ResultPdfToPng{0:D2}.png" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
  const json = AsposePdfModule.AsposePdfPagesToPng(pdf_file, "ResultPdfToPng{0:D2}.png", 150);
  console.log("AsposePdfPagesToPng => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);

Scalable Vector Graphics (SVG) is a family of specifications of an XML-based file format for two-dimensional vector graphics, both static and dynamic (interactive or animated). The SVG specification is an open standard that has been under development by the World Wide Web Consortium (W3C) since 1999.

Convert PDF to SVG

Convert PDF to classical SVG

In case you want to convert PDF document, you can use AsposePdfPagesToSvg function. Please check the following code snippet in order to convert in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file that will be converted.
  3. Call AsposePdf as Promise and perform the operation for converting file. Receive the object if successful.
  4. Call the function AsposePdfPagesToSvg.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToSvg.svg”. 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 => {
      /*Convert a PDF-file to SVG and save the "ResultPdfToSvg.svg"*/
      const json = AsposePdfModule.AsposePdfPagesToSvg(pdf_file, "ResultPdfToSvg.svg");
      console.log("AsposePdfPagesToSvg => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file that will be converted
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfPagesToSvg.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToSvg.svg”. 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';
  /*Convert a PDF-file to SVG and save the "ResultPdfToSvg.svg"*/
  const json = AsposePdfModule.AsposePdfPagesToSvg(pdf_file, "ResultPdfToSvg.svg");
  console.log("AsposePdfPagesToSvg => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);

Convert PDF to zipped SVG

In case you want to convert PDF document, you can use AsposePdfPagesToSvgZip function. Please check the following code snippet in order to convert in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file that will be converted.
  3. Call AsposePdf as Promise and perform the operation for converting file. Receive the object if successful.
  4. Call the function AsposePdfPagesToSvgZip.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToSvgZip.zip”. 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 => {
      /*Convert a PDF-file to SVG(zip) and save the "ResultPdfToSvgZip.zip"*/
      const json = AsposePdfModule.AsposePdfPagesToSvgZip(pdf_file, "ResultPdfToSvgZip.zip");
      console.log("AsposePdfPagesToSvgZip => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file that will be converted
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfPagesToSvgZip.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToSvgZip.zip”. 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';
  /*convert a PDF-file to SVG zip and save the "ResultPdfToSvgZip.zip"*/
  const json = AsposePdfModule.AsposePdfPagesToSvgZip(pdf_file, "ResultPdfToSvgZip.zip");
  console.log("AsposePdfPagesToSvgZip => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText)

Convert PDF to DICOM

In case you want to convert PDF document, you can use AsposePdfPagesToDICOM function. Please check the following code snippet in order to convert in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file that will be converted.
  3. Call AsposePdf as Promise and perform the operation for converting file. Receive the object if successful.
  4. Call the function AsposePdfPagesToDICOM.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToDICOM{0:D2}.dcm”. 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 => {
      /*Convert a PDF-file to DICOM with template "ResultPdfToDICOM{0:D2}.dcm" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
      const json = AsposePdfModule.AsposePdfPagesToDICOM(pdf_file, "ResultPdfToDICOM{0:D2}.dcm", 150);
      console.log("AsposePdfPagesToDICOM => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file that will be converted
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfPagesToDICOM.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToDICOM{0:D2}.dcm”. 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';
  /*Convert a PDF-file to DICOM with template "ResultPdfToDICOM{0:D2}.dcm" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
  const json = AsposePdfModule.AsposePdfPagesToDICOM(pdf_file, "ResultPdfToDICOM{0:D2}.dcm", 150);
  console.log("AsposePdfPagesToDICOM => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);

Convert PDF to BMP

In case you want to convert PDF document, you can use AsposePdfPagesToBmp function. Please check the following code snippet in order to convert in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file that will be converted.
  3. Call AsposePdf as Promise and perform the operation for converting file. Receive the object if successful.
  4. Call the function AsposePdfPagesToBmp.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToBmp{0:D2}.bmp”. 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 => {
      /*Convert a PDF-file to BMP with template "ResultPdfToBmp{0:D2}.bmp" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
      const json = AsposePdfModule.AsposePdfPagesToBmp(pdf_file, "ResultPdfToBmp{0:D2}.bmp", 150);
      console.log("AsposePdfPagesToBmp => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file that will be converted
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfPagesToBmp.
  5. Convert PDF file. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfToBmp{0:D2}.bmp”. 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';
  /*Convert a PDF-file to BMP with template "ResultPdfToBmp{0:D2}.bmp" ({0}, {0:D2}, {0:D3}, ... format page number), resolution 150 DPI and save*/
  const json = AsposePdfModule.AsposePdfPagesToBmp(pdf_file, "ResultPdfToBmp{0:D2}.bmp", 150);
  console.log("AsposePdfPagesToBmp => %O", json.errorCode == 0 ? json.filesNameResult : json.errorText);