Convert HTML to an image in Python

0 votes
I want to convert following HTML to PNG image in Python. For example:

<html>
    <b>Bold text</b>
</html>
 

I have tried 'pisa' but it converts html to PDF, not to image. I can convert HTML to PDF and then convert PDF to PNG, but I was wondering if there is any direct solution.
Feb 4 in Python by ana1504.k
• 7,870 points
2,786 views
can you help me in converting html source code generated from selenium to PDF?

Hey @SAK!

You can use the pdfkit library to convert the html code to PDF. 

Firsty, you'll have to install wkhtmltopdf and pdfkit for this:

$ sudo apt-get install wkhtmltopdf
$ pip install pdfkit

Next, to convert the code to PDF, refer to the following Python script:

import pdfkit

#If the source code is a string, then use this;
pdfkit.from_string('<string>', '<Output filename with .pdf as extension>')

#If the source code is a html file, then use this:
pdfkit.from_file('<html filename>','<Output filename with .pdf as extension>')
Hi Omkar,

The problem here is I'm doing this work Online where i just have option to use Python package... i cant use wkhtmltopdf there... is there any other way?

Can you try using the script without installing wkhtmltopdf? Check if it works, or post the error that you get. 

"OSError: No wkhtmltopdf executable found: "b''" If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf"

This is the error i get if i just use pdfkit without wkhtmltopdf

Okay, That approach won't work. You said you are working online, can you mention what framework or tool you are using? Maybe can find an API for your requirement.
Im working on client's environment, where i have access to using Python alone and actual problem is to webscrape content directly to PDF using selenium (or any other option if available)

I don't see a way to do this without using external libraries. @SAK

Directly creating a file and writing in it using open method also won't work. You have to at least run a pip install command.

K sure. Whats the way to make it run using any package installed via pip???

Ok so first install the pyfpdf library:

$ pip install pyfpdf

Then in your python script, store the html source code in a variable:

html_source= # html source code.

Import pyfpdf, add the below line at the beginning of your script:

import pyfpdf

Write the text to pdf:

pdf = pyfpdf.FPDF(format='letter')
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt=html_source align="C")
pdf.output("htmlsource.pdf")
Thnx bro! it worked
You're welcome :)
@SAK, I also suggest you sign up to the community so that you get regular updates on Python-related threads. Would be helpful for your projects and you can also help out others with queries.
Yeah sure, will do

1 answer to this question.

0 votes

webkit2png. The original version is OSX-only, but luckily there is a cross-platform fork:https://github.com/AdamN/python-webkit2png

answered Feb 4 by SDeb
• 13,180 points

Related Questions In Python

0 votes
1 answer

I'm using Python 2.7 to convert an XML response (from a REST call to Atlassian Fisheye) into an HTML table.

You don't have a template matching tabularQueryResult in your ...READ MORE

answered Oct 4, 2018 in Python by Priyaj
• 56,900 points
46 views
+4 votes
7 answers
+1 vote
1 answer

How to print an error in Python?

For Python 2.6 and later and Python ...READ MORE

answered Aug 23, 2018 in Python by Priyaj
• 56,900 points
247 views
0 votes
1 answer

How to resize image canvas to maintain square aspect ratio in Python, OpenCv

Building on Alexander-Reynolds answer above, here is ...READ MORE

answered Sep 4, 2018 in Python by Priyaj
• 56,900 points
1,353 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 332 views
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,180 points
72 views
0 votes
1 answer

How to Reverse a list in Python

You can use the reversed function in ...READ MORE

answered Sep 28, 2018 in Python by SDeb
• 13,180 points
78 views