title: iTextSharp | Failing to Output PDF in IE
body: <p>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.</p>
  
  <p>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. <em>(Where as Chrome and Firefox seem to work just fine)</em></p>
  
  <p>I'll go through an overview of the process in hopes of providing some assistance to you all:</p>
  
  <p><strong>Step 1:</strong></p>
  
  <p>The User clicks the "Print" button that I have within a View, and this calls the following Javascript to execute:</p>
  
  <pre><code>window.open($("#PrintURL").val(), 'Print_Preview', 'resizable=1');
  </code></pre>
  
  <p><strong>Step 2:</strong></p>
  
  <p>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:</p>
  
  <pre><code>//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, ...);
  </code></pre>
  
  <p><strong>Step 3:</strong></p>
  
  <p>Within the PDFResult <code>ExecuteResult()</code> method, I build the document that the Report will output, and adjust other parameters, such as PageSize, Headers and Footers.</p>
  
  <pre><code>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();
  }
  </code></pre>
  
  <p>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.)</p>
  
  <p>Any idea/suggestions to improve this process would be greatly appreciated.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <hr>
  
  <p>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:</p>
  
  <pre><code>document.Add(new Phrase("TEST"));
  </code></pre>
  
  <p>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.</p>
  
  <p>Could this be an issue with the PDF not being "ready" when the new window attempts to load it?</p>
  
  <p>(This is in IE8)</p>