2011-07-24 55 views
1

我爲創建從我的asp.net頁面的PDF文件,其中包含嵌套radgrid控件控制以下驗收標準:如何將我的ASP.NET頁面轉換爲PDF?

  1. 頁面的當前視圖應該被轉換成PDF,這意味着視圖狀態和會話應該考慮當前頁面請求的信息。這讓我只有一個選擇;在發送新的pdf回發時,在當前會話的Page_Render()事件處理程序中進行PDF轉換。

  2. 在$(document).ready(...)時,使用JQuery更改了asp.net頁面佈局,這意味着不僅呈現的HTML應該轉換爲PDF,而且JavaScript必須運行它以在最終的PDF文件中進行所需的佈局更改;例如列的路線等,我希望這將是可能的,否則...

  3. asp.net頁面只在IE 6+正確顯示,因此使用的PDF工具必須使用IE渲染引擎。

請你能建議哪種工具可以在這種情況下提供幫助嗎?

我下載並測試了EvoPdf工具,但它並不支持IE渲染引擎(僅FireFox渲染),並且無法使JavaScripts使其正確工作。

我打算評估ABCPdf和Winnovetive,但我不確定他們會支持我想要的。

如果我找不到任何工具,以幫助上述情況,另一個可能的解決方案可能是隻考慮使用客戶端腳本的網頁截圖(不知道是否會是可能的),然後將其發送到服務器並最終將該圖像轉換爲pdf。

非常感謝,

回答

0

winnovative做正是我需要的:)它使用IE渲染引擎不像EvoPdf。

我還沒有來得及測試等工具。

感謝

1

你可以試試WebToPDF.NET

  1. 嘗試轉換的HTML頁面,你得到的asp.net頁面已經呈現
  2. WebToPDF.NET suports的JavaScript(和JQuery)之後,所以這不是問題
  3. WebToPDF.NET通過了所有測試W3C (BIDI除外),並支持HTML 4.01,JavaScript,XHTML 1.0,XHTML 1.1和CSS 2.1,包括分頁符,表單和鏈接。
+0

我得到了保護覆蓋無效的HTML渲染(HtmlTextWriter的作家),並調用轉換方法。現在的問題是,PDF不會以這種方式顯示爲彈出式窗口,只是在頁面上呈現一些二進制信息。任何想法如何使它顯示爲一個彈出? –

+0

其實我不明白什麼是二進制信息...我補充說,你應該得到呈現的asp.net頁面的HTML代碼,並將此代碼轉換爲PDF。因此,您將獲得PDF格式的流。進一步你可以用它來做任何你想要的。如果您希望它顯示爲彈出窗口,則將此流寫入Response並將內容類型指定爲「application/pdf」 – zavolokas

0

EvoPdf是誰開發ExpertPDF(http://www.html-to-pdf.net/)同一個團隊開發的。 ExpertPDF是老產品所以雖然API是幾乎完全相同,EvoPDF API稍微更精緻。

產品之間的主要區別是ExpertPDF使用本地IE呈現引擎。

0

Winnovative HTML to PDF Converter不使用IE作爲渲染引擎。它與WebKit渲染兼容,不依賴於IE或任何其他第三方工具。

您可以轉換當前HTML頁面,覆蓋ASP.NET頁面的Render()方法並捕獲頁面呈現的HTML代碼。您可以在Convert the Current HTML Page to PDF Demo中找到完整的C#源代碼示例。

下面是該方法的相關源代碼:

// 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); 
    } 
} 
相關問題