Search code examples
c#asp.netweb-applicationsashxaspx-user-control

How to use web handlers for PDF creation?


I have pretty less knowledge in web handlers. All I know is web handlers are used for creating some dynamic file creation purpose.

And also I know how to add the Web handlers.

But, I need to use web handlers in my ASP.NET project for PDF creation purpose.


Solution

  • You can have a HTTP handler to serve out your PDFs like this:

    public void ProcessRequest (HttpContext context) {
    
        // Get your file as byte[]
        string filename = "....file name.";
        byte[] data = get your PDF file content here;
    
        context.Response.Clear();
        context.Response.AddHeader("Pragma", "public");
        context.Response.AddHeader("Expires", "0");
        context.Response.AddHeader("Content-Type", ContentType);
        context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", filename));
        context.Response.AddHeader("Content-Transfer-Encoding", "binary");
        context.Response.AddHeader("Content-Length", data.Length.ToString());
        context.Response.BinaryWrite(data);
        context.Response.End(); 
    
    }