HTML to PDF in Python

Convert HTML to PDF using Python Cloud SDK

In a digital age inundated with online content, the need to preserve, share, and access information in a versatile and presentable format has never been more crucial. Converting HTML files to PDF addresses this need precisely, offering a plethora of benefits that extend beyond the capabilities of HTML alone. We understand that the PDFs are universally recognized, ensuring that the intended format and style of the content remain consistent across various devices and platforms. Therefore, if you are looking to archive web content, create printable documents, or streamline data sharing, we are going to discuss a powerful and accessible solution for HTML to PDF conversion.

So, this article delves into the compelling reasons behind the shift from HTML to PDF using Python Cloud SDK. It highlights the diverse conversion approaches, empowering individuals and businesses in managing and distributing information effectively.

HTML to PDF Conversion REST API

The seamless conversion of HTML to PDF is a breeze with the help of Aspose.PDF Cloud SDK for Python. This powerful SDK offers a straightforward and efficient way to integrate HTML to PDF conversion capabilities into Python applications. Leveraging Aspose.PDF Cloud’s versatility, you can effortlessly transform HTML format to professionally formatted PDFs.

Now the first step in using the SDK is its installation, which 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 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.

Convert 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 then stored in Cloud storage.

  • Firstly, 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 ApiClient object as an 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, resultant PDF name, Height, Width, and parameters including page orientation as arguments.

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

Web to PDF in Python

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

  • 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 with above code snippet.

How to Convert HTML to PDF using cURL Commands

Converting HTML to PDF using Aspose.PDF Cloud and cURL commands is a streamlined process, combining the power of Aspose.PDF Cloud with the simplicity of cURL. With Aspose.PDF Cloud, developers can achieve HTML to PDF conversion without the need for complex code or extensive configurations. Furthermore, the integration is platform-independent, making it accessible and effective across various operating systems.

Now, as a prerequisite, we need to generate a JWT access token using 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 web to PDF and save the output 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 explored two robust approaches: leveraging the Aspose.PDF Cloud SDK for Python and utilizing Aspose.PDF Cloud with cURL commands. The dedicated SDK for Python offers a developer-friendly toolkit, providing precise control and customization options for HTML to PDF conversion. It empowers you to integrate this functionality seamlessly, enhancing document portability and presentation. On the other hand, integrating Aspose.PDF Cloud through cURL commands streamlines the Web to HTML conversion, providing a versatile and accessible solution.

Whether you opt for the feature-rich Aspose.PDF Cloud SDK for Python or the simplicity of cURL commands with Aspose.PDF Cloud, both pathways lead to efficient HTML to PDF conversion. This flexibility empowers you to tailor your HTML to PDF conversion process, enhancing document management and sharing.

We highly recommend visiting the following links to learn about: