How to Return PDF to Browser in MVC

How to return PDF to browser in MVC?

I got it working with this code.

using iTextSharp.text;
using iTextSharp.text.pdf;

public FileStreamResult pdf()
{
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");
}

Return PDF to the Browser using ASP.NET Core

As explained in ASP.NET Core HTTPRequestMessage returns strange JSON message, ASP.NET Core does not support returning an HttpResponseMessage (what package did you install to get access to that type?).

Because of this, the serializer is simply writing all public properties of the HttpResponseMessage to the output, as it would with any other unsupported response type.

To support custom responses, you must return an IActionResult-implementing type. There's plenty of those. In your case, I'd look into the FileStreamResult:

public IActionResult Get(int id)
{
var stream = new FileStream(@"path\to\file", FileMode.Open);
return new FileStreamResult(stream, "application/pdf");
}

Or simply use a PhysicalFileResult, where the stream is handled for you:

public IActionResult Get(int id)
{
return new PhysicalFileResult(@"path\to\file", "application/pdf");
}

Of course all of this can be simplified using helper methods, such as Controller.File():

public IActionResult Get(int id)
{
var stream = new FileStream(@"path\to\file", FileMode.Open);
return File(stream, "application/pdf", "FileDownloadName.ext");
}

This simply abstracts the creation of a FileContentResult or FileStreamResult (for this overload, the latter).

Or if you're converting an older MVC or Web API application and don't want to convert all your code at once, add a reference to WebApiCompatShim (NuGet) and wrap your current code in a ResponseMessageResult:

public IActionResult Get(int id)
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
var stream = ...
response.Content...

return new ResponseMessageResult(response);
}

If you don't want to use return File(fileName, contentType, fileDownloadName), then the FileStreamResult doesn't support setting the content-disposition header from the constructor or through properties.

In that case you'll have to add that response header to the response yourself before returning the file result:

var contentDisposition = new ContentDispositionHeaderValue("attachment");
contentDisposition.SetHttpFileName("foo.txt");
Response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString();

Return PDF to browser using JSON and MVC?

I feel obligated to post my answer since I didn't hear from anyone. I ended up creating a form that includes a hidden input, then saved my json object in the hidden input and then submit the form. This time I will get input as an string not a json or xml.

var $hidInput = $("#dataToReport"); 
$hidInput.val(JSON.stringify(Screentable));
$('#frmScreenreport').submit();

Thanks all anyways.



Related Topics



Leave a reply



Submit