I recently encountered an issue regarding outputting a PDF generated in iTextSharp into a new window and haven’t been able to figure out a way to fix the issue.
This problem only seems to occur in IE, and upon the new window opening, it remains blank and doesn’t seem to load the PDF at all. (Where as Chrome and Firefox seem to work just fine)
I’ll go through an overview of the process in hopes of providing some assistance to you all:
Step 1:
The User clicks the “Print” button that I have within a View, and this calls the following Javascript to execute:
window.open($("#PrintURL").val(), 'Print_Preview', 'resizable=1');
Step 2:
The URL that is called pulls the appropriate data into a model and then passes that into a partial view, which is passed into a PDFResult, as shown:
//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, ...);
Step 3:
Within the PDFResult ExecuteResult() method, I build the document that the Report will output, and adjust other parameters, such as PageSize, Headers and 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();
}
I cannot seem to figure out why the new window simply remains blank in Internet Explorer, while a majority of other browsers seem to work just fine. (I know I have had this same process work in IE in the past, I’m just stumped currently.)
Any idea/suggestions to improve this process would be greatly appreciated.
UPDATE
I was testing out some of the changes suggested, and when I removed the areas that would build the document and just added the following:
document.Add(new Phrase("TEST"));
which would add just a single line to the PDF and then generate it. I received an “Internet Explorer cannot display the webpage” error, with the option to Diagnose Connection Problems. When clicking the Diagnose Connection Problems option, and it finishes, the PDF is loaded as intended.
Could this be an issue with the PDF not being “ready” when the new window attempts to load it?
(This is in IE8)
I managed to finally solve this issue. It appears that the window was opening prior to the PDF being ready to be displayed. (This was determined by simply refreshing the window and then the PDF would appear properly.)
So rather than opening the window to a specific URL, I generated a window and opened the URL from within the window, which seemed to fix the issue.)