4
I own a string
with the page’s HTML, is there any way to save it as an image? Render the whole page as an image, and if possible, store it in an MemoryStream
.
4
I own a string
with the page’s HTML, is there any way to save it as an image? Render the whole page as an image, and if possible, store it in an MemoryStream
.
3
It is a C#library. I recommend it because it does not need anything else besides the DLL itself, without other dependencies, if you want to save in image.
Example (withdrawn from here, in English):
namespace HtmlToBmpImageDemo
{
class Program
{
static void Main(string[] args)
{
Bitmap m_Bitmap = new Bitmap(400, 600);
PointF point = new PointF(0, 0);
SizeF maxSize = new System.Drawing.SizeF(500, 500);
HtmlRenderer.HtmlRender.Render(Graphics.FromImage(m_Bitmap),
"<html><body><p>This is a shitty html code</p>"
+ "<p>This is another html line</p></body>",
point, maxSize);
m_Bitmap.Save(@"C:\Test.bmp }
}
}
I also recommend this library because they have direct save support in *.pdf. Get a Nuget search for HtmlRenderer.PdfSharp
.
Example (withdrawn from here, in English)
public static Byte[] PdfSharpConvert(String html)
{
Byte[] res = null;
using (MemoryStream ms = new MemoryStream())
{
var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
pdf.Save(ms);
res = ms.ToArray();
}
return res;
}
wkhtmltopdf encapsulates a console application for the .NET. According to their page on Github, It is simple to convert HTML to PDF using the tool.
I won’t go into it because there is already an answer talking about it.
He is protected by LGPL, So I recommend you take a look, depending on your case, it could be a complication. Of course, reading the official license "contract" is critical, but there are some tools that can help you, such as Choose a License, which I find quite reliable. Another curious fact about the site is that he is opensource.
So, I was already trying to convert the html directly to PDF, but it does not load all CSS, because there is no support at all, then the layout gets all messy.
2
The easiest way for you to achieve this is by using an external utility, in case the WKHtmlToImage
.
To do so, you will need to write your HTML
on disk, using the System.IO.File.WriteAllText(string path, string contents)
.
The second step is to instantiate a System.Diagnostics.Process
, set the processo.StartInfo.FileName
to the place where the WKHtmlToImage
and pass the appropriate arguments through processo.StartInfo.FileName
if you need to know what arguments are expected by WKHtmlToImage
, you can consult the Manual
, although in your case, I believe that wkhtmltoimage <input file> <output file>
will be enough.
When the process is finished, you can read the disk image.
There wouldn’t be any I could save html to a string and then convert it to an image without needing to download?
Browser other questions tagged c# html image pdf-generation
You are not signed in. Login or sign up in order to post.
What’s the point of doing this?
– Woss
I need to convert it to a PDF, I tried to convert straight from HTML only iTextSharp n is compatible with all the css used, so I thought to convert html to image and then convert the image to PDF.
– Brayan
See if this online feature helps you Convert HTML to PDF
– Thiagopsw