我最近遇到有關將iTextSharp中生成的PDF輸出到新窗口的問題,但無法找出解決問題的方法。iTextSharp |無法在IE中輸出PDF
這個問題似乎只發生在IE瀏覽器,並在新的窗口打開時,它仍然是空白的,似乎並沒有加載PDF。 (凡爲Chrome和Firefox似乎工作就好了)
我將要經歷的過程的概述中大家提供一些幫助的希望:
第1步:
用戶點擊「打印」按鈕,我有一個視圖中,這將調用下面的JavaScript執行:
window.open($("#PrintURL").val(), 'Print_Preview', 'resizable=1');
步驟2:
被稱爲拉動相應的數據到一個模型,然後將URL傳遞到這一點的局部視圖,其被傳遞到一個PDFResult,如圖所示:
//Grabs the Data
var data = reportAgent.GetData();
//Builds a string that contains the Report layout and builds the Report
string html = ControllerContext.RenderPartialAsString("~/Views/Reports/Report.cshtml", data);
return new PDFResult(html, ...);
步驟3 :
在PDFResult ExecuteResult()
方法中,我構建報表將輸出的文檔,並調整其他參數,例如PageSize,Headers和Footers。
public override void ExecuteResult(ControllerContext context)
{
//Sets Response to output a PDF
var response = context.HttpContext.Response;
response.ContentType = "application/pdf";
//Generate Document
Document document = new Document();
//Sets Page Size and Styles
//Build Headers and Footers and Add to Document
//Builds the document writer and prepares a Print Dialog upon opening
PdfWriter writer = PdfWriter.GetInstance(document, context.HttpContext.Response.OutputStream);
PdfAction action = new PdfAction(PdfAction.PRINTDIALOG);
writer.SetOpenAction(action);
writer.PageEvent = page;
document.Open();
//Iterates through the html string that was passed in and formats the document
document.Close();
}
我似乎無法弄清楚爲什麼新窗口在Internet Explorer中保持空白,而大多數其他瀏覽器似乎工作得很好。 (我知道我過去曾在IE中使用過相同的流程,現在我只是難住了。)
任何想法/建議,以改善這一進程將不勝感激。
UPDATE
我測試了一些變化表明,當我刪除了將建立文檔的區域,只是增加了以下內容:
document.Add(new Phrase("TEST"));
這將只需將一行添加到PDF中,然後生成它。我收到了「Internet Explorer無法顯示網頁」的錯誤,並提供了診斷連接問題的選項。單擊「診斷連接問題」選項並完成時,將按預期加載PDF。
當新窗口嘗試加載時,這可能是PDF未被「就緒」的問題嗎?
(這是在IE8中)
可能你或許返回的內容作爲字節數組..?看看這個鏈接,看看這也許可以幫助..http://www.4guysfromrolla.com/articles/030911- – MethodMan 2012-01-09 16:59:33
PDF只是下載也許?如果沒有,放棄PDF寫作部分,並嘗試只寫原始HTML,以確保IE沒有其他問題。 – 2012-01-09 17:50:12
不是 - 它不是下載。我在那裏有一個標誌用於_isExport來確定它是否應該被下載,除此之外,導出和打印是相同的。導出作品如魅力,然而打印不是。 (這也只發生在IE瀏覽器中) – 2012-01-09 17:59:18