Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert HTML To Image in .NET Core

Heading ##I want to convert HTML code to Image(png/jpg) on Web Server and then send image link in email in my .NET Core application. I don't want to purchase any third party library like NReco or EVo.

Is there any other way to convert HTML To Image in dotnet core 2.0?

like image 842
N P Avatar asked May 02 '18 15:05

N P


2 Answers

I use net-core-html-to-image library that embeds wkhtmltoimage tool. The library is very simple to use. There is a nuget package:

Install-Package CoreHtmlToImage

If you want to convert HTML string to image:

var converter = new HtmlConverter();
var html = "<div><strong>Hello</strong> World!</div>";
var bytes = converter.FromHtmlString(html);
File.WriteAllBytes("image.jpg", bytes);

Or for URLs:

var converter = new HtmlConverter();
var bytes = converter.FromUrl("http://google.com");
File.WriteAllBytes("image.jpg", bytes);
like image 110
Andrei Avatar answered Nov 03 '22 13:11

Andrei


I know this is old but I wanted to save some from headaches if you come across it. The wkhtmltopdf.exe file that this uses, uses an older rendering engine. I had to go all the way back to bootstrap v2 to make it render correctly.

like image 30
Christopher Steven Avatar answered Nov 03 '22 11:11

Christopher Steven