views:

187

answers:

4

Hello

I want to convert an aspx page to PDF using a component that can convert Html to PDF. Is it possible to, during post back, redirect the output from the aspx-page and send it as a stream or string to a HtmlToPdf method?

A: 

Have you tried to send the value returned from "HttpContext.Current.Response.OutputStream;" in the postback ?

ThorHalvor
A: 

Hi I think that the way to do this would be to use the Reponse.Filter property to intercept and alter the HTML being sent to a page.

There's a tutorial video and sample code in both VB.net and C# on this page on the ASP.net website:

http://www.asp.net/learn/videos/video-450.aspx

Nils
A: 

You would write an HttpFilter that is attached to the request. This is code that can change the output after it has been written by the ASP.NET Page's Render step.

This article shows how to do this (they change the output from HTML to valid XHTML, but the idea is the same).

Teun D
+2  A: 
protected override void Render(HtmlTextWriter writer)
{
    // setup a TextWriter to capture the page markup
    TextWriter tw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(tw);

    // render the page into our surrogate TextWriter
    base.Render(htw);

    // convert the TextWriter markup to a string
    string pageSource = tw.ToString();

    if (convertToPDF)
    {
        // convert the page markup to a pdf
        // eg, byte[] pdfBytes = HtmlToPdf(pageSource);
    }

    // write the page markup into the output stream
    writer.Write(pageSource);
}
LukeH
Tanks Luke. This was exactly what I was looking for.
Sanjo