“name” web pdf for better default save filename in Acrobat?

后端 未结 16 1720
难免孤独
难免孤独 2020-12-14 00:09

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

相关标签:
16条回答
  • 2020-12-14 00:46

    In ASP.NET 2.0 change the URL from

    http://www. server.com/DocServe.aspx?DocId=XXXXXXX
    

    to

    http://www. server.com/DocServe.aspx/MySaveAsFileName?DocId=XXXXXXX
    

    This works for Acrobat 8 and the default SaveAs filename is now MySaveAsFileName.pdf.

    However, you have to restrict the allowed characters in MySaveAsFileName (no periods, etc.).

    0 讨论(0)
  • 2020-12-14 00:46

    I was redirected here because i have the same problem. I also tried Troy Howard's workaround but it is doesn't seem to work.

    The approach I did on this one is to NO LONGER use response object to write the file on the fly. Since the PDF is already existing on the server, what i did was to redirect my page pointing to that PDF file. Works great.

    http://forums.asp.net/t/143631.aspx

    I hope my vague explanation gave you an idea.

    0 讨论(0)
  • 2020-12-14 00:47

    If you use asp.net, you can control pdf filename through page (url) file name. As other users wrote, Acrobat is a bit s... when it choose the pdf file name when you press "save" button: it takes the page name, removes the extension and add ".pdf". So /foo/bar/GetMyPdf.aspx gives GetMyPdf.pdf.

    The only solution I found is to manage "dynamic" page names through an asp.net handler:

    • create a class that implements IHttpHandler
    • map an handler in web.config bounded to the class

    Mapping1: all pages have a common radix (MyDocument_):

    <httpHandlers>  
    <add verb="*" path="MyDocument_*.ashx" type="ITextMiscWeb.MyDocumentHandler"/>
    

    Mapping2: completely free file name (need a folder in path):

    <add verb="*" path="/CustomName/*.ashx" type="ITextMiscWeb.MyDocumentHandler"/>
    

    Some tips here (the pdf is dynamically created using iTextSharp):
    http://fhtino.blogspot.com/2006/11/how-to-show-or-download-pdf-file-from.html

    0 讨论(0)
  • 2020-12-14 00:49

    Part of the problem is that the relevant RFC 2183 doesn't really state what to do with a disposition type of "inline" and a filename.

    Also, as far as I can tell, the only UA that actually uses the filename for type=inline is Firefox (see test case).

    Finally, it's not obvious that the plugin API actually makes that information available (maybe someboy familiar with the API can elaborate).

    That being said, I have sent a pointer to this question to an Adobe person; maybe the right people will have a look.

    Related: see attempt to clarify Content-Disposition in HTTP in draft-reschke-rfc2183-in-http -- this is early work in progress, feedback appreciated.

    Update: I have added a test case, which seems to indicate that the Acrobat reader plugin doesn't use the response headers (in Firefox), although the plugin API provides access to them.

    0 讨论(0)
  • 2020-12-14 00:52

    Like you, I tried and tried to get this to work. Finally I gave up on this idea, and just opted for a workaround.

    I'm using ASP.NET MVC Framework, so I modified my routes for that controller/action to make sure that the served up PDF file is the last part of the location portion of the URI (before the query string), and pass everything else in the query string.

    Eg:

    Old URI:

    http://server/app/report/showpdf?param1=foo&param2=bar&filename=myreport.pdf

    New URI:

    http://server/app/report/showpdf/myreport.pdf?param1=foo&param2=bar

    The resulting header looks exactly like what you've described (content-type is application/pdf, disposition is inline, filename is uselessly part of the header). Acrobat shows it in the browser window (no save as dialog) and the filename that is auto-populated if a user clicks the Acrobat Save button is the report filename.

    A few considerations:

    In order for the filenames to look decent, they shouldn't have any escaped characters (ie, no spaces, etc)... which is a bit limiting. My filenames are auto-generated in this case, and before had spaces in them, which were showing up as '%20's in the resulting save dialog filename. I just replaced the spaces with underscores, and that worked out.

    This is by no names the best solution, but it does work. It also means that you have to have the filename available to make it part of the original URI, which might mess with your program's workflow. If it's currently being generated or retrieved from a database during the server-side call that generates the PDF, you might need to move the code that generates the filename to javascript as part of a form submission or if it comes from a database make it a quick ajax call to get the filename when building the URL that results in the inlined PDF.

    If you're taking the filename from a user input on a form, then that should be validated not to contain escaped characters, which will annoy users.

    Hope that helps.

    0 讨论(0)
  • 2020-12-14 00:53

    For anyone still looking at this, I used the solution found here and it worked wonderfully. Thanks Fabrizio!

    0 讨论(0)
提交回复
热议问题