0
votes

I want to download pdf when the user clicks on print button.

My controller method:

[System.Web.Http.HttpGet]
        public FileStreamResult CreatePDF()
        {
            MemoryStream workStream = new MemoryStream();
            Document document = new Document();
            PdfWriter.GetInstance(document, workStream).CloseStream = false;

            document.Open();
            document.Add(new Paragraph("Hello World"));
            document.Add(new Paragraph(DateTime.Now.ToString()));
            document.Close();

            byte[] byteInfo = workStream.ToArray();
            workStream.Write(byteInfo, 0, byteInfo.Length);
            workStream.Position = 0;

            return new FileStreamResult(workStream, "application/pdf");
        }

And here's how I am calling it from view:

 $('#print').click(function (evt) {
                window.location.href = "/api/pdfdownload/createpdf";
            });

I am getting the following error upon clicking the print button:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'. System.InvalidOperationException An error has occurred. Error getting value from 'ReadTimeout' on 'System.IO.MemoryStream'. Newtonsoft.Json.JsonSerializationException at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext() An error has occurred. Timeouts are not supported on this stream. System.InvalidOperationException at System.IO.Stream.get_ReadTimeout() at GetReadTimeout(Object ) at Newtonsoft.Json.Serialization.DynamicValueProvider.GetValue(Object target)

I just dont understand what this exception means in this context. Will appreciate your help.

1

1 Answers

0
votes

It means it couldn't serialize your object of type MemoryStream into JSON:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'. System.InvalidOperationException An error has occurred. Error getting value from 'ReadTimeout' on 'System.IO.MemoryStream'. Newtonsoft.Json.JsonSerializationException...

My guess is that you are trying, maybe by intent and maybe not, to serialize MemoryStream into JSON, which is not out of the blue as you are dealing with PDF files, while you cannot do this. MemoryStream contains bytes and cannot be serialized as JSON.

I would suggest you to look at this post from asp.net forums but without anymore information, that is all there is to produce.