Search code examples
asp.net-corehttpresponsehttpcontextabcpdf

ABCpdf - Download PDF with .NET Core 2.1 - HttpContext/HttpResponse


I'm creating a web page that will allow the user to download a report as a PDF using ABCpdf. But reading the documentation, the only options I see are by using doc.Save("test.pdf") (which saves the file on the server that is hosting the application) or using 'HttpContext.Current.ApplicationInstance.CompleteRequest();' (which saves on the client side, which is what I want, but HttpContext.Current is not available on .NET Core.

The band-aid solution I have is with the doc.Save(), I would save the file on the server then send a link to the view which then downloads it from the server. A potential risk I can think of is making sure to 'clean up' after the download has commenced on the server.

Is there a alternative/.NET Core equivalent for HttpContext.Current and also HttpResponse?

Here is the code that I'd like to make work:

byte[] theData = doc.GetData();
Response.ClearHeaders();
Response.ClearContent();
Response.Expires = -1000;
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", theData.Length.ToString());
Response.AddHeader("content-disposition", "attachment; filename=test.pdf"); 
Response.BinaryWrite(theData);
HttpContext.Current.ApplicationInstance.CompleteRequest();

Errors I get (non-verbose)

'HttpResponse' does not contain a definition for 'ClearHeaders'
'HttpResponse' does not contain a definition for 'ClearContent'
'HttpResponse' does not contain a definition for 'Expires'
'HttpResponse' does not contain a definition for 'AddHeader'
'HttpResponse' does not contain a definition for 'BinaryWrite'
'HttpContext' does not contain a definition for 'Current'

Solution

  • I've updated this answer to something that actually works! GetStream does what you need, however to facilitate a file download in .NET Core it would be far easier if you create a controller as described in https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-2.1. Then you can create a route controller to serve the file from the stream as shown in Return PDF to the Browser using Asp.net core. So your controller would look something like:

    [Route("api/[controller]")]
    public class PDFController : Controller {
    // GET: api/<controller>
        [HttpGet]
        public IActionResult Get() {
            using (Doc theDoc = new Doc()) {
                theDoc.FontSize = 96;
                theDoc.AddText("Hello World");
                Response.Headers.Clear();
                Response.Headers.Add("content-disposition", "attachment; filename=test.pdf");
                return new FileStreamResult(theDoc.GetStream(), "application/pdf");
            }
        }
    }
    

    Out of curiosity I just mocked this up and it does work - serving the PDF direct to the browser as download when you go to the URL localhost:port/api/pdf. If you make the content-disposition "inline; filename=test.pdf" it will show in the browser and be downloadable as test.pdf.

    More information on the GetStream method here: https://www.websupergoo.com/helppdfnet/default.htm?page=source%2F5-abcpdf%2Fdoc%2F1-methods%2Fgetstream.htm