2009-02-11 60 views
0

我想使用可以將Html轉換爲PDF的組件將aspx頁面轉換爲PDF。是否有可能在回發期間重定向aspx頁面的輸出並將其作爲流或字符串發送到HtmlToPdf方法?從aspx下載劫持輸出

回答

2
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); 
} 
+0

坦克盧克。這正是我正在尋找的。 – Sanjo 2009-02-12 06:39:46

0

您是否嘗試發送從「HttpContext.Current.Response.OutputStream;」返回的值在回發中?

0

你會寫是連接請求的HttpFilter。這是在ASP.NET頁面的Render步驟寫入後可以更改輸出的代碼。

This article顯示瞭如何做到這一點(它們將輸出從HTML更改爲有效的XHTML,但想法相同)。