HTML to PDF Conversion

Summary

Aspose.HTML Cloud v4.0 provides the simplest API to convert HTML documents to PDF format with high quality, easy and fast. HTML to PDF conversion is often required to establish limited access to documents editing or copying; to produce official documentation or send some information, for example, by email.

This article demonstrates how to convert HTML file to PDF by direct REST API calls or using SDKs.

cURL Example

A cURL is a command-line tool that you can use to access Aspose.HTML Cloud web services.

This example demonstrates how to convert an HTML file located in the default cloud storage to PDF with default parameters using the cURL utility (in the console, or using the system shell script - .bat file or PowerShell script in Windows, bash script in Linux etc.).

Step 1: Get JWT token for the user authentication

Step 2: Initialize an asynchronous conversion process using the POST method

The curl -X POST command specifies a request method (POST) to use when communicating with the HTTP server. The POST method is usually used in requests for adding data; in this case, an empty parameter list is passed -d "{}".

Step 3: Check the previously started conversion process status using the GET method

The GET method allows checking the previously started operation state by its ID and returns the state object.

SDK Examples

These examples demonstrate how to convert HTML to PDF using available SDKs.

Case 1. Convert an HTML document from the storage by its name to PDF and save to the storage

The conversion process is following - a source document is loaded from the cloud storage, and after conversion is completed, the result is saved to this storage.

In the example, we use methods of the ConverterBuilder class: FromStorageFile(inputPath), To(ConversionOptions) and SaveToStorage(outputDirectory) that specify input data, the output format and the target directory for a conversion result. HTML to PDF conversion occurs with the default conversion options: the resulting PDF document’s width and height correspond to A4, all margins have zero value, and Quality of jpeg compression is 95%.

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

Case 2. Convert a Web page by its URL to PDF with explicit PDF options and save to the local file system

Aspose.HTML Cloud SDK allows you to get HTML page from the web by its URL, convert it to another format and save into the local file system. The example below demonstrates how to convert HTML to PDF with the conversion parameters specifying.

In the example, we use methods of the ConverterBuilder class: FromUrl(urlAddress), To(ConversionOptions) and SaveToStorage(outputDirectory) that specify input data, the output format and the target directory for a conversion result.

You can get information about all the API resources from the Aspose.HTML for Cloud API Reference.