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

3

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)

    
asked by anonymous 01.02.2014 / 21:12

2 answers

3

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)
HtmlRenderer.HtmlRender.RenderGdiPlus(Graphics.FromImage(arquivoImagem), 
                                      "<html><body><table border=1><tr><td>A</td><td>B</td></tr><tr><td>C</td><td>D</td></tr></body>", 
                                      pontoInicial, 
                                      TamanhoMaximo)
arquivoImagem.Save("C:\Teste.png", ImageFormat.Png)

Console.ReadKey()

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.

    
18.02.2014 / 19:05
1

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:)

    
28.02.2014 / 13:54