Convert HTML to Image – Aspose.HTML Cloud SDK

A widespread use case for Aspose.HTML Cloud functions is file processing and converting. HTML to image conversion allows developers and users to save the visual representation of a web page as a single image file that can be easily shared, displayed, or manipulated.

This article explains in a set of Aspose.HTML Cloud SDK examples how to convert an HTML document to Image file formats such as JPEG, PNG, GIF, TIFF, or BMP. We’ll look at different scenarios for converting HTML to an image: from storage to storage, from local file to local file system, and from the Web to local file system.

HTML to Image Conversion

Aspose.HTML Cloud SDK allows you to fetch an HTML document from storage location by its name, from a URL or a local file on your drive, convert it to a specified format and save it to the storage or a local drive. The following code examples demonstrate how to convert HTML to Image formats for different cases. These examples show how to use available SDKs and REST API for conversion operations. A family of SDKs is available to help developers speed up development, including C#, Java, C++, Python, PHP, Ruby, Swift, Java/Android, Node.js, etc.

Asynchronous vs Synchronous Conversion

There are two general approaches for converting HTML to Image. The asynchronous approach involves submitting a conversion request, receiving an operation ID, and then checking the status of the conversion until it is completed. Once the status is completed, the resulting file can be downloaded using the provided download link. The synchronous approach, on the other hand, processes the conversion in a single operation. The API waits for the conversion to complete and directly returns the PDF in the response, eliminating the need to check statuses and perform separate download steps. Both methods support the same conversion options.

Asynchronous Conversion

The asynchronous conversion approach allows for the submission of a conversion request, where the server processes the conversion in the background. Once the request is submitted, an operation ID is returned, which can be used to check the status of the conversion. Upon completion, the resulting file can be downloaded using the provided ID and download link. Below is an example of how to initiate an asynchronous conversion request.

Submit conversion request

1curl -X POST -v \
2	"https://api.aspose.cloud/v4.0/html/conversion/html-jpeg" \
3    -d "{'InputPath': 'https://example.com'}" \
4    -H "Content-Type: application/json" \
5    -H "Authorization:Bearer <JWT_token>"

Result:

 1{
 2    "file": null,
 3    "code": 200,
 4    "id": "CN-a902f0ab-6d67-4847-93b3-9d90826f7ead",
 5    "status": "running",
 6    "description": null,
 7    "links": {
 8        "self": "/v4.0/html/conversion/CN-a902f0ab-6d67-4847-93b3-9d90826f7ead",
 9        "download": null
10    }
11}

Checking Conversion Status:

To check the status of the conversion, use the id returned in the previous response:

1curl -X GET "https://api.aspose.cloud/v4.0/html/conversion/CN-a902f0ab-6d67-4847-93b3-9d90826f7ead" \
2     -H "Content-Type: application/json" \
3     -H "Authorization:Bearer <JWT_token>"

Result:

 1{
 2    "file": "76482e8b-8d2e-465b-8407-5c7f6e93e614/example.jpeg",
 3    "code": 200,
 4    "id": "CN-a902f0ab-6d67-4847-93b3-9d90826f7ead",
 5    "status": "completed",
 6    "links": {
 7        "self": "/v4.0/html/conversion/CN-a902f0ab-6d67-4847-93b3-9d90826f7ead",
 8        "download": "v4.0/html/file?path=76482e8b-8d2e-465b-8407-5c7f6e93e614/example.jpeg"
 9    }
10}

Possible Statuses:

•	completed
•	pending
•	running
•	faulted
•	canceled

Downloading the Conversion Result:

If the status is completed, the result can be downloaded using the id:

1curl -X GET "https://api.aspose.cloud/v4.0/html/conversion/CN-662a4f3f-44a3-43ed-8c23-5c7b2a052c02/download" \
2     -H "Authorization:Bearer <JWT_token>"

The REST API will return the file as a response. This response will include the Content-Type set as application/octet-stream and the file will be sent as a binary stream for download. This is equivalent to:

1Content-Type: application/octet-stream
2Content-Disposition: attachment; filename="example.jpeg"

Synchronous Conversion

For smaller files, synchronous conversion can be used to eliminate the need to check statuses and download the result in separate calls. The entire process is handled within one call:

1curl -X POST "https://api.aspose.cloud/v4.0/html/conversion/html-jpeg/sync" \
2     -H "Content-Type: application/json" \
3     -H "Authorization:Bearer <JWT_token>" \
4     -d '{"InputPath": "https://example.com"}'

Result:

1Content-Type: application/octet-stream
2Content-Disposition: attachment; filename="example.jpeg"

Example 1. Convert HTML to JPEG

JPEG is one of the most commonly used image formats. Its uniqueness is the controlled loss of quality during compression. Therefore, it is widely used to store and send graphic content (photos, scanned copies, digitized images) over the Internet. Let’s look at an example of converting a local HTML file to JPEG and saving the result to a local path. The conversion occurs with default conversion options.

HTML to JPEG conversion occurs with the default conversion options: the resulting JPEG document’s width and height correspond to A4, and all margins have zero value.

Example 2. Convert HTML to PNG with explicitly specified options

The PNG image format is widely used for transferring images over the network, displaying photos and graphics on web pages, and backing them up to cloud storage. Converting HTML to PNG may be necessary, for example, if you want to add a web page to a PowerPoint presentation, embed it in a blog for readers, or send it via email. The example below shows how to convert HTML to PNG with explicit parameters and save the result back to the local file system.

Example 3. Convert Webpage to Image

Aspose.HTML Cloud allows you to retrieve an HTML page from the web by its URL, convert it to image format, and save it to the local file system. The example below demonstrates how to convert webpage to image in GIF format with the default conversion parameters.

HTML to GIF conversion occurs with the default conversion options: the resulting GIF document’s width and height correspond to A4, and all margins have zero value.

Example 4. Convert HTML to JPEG inside cloud storage

Aspose.HTML Cloud allows you to get an HTML file from your cloud storage and save conversion result back to cloud storage. The conversion occurs with default conversion options.

See Also