How can I convert an Html string into an image file?

Posted on

Question :

I have a problem in ItextSharp to put certain HTML in the PDF because it has some limitations on CSS. So I thought about converting HTML to image and simply attaching it to my document.

How can I convert, for example, one table to image? (Without having to convert the entire contents of a page)


Answer :

There is an external component called HtmlRenderer that may be what you need. In the code below I generate a bitmap which is the rendering of an Html table. In the example I’m saving the file to disk, but you can attach it to your document.

Dim arquivoImagem As New Bitmap(600, 600)
Dim pontoInicial As New PointF(0, 0)
Dim TamanhoMaximo As New System.Drawing.SizeF(400, 400)
                                      "<html><body><table border=1><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></body>", 
arquivoImagem.Save("C:Teste.png", ImageFormat.Png)


If you want more information you can take a look at this Stackoverflow post on English because it helped me to find this component and to solve this problem.


Try using Pechkin , which is a wrapper of wkhtmtopdf . This, in turn, uses WebKit, the default engine of some browsers, to generate the PDF. The result is pretty much true to what you see in the browser:)


Leave a Reply

Your email address will not be published. Required fields are marked *