Within the context of projects is normal the need to generate reports, basically as the level of detail increases, so does the complexity of the report. Hence the need to incorporate images to texts, or tables to images or even all together.
Therefore, I will suggest as a response the transformation of HTML to PDF, because then the code can be reusable and without the need to keep changing library or plugins, besides the fact that the PDF is mostly composed of Post Script.
In other words, PDF is basically an image.
There is a library free calling for iTextSharp, I’ve used and solved a similar problem I’ve had.
Example of how to use:
//Cria um array de bytes para salvar os dados do PDF
Byte[] bytes;
//Para evitar preocupação com alocação de memória e etc...
using (var ms = new MemoryStream()) {
//Cria um documento abstrato
using (var doc = new Document()) {
//Cria um escritor para bindar os dados no documento abstrato
using (var writer = PdfWriter.GetInstance(doc, ms)) {
//Abre o documento abstrato
doc.Open();
//Insere o HTML e o CSS como string ou como variável
var example_html = @"<body><p>This is a shitty html code</p><p>This is another html line</p></body>";
var example_css = @"CSS AQUI";
//Converte as strings do HTML e CSS
using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css))) {
using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html))) {
//Faz o Parse do HTML
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
}
}
//Fecha o documento
doc.Close();
}
}
//Fecha MemoryStream e joga dentro do array de bytes
bytes = ms.ToArray();
}
//Transfere o array para o disco
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "download.pdf");
System.IO.File.WriteAllBytes(testFile, bytes);
Now if what you really want is an image, just do it:
//Realiza uma iteração para cada página
for(int i=0;i< pdfdocument.Pages.Count;i++)
{
//Salva a página como imagem
System.Drawing.Image image= pdfdocument.SaveAsImage(i, 96, 96);
image.Save(string.Format("ImagePage{0}.png", i), System.Drawing.Imaging.ImageFormat.Png);
}
Reference
Why would you need an option for ASP.Net MVC?
– Maniero
Man, if this Winforms thing worked on my project, it’d be good for me...
– Jedaias Rodrigues
I don’t understand how this works but it’s strange to be specific, you should generate an image and point, no matter where you should use it. I already question her quality because of this. Are there others that work? Is specific to Mono that works? Or p/PDF? Here seems to be the right one: http://htmlrenderer.codeplex.com/wikipage?title=Image%20generation
– Maniero
Try this if you want to be treated on the client side: http://html2canvas.hertzen.com/examples.html If it’s on the server side, maybe this answer will help you because it helped me in the past: http://stackoverflow.com/questions/25164257/-howto-converthtml-to-pdf-using-itextsharp
– petersonfortes