Convert HTML to PDF using Cloud SDK – Aspose.HTML Cloud

HTML files are frequently used to create, edit, or communicate a lot of information. However, there are times when converting HTML documents to PDF becomes imperative. 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 explains in a set of Aspose.HTML Cloud SDK and REST API examples how to convert HTML to PDF. We’ll look at different HTML to PDF conversion scenarios: from storage to storage, from local file to local file system, and from the Web to local file system.

HTML to PDF Conversion

A widespread use case for Aspose.HTML Cloud functions is file processing and converting. Aspose.HTML Cloud allows you to fetch an HTML document from a 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 the local drive. The following code examples demonstrate how to convert HTML to PDF for different cases using available SDKs and REST API. A family of SDKs is available to help developers speed up their development, including C#, Java, C++, Python, PHP, Ruby, Swift, Java/Android, Node.js, etc. These SDKs allow developers to easily integrate HTML to PDF conversion functionality into their applications, regardless of the platform they are running on.

Asynchronous vs Synchronous Conversion

There are two general approaches for converting HTML to PDF. 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-pdf" \
3    -d "{'InputPath': 'https://example.com', 'OutputFile': 'test.pdf'}" \
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.pdf",
 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.pdf"
 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.pdf"

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-pdf/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.pdf"

Example 1. Convert HTML to PDF with default save options

Let’s look at an example of converting a local HTML file to PDF and saving the result to a local path. The conversion occurs with default conversion options.

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

Example 2. Convert HTML to PDF with explicitly specified options

The example below demonstrates how to convert an HTML file from a local file system to PDF with explicitly specified options and save the result back to a local path.

More details about available conversion parameters for HTML files you find in the SDK Conversion Options article.

Example 3. Convert a Web page to PDF

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

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

Example 4. Convert HTML to PDF inside cloud storage

Aspose.HTML Cloud allows you to get an HTML file from your cloud storage and save the conversion result back to cloud storage. Let’s look at an example of conversion within the cloud storge with default saving options.

See Also