Convert PDF to PNG

The Portable Document Format (PDF) has become quite popular for data and information sharing. However, sometimes we stumble upon a requirement to share a certain portion of a document or only a few pages. In that scenario, either we need to extract the pages from the main file and share them or, we expunge the sensitive information and share the original file. In order to do all these operations, we need to have specific PDF file processing applications installed which consume time and incur licensing costs. But, a viable approach is to utilize a service when required and we pay for the services we use. Rather than paying a huge license amount when only a certain operation is required.

PDF processing API

Aspose.PDF Cloud is our award-winning PDF document processing API offering the capabilities to create, edit and transform PDF files to other supported formats in the Cloud. No software download or installation is required. Aspose.Pdf Cloud is a platform-independent PDF document manipulation API is a true REST API that can be used with any language: .NET, Java, PHP, Ruby, Rails, Python, jQuery, and many more. It can be used in any application type i.e. web, desktop, mobile, and cloud. Owing to the flexibility of the API, it integrates with other cloud services to give you the pliability for documents processing and is suitable for any type of business, document, or content.

In this article, we are going to discuss PDF to PNG conversion using Aspose.PDF Cloud SDK for Node.js which is a wrapper around Aspose.PDF Cloud REST API. It has been specifically created to facilitate our Node.js users so that they get all the capabilities of Cloud API right within their Node.js application.

Installation

In order to use the SDK, we need to first install it either from NPM or GitHub. We are going to discuss the installation of SDK through NPM. Please execute the following command on the command line terminal to perform the installation:

npm i asposepdfcloud

Now the next step is to subscribe on Aspose.Cloud dashboard because our APIs are only accessible to authorized persons only. If you have GitHub or Google account, simply Sign Up. Otherwise, click on the Create a new Account button and provide the required information. Now login to the dashboard using credentials and expand the Applications section from the dashboard and scroll down towards the Client Credentials section to see Client ID and Client Secret details.

Client Credentials

PDF to PNG in Node.js

In order to perform the conversion of PDF files to PNG format, either we convert all the pages of PDF files to Image format, or we select specific pages and then perform the conversion operation. In the following section, we are going to discuss the details on how to convert all the pages of PDF files to PNG format.

  • First we need to import require class references
  • Secondly, create an instance of PdfApi class while passing Client ID and Client Secret details
  • Thirldy, define the input PDF file and resultant image format (PNG)
  • Finally call the putPageConvertToPng(…) method of PdfApi class to initialize the conversion operation
PDF to PNG

The input PDF and resultant output generated in above example can be downloaded from

Convert PDF to PNG using cURL command

Since Aspose.PDF Cloud is developed on REST architecture, so it can also be accessed using the cURL command. However, we need to first generate a JWT access token based on Client ID and Client Secret details because the APIs are only accessed by authorized persons.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=718e4235-8866-4ebe-bff4-f5a14a4b6466&client_secret=388e864b819d8b067a8b1cb625a2ea8e" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Now use the following cURL command to convert the 2nd page of PDF file to PNG format where Width is specified as 400 and Height is mentioned as 600 and the output is saved on local drive.

curl -v -X GET "https://api.aspose.cloud/v3.0/pdf/PdfWithTable(1).pdf/pages/2/convert/png?outPath=PdfWithTable(1)_2.png&width=400&height=600" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-o myResultant.png

Conclusion

This article has explained the capabilities of Aspose.Pdf Cloud SDK for Node.js to convert PDF pages to PNG format. Please note that the complete source code of SDK is available on GitHub, so you can also update it as per your requirements. In case you encounter any issue while using the API or you have any related query, please feel free to contact.

Relatd artciles

We recommend visiting following articles to learn more about