Convert HTML to PDF in Python

HTML to PDF in Python
Convert HTML to PDF using Python

HTML is the fundamental language for webpages 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.

HTML to PDF Conversion 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 the first step in using the SDK is its installation and it is 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.

After installation, 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.

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 that takes the ApiClient object as an 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 the 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 generated in the 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 a 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 that takes the ApiClient object as an input argument
  • Thirdly, specify the name of the 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 the above code snippet.

How to Save HTML to PDF using cURL Commands

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. Now as a pre-requisite, we need to generate a JWT access token based on client credentials. 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 Webpage to PDF and save the resultant file in 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 to PDF as well as a URL to PDF. We have also seen the details on accessing Cloud API via the cURL command. If you are interested to make changes to the source code of Cloud SDK, it can be downloaded from GitHub. You may also consider exploring the detailed documentation to learn more about the other exciting features being offered.

In case 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 visit the following links to learn the details on