Convert XHTML to Image – Cloud SDKs and REST API

XHTML to Image

XHTML is a markup language that combines the syntax of HTML with the strictness of XML, allowing for well-formed and structured web documents. Converting XHTML to an image format such as JPEG or PNG allows users to create visual snapshots of web content for various purposes. This conversion is especially useful for creating thumbnails, previews, or visual representations of web pages that can be easily shared, displayed, or embedded in other documents or applications.

Aspose.HTML Cloud v4.0 provides the simplest API to convert XHTML documents to Image file formats with high quality, easy and fast.

The article explains in a set of code examples how to convert an XHTML document to Image file formats such as JPEG, PNG, GIF, TIFF or BMP using Aspose.HTML Cloud SDK and REST API calls. SDKs are available in PHP, C++, Python, C#, Ruby, Java, Android, Swift, and Node.js. We will consider various scenarios of XHTML to Image conversion:

  • Converting a local XHTML file to an image and saving the result in your local file system. In this scenario, we will explore both conversions with default conversion parameters and with explicitly specified parameters.
  • Converting XHTML to Image in cloud storage with default conversion parameters.

SDK Examples of XHTML to Image Conversion

A widespread use case for Aspose.HTML Cloud SDK functions is file processing and converting. Cloud SDK allows you to fetch an XHTML document from a storage location by its name 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 XHTML to Image for different cases using available SDKs.

Example 1. Convert XHTML to JPG

JPG 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.

Consider converting an XHTML document initially located on a local file system to JPG format. The resultant file is then saved back to the local file system. The conversion occurs with default conversion options.

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

Example 2. Convert XHTML to PNG with explicitly specified options

PNG image format widely takes to transmit pictures over the network, display photos and graphics on web-pages and reserve in cloud storages. XHTML to PNG conversion may be required, for example, if you want to add a web page in a PowerPoint presentation, insert it on a blog for your readers, or send it by e-mail. The example below shows how to convert XHTML to PNG with explicitly specified options and save the result back to a local file system.

More details about available conversion parameters for XHTML files are in the SDK Conversion Options section.

Example 3. Convert XHTML to GIF inside cloud storage

For those who need to convert an XHTML file stored in cloud storage to an image, the process is as follows. The XHTML file is accessed from the cloud storage, converted to the image format, and then saved back to the same cloud storage location.

See Also