Blog

Back to blog posts

Convert HTML to Image with Python

Published Jul 14, 2021

Intro

You might need to convert HTML to an image or HTML to png format for various reasons. Perhaps you are generating a snapshot or image preview of an HTML template you designed as part of your application. Maybe you are crawling websites and would like an image preview of all of them for a database. In either case, if you’re writing python and you have been doing research, you probably came across Headless Chrome or wkhtmltoimage. Working with either is a headache. Much easier to write just a few lines of code and call it a day. You can easily convert HTML to an Image (png) with python within minutes using our REST API.

Convert HTML to Image with Python

Step 1) Open up your package manager and run the command

pip install api2pdf

Step 2) Grab an API key from https://portal.api2pdf.com. Only takes 60 seconds.

Step 3) Use the sample code below and replace “YOUR-API-KEY” with the api key you acquired in step 2.

from api2pdf import Api2Pdf

a2p_client = Api2Pdf('YOUR-API-KEY')
api_response = a2p_client.Chrome.html_to_image('Hello, World')
print(api_response.result)

And that’s it! Modify the code as you see fit. Hopefully this saves you time and makes converting HTML to images easy and painless for those writing python code.

See full github library

We have a whole python based client library for our API that does a lot more than just this. Check out the full library capabilities here: https://github.com/Api2Pdf/api2pdf.python