save a div with css to pdf file ASP.net C#

1.4k views Asked by At

i have a div in asp.net page

<div id="blah"></div>

the contents of this div are generated from code behind using c#. And it is being applied with external css file. Now using c#i want to save this rendered div to a pdf file on server.

i tried using HtmlRenderer.PdfSharp 1.5.0.6 Sample code which is working is

PdfSharp.Pdf.PdfDocument pdf = PdfGenerator.GeneratePdf("<p><h1>Hello World</h1>This is html rendered text</p>", PageSize.A4);
        pdf.Save(@"C:\document.pdf");

But instead of this html snippet i want to get the rendered div

what i have tried so far is

StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            blah.RenderControl(hw);
            PdfSharp.Pdf.PdfDocument pdf = PdfGenerator.GeneratePdf(sw.ToString(), PdfSharp.PageSize.A4);
            pdf.Save(@"C:\document1.pdf");

            sw.Close();

its coming correct. but the only this is that it just takes the html markup inside the div without css

0

There are 0 answers