Convert HTML to PDF in Python – URL to PDF in Python

HTML to PDF in Python

HTML is the fundamental language for webpage and in today’s internet world, they play an essential role in information sharing. Mainly, all web browsers support HTML format and use it as the default rendering language. You may view information online or save a copy of the HTML file for later review. If the graphs or images are referenced inside an HTML article, they will only be displayed when a proper internet connection is provided and those images are still available at their source. But in any unfavorable condition, they may not always be included in the HTML file or may look different. So in order to avoid such issues, HTML files can be converted to PDF format. Some other benefits that we may achieve with this conversion include setting paper size & margins., retaining the tags from HTML pages, etc.

PDF Processing API

Aspose.PDF Cloud is our REST-based API offering the capabilities to create as well as manipulate existing PDF files. The reason we are discussing this API here is that it also provides the feature to convert HTML, PCL, PS, XSLFO, XPS, and various other supported formats to PDF. So we can use the same API to load an HTML file and render the output to PDF format.

Now in order to further facilitate the developers, we have created programming language SDKs. These are wrappers around Cloud API. In this article, we are going to use Aspose.PDF Cloud SDK for Python for a programmatic conversion of HTML files to PDF.

Installation

The first step in using the SDK is its installation and its available for download over PIP and GitHub repository. Execute the following command on the terminal/command prompt to install the latest version of SDK on the system.

 pip install asposepdfcloud

In case you need to directly add the reference in your Python project within Visual Studio IDE, please search asposepdfcloud as a package under the Python environment window. Please follow the steps numbered in the image below to complete the installation process.

Aspose.PDF Cloud Python
Image 1:- Aspose.PDF Cloud SDK for Python package.

Aspose.Cloud Dashboard

In order to get started with Cloud APIs, we need to create an account on Aspose.Cloud dashboard. If you have GitHub or Google account, simply Sign Up or, 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
Image 2:- Client credentials on Aspose.Cloud dashboard.

HTML to PDF in Python

In this section, we are going to discuss the details on how to load an HTML file already available in Cloud storage and convert the output to PDF format. The resultant file is also saved in Cloud storage.

  • First, we need to create an instance of ApiClient class while providing Client ID Client Secret as arguments
  • Secondly, create an instance of PdfApi class which takes ApiClient object as input argument
  • Now specify the name of input HTML (package as .zip archive) and resultant PDF file name
  • Finally, call the put_html_in_storage_to_pdf(..) method which takes source .zip file, name of HTML inside .zip archive, resultant PDF name, Height, Width, and parameter to specify if the resultant output will be Landscape or not.

The input HTML and resultant PDF genearted in above example can be downloaded from source.zip and HTMLConverted.pdf.

Webpage to PDF in Python

In this section, we are going to convert a live webpage to PDF format using Python code snippet.

  • First, we need to create an instance of ApiClient class while providing Client ID Client Secret as arguments
  • Secondly, create an instance of PdfApi class which takes ApiClient object as input argument
  • Thirdly, specify the name of resultant PDF file name
  • Now call the method put_web_in_storage_to_pdf(..) to convert Webpage to PDF format

Please click URL2PDF.pdf to download the PDF file generated as a result of above code snippet.

Convert HTML to PDF using cURL Command

The cURL commands provide an amazing approach for accessing the REST APIs through the command line terminal on any platform. Since Aspose.PDF Cloud is based on REST architecture, so it can also be accessed via the cURL command.

But, before proceeding further, please note that our Cloud APIs are only accessible by authorized persons, so, please generate a JWT access token based on client credentials specified over Aspose.Cloud dashboard. So please execute the following command to generate the JWT access token.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=88d1cda8-b12c-4a80-b1ad-c85ac483c5c5&client_secret=406b404b2df649611e508bbcfcd2a77f" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Once the JWT token has been generated, please use the following cURL command to convert an HTML file packaged as a .zip archive in Cloud storage to PDF format and save the resultant PDF in the same cloud storage.

curl -v -X PUT "https://api.aspose.cloud/v3.0/pdf/Converted.pdf/create/html?srcPath=source.zip&htmlFileName=completeWorkbook.html&height=1024&width=800&isLandscape=false" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>"

In case we need to convert a Live webpage to PDF format, please try using the following cURL command.

curl -X PUT "https://api.aspose.cloud/v3.0/pdf/URL2PDF.pdf/create/web?url=https%3A%2F%2Fwww.aspose.cloud%2F&isLandscape=true" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>"

Conclusion

In this article, we have explored the steps on how to programmatically convert an HTML file and webpage to PDF format. We have also seen the details on accessing Cloud API via the cURL command. If you are interested to make changes in the source code of Cloud SDK, it can be downloaded from GitHub.

We suggest you explore the extensive documentation of the product to learn more about the other exciting features being offered. Also, if you encounter any issues while using the API or you have any further queries, please feel free to contact us via the free customer support forum.

Related Articles

We recommend you to visit the following links to learn the details on