We want to convert a html string into pdf and image. We have done this by following these piece of code. Pdf output is fine but the image output is very blurry. How do we make the image output very clear (Same as Pdf)?
// Add the html string from here https://pastebin.com/bYZd50ME
string htmlString = "";
var htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
var webKitSettings = new WebKitConverterSettings
{
AspectRatio = AspectRatio.FitPageSize,
PdfPageSize = PdfPageSize.A4,
Orientation = PdfPageOrientation.Landscape,
SinglePageLayout = SinglePageLayout.FitWidth
};
htmlConverter.ConverterSettings = webKitSettings;
var stream = new MemoryStream();
using (var doc = new PdfDocument())
{
PdfPage page = doc.Pages.Add();
PdfPage currentPage = doc.Pages[0];
PdfDocument document = htmlConverter.Convert(htmlString, string.Empty);
PdfPage pdfPage = document.Pages[0];
PdfTemplate template = pdfPage.CreateTemplate();
currentPage.Graphics.DrawPdfTemplate(template, new PointF(50,50));
// Save the document to stream
doc.Save(stream);
// Save the stream as pdf
FileStream pdfFileStream = File.OpenWrite("TestReport.pdf");
stream.WriteTo(pdfFileStream);
// Save the stream as image (png)
using (var loadedDoc = new PdfLoadedDocument(stream))
{
using (var imageStream = new MemoryStream())
{
Image image = loadedDoc.ExportAsImage(0);
image.Save(imageStream, ImageFormat.Png);
imageStream.Position = 0;
FileStream outStream = File.OpenWrite("TestReport.png");
imageStream.WriteTo(outStream);
outStream.Flush();
imageStream.Dispose();
}
}
}
|
PDF to Image |
We can export the Pdf document into image with good quality by using dpi values. Please try the below code snippet on your end and let us know the result.
Please find the output document for your reference,
| |
|
Html to Image |
The WebKit HTML Converter provides support for converting HTML string to Image. Please refer the below documentation link,
https://help.syncfusion.com/file-formats/pdf/working-with-document-conversions#html-string-to-raster-image
Please let us know if you need any further assistance with this.
|