Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Specified method is not supported" error with iTextSharp
    text
    copied!<p>I'm using <a href="http://sourceforge.net/projects/itextsharp/" rel="nofollow">iTextSharp</a> to generate PDFs. I've added a test method below that makes a simple page with one paragraph. It works, the PDF is generated, however, sometime after the PDF is sent to the browser I get a NotSupportedException in the Event log (or if I catch them myself from Application_Error). Here's the simplest code that causes this error:</p> <pre><code>public FileStreamResult TestPdf() { using (var ms = new MemoryStream()) { using (var document = new Document()) { PdfWriter.GetInstance(document, ms); document.Open(); document.Add(new Paragraph("Hello World!")); document.Close(); } Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=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>And the error it throws (sometime after the request was completed)</p> <pre><code>Exception information: Exception type: NotSupportedException Exception message: Specified method is not supported. at System.Web.HttpResponseStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Web.Mvc.FileStreamResult.WriteFile(HttpResponseBase response) at System.Web.Mvc.FileResult.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClass1c.&lt;InvokeActionResultWithFilters&gt;b__19() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClass1c.&lt;&gt;c__DisplayClass1e.&lt;InvokeActionResultWithFilters&gt;b__1b() at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) at System.Web.Mvc.Controller.ExecuteCore() at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClass6.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5() at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() at System.Web.Mvc.Async.AsyncResultWrapper.&lt;&gt;c__DisplayClass8`1.&lt;BeginSynchronous&gt;b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() at System.Web.Mvc.MvcHandler.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d() at System.Web.Mvc.SecurityUtil.&lt;GetCallInAppTrustThunk&gt;b__0(Action f) at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) </code></pre> <p>Any ideas on how I could fix this? Is there a problem with the library or something else?</p> <p>Thanks!</p> <p><strong>Edit:</strong></p> <p>To solve this problem, rather than return FileStreamResult I was able to use FileContentResult instead via the Controller's File method. Here's the working code:</p> <pre><code>public ActionResult TestPdf() { using (var ms = new MemoryStream()) { using (var document = new Document()) { PdfWriter.GetInstance(document, ms); document.Open(); document.Add(new Paragraph("Hello World!")); document.Close(); } Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=test.pdf"); Response.Buffer = true; Response.Clear(); return File(ms.ToArray(), "application/pdf"); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload