title: ASP.NET Core: Create and Open PDF (in browser)
body: <p>Working in ASP.NET Core and using iTextSharp, I'm building a PDF and save it to the local system. I now want to open that file in the browser but can't seem to make that work since I get a FileStream error in one try and nothing at all in another try.</p>
  
  <p>My logic is in the controller below. I've replaced the unnecessary code with <code>// region description</code>. The important code (the things I tried) is inside the <code>TODO: Open the file</code> region.</p>
  
  <h1>Controller</h1>
  
  <pre><code>    [HttpPost]
      public (JsonResult, IActionResult) CreatePDF([FromBody] ReportViewModel model)
      {
          try
          {
  
              // region Logic code
              using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
              {
                  // Create the document
                  iTextSharp.text.Document document = new iTextSharp.text.Document();
                  // Place the document in the PDFWriter
                  iTextSharp.text.pdf.PdfWriter PDFWriter =
                      iTextSharp.text.pdf.PdfWriter.GetInstance(document, memoryStream);
  
                  // region Initialize Fonts
  
                  // Open the document
                  document.Open();
  
                  // region Add content to document
  
                  // Close the document
                  document.Close();
  
                  #region Create and Write the file
                  // Create the directory
                  string directory = $"{_settings.Value.ReportDirectory}\\";
                  System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(directory));
  
                  // region Create the fileName
  
                  // Combine the directory and the fileName
                  directory = System.IO.Path.Combine(directory, fileName);
  
                  // Create the file
                  byte[] content = memoryStream.ToArray();
                  using (System.IO.FileStream fs = System.IO.File.Create(directory))
                  {
                      fs.Write(content, 0, (int)content.Length);
                  }
                  #endregion
  
                  #region TODO: Open the file
                  // TRY: File(Stream, type) =&gt; Newtonsoft.Json.JsonSerializationException:
                  // Error getting value from 'ReadTimeout' on 'System.IO.FileStream'.
                  // ---&gt; System.InvalidOperationException: Timeouts are supported for this stream.
                  System.IO.FileStream fileStream = new System.IO.FileStream(directory, System.IO.FileMode.Open);
                  var returnPDF = File(fileStream, contentType: "application/pdf");
  
                  // TRY: File(path, type) =&gt; Newtonsoft.Json.JsonSerializationException:
                  // Error getting value from 'ReadTimeout' on 'System.IO.FileStream'.
                  // ---&gt; System.InvalidOperationException: Timeouts are supported for this stream.
                  returnPDF = File(System.IO.File.OpenRead(directory), contentType: "application/pdf" );
  
                  // TRY: File(byte[], type) =&gt; Nothing happened
                  returnPDF = File(content, contentType: "apllication/pdf");
                  #endregion
  
                  return ( Json(new { isError = false }), returnPDF );
              }
          }
          catch (System.Exception ex)
          {
              Serilog.Log.Error($"ReportController.CreatePDF() - {ex}");
              return ( Json(new { isError = true, errorMessage = ex.Message }), null );
          }
      }
  </code></pre>
  
  <h3>References to useful Stackoverflow answers</h3>
  
  <ul>
  <li><a href="https://stackoverflow.com/a/40488246/6761698">Return PDF to the Browser using Asp.net core</a></li>
  <li><a href="https://stackoverflow.com/a/37465015/6761698">Open PDF in a new tab in browser</a></li>
  <li><a href="https://stackoverflow.com/a/12865689/6761698">Web api controller method giving exception while serializing the Stream object</a></li>
  <li><a href="https://stackoverflow.com/a/43069192/6761698">stream.ReadTimeout threw an exception of type System.InvalidOperationException</a></li>
  </ul>