title: ASP.NET MVC Generate PDF from byte[]
body: <p>Before posting this message I looked many posts in SO but few where closed outright saying they need to see FAQ, few gave solutions that use iTextSharp or something else. But none solves my issue. My issue is I have a <strong>byte[] and I need to generate a PDF in new child window</strong>. <strong>We are just using ASP.NET MVC 4 and no iTextSharp or similar.</strong> Please let me know if there is already a post that exactly matches this. I am ok to create new Partial Views</p>
  
  <p>I have one PDF Icon image in my Partial View. When user clicks it I need to display PDF in new browser window. I can successfully call a JavaScript function that calls the controller that gets the file from another server. I can even get the file converted to a byte array. I want to show this byte array in PDF format in a new browser window. </p>
  
  <p>In View I have PDF Icon like below</p>
  
  <pre><code>&lt;img onclick="ShowCDinPDF('@Url.Action("ShowPDF", "MyController", new {personid= personid} )','', '920','500')" /&gt;
  </code></pre>
  
  <p>ShowCDinPDF is in my javascript like below</p>
  
  <pre><code>function ShowCDinPDF(popUpURL, windowProperties, w, h) {    
  var childWindow = window.showModelessDialog(popUpURL, "", "");   
  }
  </code></pre>
  
  <p>In my Controller, I have below ShowPDF method</p>
  
  <pre><code>public ActionResult ShowPDF(string personid)
  { 
     //call service and get data
     string fileContent = response.FileContent;
     byte[] data = Convert.FromBase64String(fileContent);
  
     **// Here using data I need to show PDF in new window**
  
  }
  </code></pre>
  
  <p>Please let me know how to create the PDF.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I made little progress. Now my code looks like below. A new window opens and I get error popup with message <strong>File does not begin with '%PDF-'</strong>. I tried to find solution to this but no success.</p>
  
  <pre><code> public ActionResult ShowPDF(string personid)
   { 
       //call service and get data
       string fileContent = response.FileContent;
       byte[] data = Convert.FromBase64String(fileContent);
  
       using (MemoryStream memoryStream = new MemoryStream())
        {
            Response.ClearHeaders();
            Response.ClearContent();
            Response.Charset = "";
            Response.AddHeader("Content-Type", "application/pdf");
            memoryStream.Write(data, 0, data.Length);
            memoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.Close();
            Response.End();
       }
      return View();
   } 
  </code></pre>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I tried alot but no use. Since we were approaching our PROD deadline, our team decided to create PDF file in our server and launch that file in IE browser.</p>
  
  <p><strong>NOTE</strong></p>
  
  <p>I really have no idea why this is down voted. Did anybody can render a file in PDF format without storing/creating the PDF file in any physical location.
  Why down vote?</p>