title: Open dynamically generated PDF in Tab or iframe
body: <p>Please help.
  I am obviously no expert but using suggestions from this site, I think I am really close to doing the following</p>
  
  <p>Be able to open a dynamically generated PDF in
  a) a new Tab 
  b) an iframe</p>
  
  <p>Hopefully, I just need a couple of lines of the correct syntax and I will be there.</p>
  
  <p>I am dynamically generating the PDF in a controller using itextSharp</p>
  
  <p>CONTROLLER</p>
  
  <pre><code>public FileStreamResult GetPdf()
    {
      ...
      return new FileStreamResult(Response.OutputStream, "application/pdf"){FileDownloadName = "download.pdf"};
    }
  </code></pre>
  
  <p>VIEW</p>
  
  <pre><code>&lt;input id="btnNewTab" type="button" value="New Tab" /&gt;
  &lt;input id="btnIframe" type="button" value="Iframe" /&gt;
  
  &lt;div id="pdfDiv"&gt;&lt;/div&gt;
  
  &lt;script type="text/javascript"&gt;
    $(function () {
  
      $("#btnIframe").click(function () {
        $.get('/PDFTest/GetPdf', function (pdf) {
          alert(pdf.length);  // Works as expected
          // What do I need to put here to get the pdf to appear in a iframe
        });
      });
  
      $("#btnNewTab").click(function () {
        // asks me if I want to Open or Save the PDF.
        // If I choose Open, the PDF opens in a completely new Window.
        // Instead of the dialog, I just want the PDF to open in a new Tab
        // I am probably going about this in completely the wrong way.
        var HTML = "&lt;iframe src='/PDFTest/GetPdf' style='width: 100%; height: 600px' &gt;&lt;/iframe&gt;";
        $('#pdfDiv').html(HTML);
      });
  
    });
  &lt;/script&gt;
  </code></pre>
  
  <p>In response to your suggestion Darin, I changed the Controller to:</p>
  
  <pre><code>public FileStreamResult GetPdf(someInfo from View)
    {
      ...
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "inline;test.pdf"); 
        Response.Buffer = true;
        Response.Clear();
        Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
        Response.OutputStream.Flush();
        Response.End();
  
        return new FileStreamResult(Response.OutputStream, "application/pdf");
    }
  </code></pre>
  
  <p>Having done that, your suggestions worked fine but I realise that I did not explain my intentions clearly enough. I have therefore changed the VIEW to reflect what i am trying to do.</p>
  
  <pre><code>input id="btnNewTab" type="button" value="New Tab" /&gt;
  &lt;input id="btnIframe" type="button" value="Iframe" /&gt;
  &lt;iframe id="iframe"&gt;&lt;/iframe&gt;
  &lt;div id="pdfDiv"&gt;
  &lt;/div&gt;
  &lt;script type="text/javascript"&gt;
    $(function () {
  
      $("#btnIframe").click(function () {
  
        $.ajax({
          url: '/PDFTest/GetPdf',
          type: "GET",
          data: json,  // This line will not be a problem
          dataType: "json",
          contentType: "application/pdf", // This line might be a problem
          success: function (pdf) {
            // What to I need to need to do to display the PDF in the above iframe
            $("#iframe").attr("src", pdf); // HTTP Error 400 - Bad Request.
          }
        });
      });
  
      $("#btnNewTab").click(function () {
        $.ajax({
          url: '/PDFTest/GetPdf',
          type: "GET",
          data: json,  // This line will not be a problem
          dataType: "json",
          contentType: "application/pdf", // This line might be a problem
          success: function (pdf) {
            // What to I need to need to do to disply the PDF in a new window
          }
        });
      });
  
    });
  &lt;/script&gt;
  </code></pre>