Question

I have the following acceptance criteria for creating a pdf file from my asp.net page which contains nested RadGrid controls:

  1. The current view of the page should be converted to PDF which means that the viewstate and session information of the current page request should be taken into account. This leaves me with only one option; make the PDF conversion at Page_Render() event handler of the current session when a new pdf postback is sent.

  2. The asp.net page layout is changed using JQuery at the time of the $(document).ready(...) that means that not only the rendered HTML should be converted to PDF but also the javascripts have to run on it to have the desired layout changes in the final PDF file; e.g. column alignments, etc. I hope it would be possible otherwise ...

  3. The asp.net page only appears correctly in IE 6+ therefore the PDF tool which is used must use IE rendering engine.

Please could you advise which tool can help in such scenario?

I downloaded and tested EvoPdf tool but it doesn't support IE rendering engine apparently (only FireFox rendering) and couldn't make the javascripts enabling work correctly with it.

I'm going to evaluate ABCPdf and Winnovetive but I'm not sure they would support what I want.

If I could find no tool to help with the above, another possible solution might be just taking a screenshot of the page using client script (don't know whether it'd be possible), then sending it to the server and finally converting that image to pdf.

Many thanks,

Was it helpful?

Solution 3

winnovative did exactly what I needed :) it uses IE rendering engine unlike EvoPdf.

I haven't had time testing other tools.

Thanks

OTHER TIPS

You can try WebToPDF.NET.

  1. Try to convert HTML page which you get after the asp.net page have been rendered
  2. WebToPDF.NET suports JavaScript(and JQuery), so it's not problem
  3. WebToPDF.NET passes all W3C tests (except BIDI) and supports HTML 4.01, JavaScript, XHTML 1.0, XHTML 1.1 and CSS 2.1 including page breaks, forms and links.

Don't know exactly about your requirements but have a look at wkhtmltopdf

How to use wkhtmltopdf.exe in ASP.net

EvoPdf is developed by the same team who develop ExpertPDF (http://www.html-to-pdf.net/). ExpertPDF is the older product so although the APIs are almost identical, the EvoPDF API is slightly more refined.

The main difference between the products is that ExpertPDF uses the local IE rendering engine.

Winnovative HTML to PDF Converter does not use IE as rendering engine. It is compatible with WebKit rendering and does not depend on IE or any other third party tools.

You can convert the current HTML page overriding the Render() method of the ASP.NET page and capture the HTML code being rendered by page. You can find complete example with C# source code in Convert the Current HTML Page to PDF Demo.

Here is the relevant source code for this approach:

// Controls if the current HTML page will be rendered to PDF or as a normal page
bool convertToPdf = false;

protected void convertToPdfButton_Click(object sender, EventArgs e)
{
    // The current ASP.NET page will be rendered to PDF when its Render method will be called by framework
    convertToPdf = true;
}

protected override void Render(HtmlTextWriter writer)
{
    if (convertToPdf)
    {
        // Get the current page HTML string by rendering into a TextWriter object
        TextWriter outTextWriter = new StringWriter();
        HtmlTextWriter outHtmlTextWriter = new HtmlTextWriter(outTextWriter);
        base.Render(outHtmlTextWriter);

        // Obtain the current page HTML string
        string currentPageHtmlString = outTextWriter.ToString();

        // Create a HTML to PDF converter object with default settings
        HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();

        // Set license key received after purchase to use the converter in licensed mode
        // Leave it not set to use the converter in demo mode
        htmlToPdfConverter.LicenseKey = "fvDh8eDx4fHg4P/h8eLg/+Dj/+jo6Og=";

        // Use the current page URL as base URL
        string baseUrl = HttpContext.Current.Request.Url.AbsoluteUri;

        // Convert the current page HTML string a PDF document in a memory buffer
        byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(currentPageHtmlString, baseUrl);

        // Send the PDF as response to browser

        // Set response content type
        Response.AddHeader("Content-Type", "application/pdf");

        // Instruct the browser to open the PDF file as an attachment or inline
        Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Convert_Current_Page.pdf; size={0}", outPdfBuffer.Length.ToString()));

        // Write the PDF document buffer to HTTP response
        Response.BinaryWrite(outPdfBuffer);

        // End the HTTP response and stop the current page processing
        Response.End();
    }
    else
    {
        base.Render(writer);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top