2

I'm using third-party software to render PDFs from html documents. I created a small test project and using the OnClick event of an <asp:Button> I was able to read a html document from a directory and render it as a PDF with no problems.

The response is created as follows:

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
HttpContext.Current.Response.AddHeader("Content-Disposition", 
    String.Format("{0}; filename=Test.pdf;", "inline" ));
HttpContext.Current.Response.BinaryWrite(pdfBuffer);
HttpContext.Current.ApplicationInstance.CompleteRequest();

When I look at the response in Chrome, I can see that Content-Type is being set correctly:

Content-Disposition:inline; filename=HtmlToPdf.pdf;

Content-Length:101482

Content-Type:application/pdf

I've tried to transfer the above to my current project. The only difference being that instead of one <asp:Button> I'm using a custom button inside a DevExpress grid view. I Initially handled the custom button click in a callback panel, but Content-Type was not being set. Viewing the response in Chrome proved this:

Content-Disposition:inline; filename=5.pdf;

Content-Encoding:gzip

Content-Length:149015

Content-Type:text/plain; charset=utf-8

I've also tried using the gvDocuments.CustomButtonCallback += new ASPxGridViewCustomButtonCallbackEventHandler(gvDocuments_CustomButtonCallback); event but Content-Type is still not being set.

Anyone have any ideas as to why I cannot set Content-Type in the above scenario?

Community
  • 1
  • 1
MattSull
  • 5,514
  • 5
  • 46
  • 68
  • Does the PDF still look OK? It appears that something in IIS's pipeline is applying compression, have you tried disabling that? – Chris Haas Nov 25 '13 at 17:33
  • It doesn't even render. The response is a 200 OK, but Content-Type is text/plain. I'm debugging locally so it's with VS Development Server. Would there be any reason why it shouldn't be getting set in a callback/ grid view custom button click event? – MattSull Nov 25 '13 at 18:40

2 Answers2

6

You can try

HttpResponse response = HttpContext.Current.Response;
response.ClearContent();
response.Clear();

response.ContentType = "application/pdf";

response.AddHeader("Content-Disposition", "attachment; filename=" + yourFileName + ".pdf");
stream.WriteTo(response.OutputStream);
response.Flush();
response.End();

hope it works :)

BMaximus
  • 1,062
  • 1
  • 11
  • 19
0
Dim Resp As HttpResponse = HttpContext.Current.Response
Resp.Headers.Remove("X-Frame-Options")
Resp.AppendHeader("X-Frame-Options", "SAMEORIGIN")

By adding the above lines in the page load/init method will add http response header.