I am trying to convert Line Chart and Circular Gauge chart html as PDF and PNG using HtmlToPdfConverter. But I am facing following issue.
Line Chart:
Circular Gauge Chart:
Line Chart Html: https://jsbin.com/bejoyawazi/edit?html,output
Circular Gauge Html 1 (Not Working): https://jsbin.com/yejozebara/edit?html,output
Circular Gauge Html 2 (Working): https://jsbin.com/vejizenoco/edit?html,output
Code:
// Please Add the html string from previous section.
//string htmlString = "";
var htmlConverter = new HtmlToPdfConverter(HtmlRenderingEngine.WebKit);
var webKitSettings = new WebKitConverterSettings
{
AspectRatio = AspectRatio.FitPageSize,
PdfPageSize = PdfPageSize.A4,
WebKitViewPort = new Size(100, 0),
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];
SizeF clientSize = currentPage.GetClientSize();
PdfDocument document = htmlConverter.Convert(htmlString, string.Empty, clientSize.Width, clientSize.Height, AspectRatio.FitPageSize);
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();
}
}
}
Hi Gowthamraj Kumar,
Thank you so much for your valuable response. The solution is working fine. But I am facing some new issues due to change the engine.
I need to convert the html to pdf in several configuration which are Actual Size, Fit Page, two chart in a page, four chart in a page. To support these types of requirement I had set the WebKitViewPort size to 900 (for Actual) and 100 (for other 3 options) and calculate the size and location of each chart dynamically before DrawPdfTemplate. These solution was working almost fine in WebKit engine. But it's broken now for Blink Engine. You may assume I am passing array of charts/html to the back-end and convert the html to pdf using a loop with the configuration.
So how do we fix this in blink engine? I have tried by changing the ViewEngine size. But I didn't get the expected output.
Details Explanation:
I have observed that the default size of a pdf page is (w=802,h=515) without the margin and header footer.
So if we have 4 charts (Most of the cases all charts are SVG) in an array and pass a configuration (You can add the previous charts in an array with duplicate value). We need to do the following things depending on the configuration.
Basically we need to find a way to convert the chart into pdf with a specific height and width no matter what is html height width. If we find that way, we can easily draw the charts into pdf page by passing the location property. Do you have any idea how we do this? It would be a great help for me.
You can find the difference here: https://drive.google.com/drive/folders/1NUeHL2lWn6WTqSZPQC62bksPPQtm1_JM?usp=sharing
Regards,
Hasan