"name" web pdf for better default save filename in Acrobat?

James Baker picture James Baker · Sep 30, 2008 · Viewed 39.2k times · Source

My app generates PDFs for user consumption. The "Content-Disposition" http header is set as mentioned here. This is set to "inline; filename=foo.pdf", which should be enough for Acrobat to give "foo.pdf" as the filename when saving the pdf.

However, upon clicking the "Save" button in the browser-embedded Acrobat, the default name to save is not that filename but instead the URL with slashes changed to underscores. Huge and ugly. Is there a way to affect this default filename in Adobe?

There IS a query string in the URLs, and this is non-negotiable. This may be significant, but adding a "&foo=/title.pdf" to the end of the URL doesn't affect the default filename.

Update 2: I've tried both

content-disposition  inline; filename=foo.pdf
Content-Type         application/pdf; filename=foo.pdf

and

content-disposition  inline; filename=foo.pdf
Content-Type         application/pdf; name=foo.pdf

(as verified through Firebug) Sadly, neither worked.

A sample url is

/bar/sessions/958d8a22-0/views/1493881172/export?format=application/pdf&no-attachment=true

which translates to a default Acrobat save as filename of

http___localhost_bar_sessions_958d8a22-0_views_1493881172_export_format=application_pdf&no-attachment=true.pdf

Update 3: Julian Reschke brings actual insight and rigor to this case. Please upvote his answer. This seems to be broken in FF (https://bugzilla.mozilla.org/show_bug.cgi?id=433613) and IE but work in Opera, Safari, and Chrome. http://greenbytes.de/tech/tc2231/#inlwithasciifilenamepdf

Answer

Vivek picture Vivek · Sep 30, 2008

Set the file name in ContentType as well. This should solve the problem.

context.Response.ContentType = "application/pdf; name=" + fileName;
// the usual stuff
context.Response.AddHeader("content-disposition", "inline; filename=" + fileName);

After you set content-disposition header, also add content-length header, then use binarywrite to stream the PDF.

context.Response.AddHeader("Content-Length", fileBytes.Length.ToString());
context.Response.BinaryWrite(fileBytes);