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

Asked

Viewed 646 times

3

I have a problem in Itextsharp to put certain HTML in the PDF, because it has some limitations in relation to CSS. So I thought of converting HTML into image and simply attaching it to my document.

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

2 answers

3

There is an external component called Htmlrenderer which may be what you need. In the code below I Gero a bitmap which is the rendering of an HTML table. In the example I am saving the file on 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 other Stackoverflow post in English, because it helped me to find that component and to solve that problem.

1

Try to use the Pechkin, which is a wrapper of wkhtmtopdf. This, in turn, uses the Webkit, standard engine of some browsers, to generate the PDF. The result is practically true to what is seen in the nagevador :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.