“姓名” web pdf 在 Acrobat 中获得更好的默认保存文件名?
我的应用程序生成 PDF 供用户使用。 “Content-Disposition”http 标头的设置如此处所述。 该值设置为“inline; filename=foo.pdf”,这足以让 Acrobat 在保存 pdf 时将“foo.pdf”作为文件名。
但是,在浏览器嵌入的 Acrobat 中单击“保存”按钮后,要保存的默认名称不是该文件名,而是将斜杠更改为下划线的 URL。 又大又丑。 有没有办法影响 Adobe 中的默认文件名?
URL 中有一个查询字符串,这是不可协商的。 这可能很重要,但在 URL 末尾添加“&foo=/title.pdf”不会影响默认文件名。
更新 2:我已经尝试过这两种
content-disposition inline; filename=foo.pdf
Content-Type application/pdf; filename=foo.pdf
方法
content-disposition inline; filename=foo.pdf
Content-Type application/pdf; name=foo.pdf
(通过 Firebug 验证),但遗憾的是,两者都不起作用。
示例 url
/bar/sessions/958d8a22-0/views/1493881172/export?format=application/pdf&no-attachment=true
会转换为
http___localhost_bar_sessions_958d8a22-0_views_1493881172_export_format=application_pdf&no-attachment=true.pdf
更新 3 的默认 Acrobat 保存文件名:Julian Reschke 对此案例带来了实际的洞察力和严谨性。 请为他的回答点赞。 这似乎在 FF 中被破坏了(https://bugzilla.mozilla.org/show_bug。 cgi?id=433613) 和 IE,但可以在 Opera、Safari 和 Chrome 中使用。 http://greenbytes.de/tech/tc2231/#inlwithasciifilenamepdf
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
如果您使用asp.net,您可以通过页面(url)文件名控制pdf文件名。
正如其他用户所写,Acrobat 有点...当您按下“保存”按钮时选择 pdf 文件名:它获取页面名称,删除扩展名并添加“.pdf”。
因此 /foo/bar/GetMyPdf.aspx 给出 GetMyPdf.pdf。
我找到的唯一解决方案是通过 asp.net 处理程序管理“动态”页面名称:
Mapping1:所有页面都有一个公共基数 (MyDocument_):
Mapping2 :完全自由的文件名(路径中需要一个文件夹):
这里有一些提示(pdf 是使用 iTextSharp 动态创建的):
http://fhtino。 blogspot.com/2006/11/how-to-show-or-download-pdf-file-from.html
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:
Mapping1: all pages have a common radix (MyDocument_):
Mapping2: completely free file name (need a folder in path):
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
您可以尝试内联而不是附件:
我在以前的 Web 应用程序中使用了内联,该应用程序将 Crystal Reports 输出生成为 PDF 并在浏览器中将其发送给用户。
Instead of attachment you can try inline:
I used inline in a previous web application that generated Crystal Reports output into PDF and sent that in browser to the user.
带有保存和打开选项的文件下载对话框 (PDF)
需要记住的要点:
这是读取流的代码并打开 PDF 文件的文件下载对话框
File download dialog (PDF) with save and open option
Points To Remember:
Here is the code for read stream and open the File download dialog for PDF file
我相信这已经以一种或另一种方式提到过,但我会尝试用我自己的话来陈述它。
而不是这样:
我使用这个:
当请求传入时,我在 URL 中查找GeneratePDF=1,而不是让“导出”处理请求。 如果找到,我会运行“导出”中运行的任何代码,而不是让我的系统尝试在位置
/bar/sessions/958d8a22-0/views/1493881172/NameThatIWantPDFToBe.pdf. 如果在 URL 中找不到GeneratePDF,我只需传输所请求的文件。 (请注意,我不能简单地重定向到所请求的文件 - 否则我会陷入无限循环)
I believe this has already been mentioned in one flavor or another but I'll try and state it in my own words.
Rather than this:
I use this:
Rather than having "export" process the request, when a request comes in, I look in the URL for GeneratePDF=1. If found, I run whatever code was running in "export" rather than allowing my system to attempt to search and serve a PDF in the location
/bar/sessions/958d8a22-0/views/1493881172/NameThatIWantPDFToBe.pdf
. If GeneratePDF is not found in the URL, I simply transmit the file requested. (note that I can't simply redirect to the file requested - or else I'd end up in an endless loop)你总是可以有两个链接。 一个在浏览器中打开文档,另一个用于下载文档(使用不正确的内容类型)。 这就是 Gmail 的作用。
You could always have two links. One that opens the document inside the browser, and another to download it (using an incorrect content type). This is what Gmail does.
对于仍在关注此问题的人,我使用了此处找到的解决方案,效果非常好。 谢谢法布里齐奥!
For anyone still looking at this, I used the solution found here and it worked wonderfully. Thanks Fabrizio!
我解决这个问题的方法(使用 PHP)如下:
假设您的 URL 是
SomeScript.php?id=ID&data=DATA
并且您要使用的文件是TEST.pdf< /代码>。
将 URL 更改为
SomeScript.php/id/ID/data/DATA/EXT/TEST.pdf
。重要的是,最后一个参数是您希望 Adobe 使用的文件名(“EXT”可以是任何内容)。 顺便说一句,请确保上面的字符串中没有特殊字符。
现在,在
SomeScript.php
顶部添加:然后将此函数添加到
SomeScript.php
(或您的函数库):现在
$_REQUEST
(或$_GET
如果您愿意)像正常的$_REQUEST['id']
、$_REQUEST['data']
一样访问, 当您内联发送时,Adobe 将使用您所需的文件名作为默认另存为或电子邮件信息。
The way I solved this (with PHP) is as follows:
Suppose your URL is
SomeScript.php?id=ID&data=DATA
and the file you want to use isTEST.pdf
.Change the URL to
SomeScript.php/id/ID/data/DATA/EXT/TEST.pdf
.It's important that the last parameter is the file name you want Adobe to use (the 'EXT' can be about anything). Make sure there are no special chars in the above string, BTW.
Now, at the top of
SomeScript.php
, add:Then add this function to
SomeScript.php
(or your function library):Now
$_REQUEST
(or$_GET
if you prefer) is accessed like normal$_REQUEST['id']
,$_REQUEST['data']
, etc.And Adobe will use your desired file name as the default save as or email info when you send it inline.
我被重定向到这里,因为我有同样的问题。 我也尝试了 Troy Howard 的解决方法,但它似乎不起作用。
我对此所做的方法是不再使用响应对象来动态写入文件。 由于 PDF 已经存在于服务器上,因此我所做的是将页面重定向到该 PDF 文件。 效果很好。
http://forums.asp.net/t/143631.aspx
我希望我的模糊的解释给了你一个想法。
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.
归功于 Vivek。
Nginx
检查
使用Firefox 62.0b5(64 位)
:好的。 Chrome 67.0.3396.99(64 位):好的。
IE 11:没有评论。
Credits to Vivek.
Nginx
Check with
Firefox 62.0b5 (64-bit): OK.
Chrome 67.0.3396.99 (64-Bit): OK.
IE 11: No comment.
如果您的可执行文件是“get.cgi”,请尝试此操作
http:// server,org/get.cgi/filename.pdf?file=filename.pdf
是的,这完全是疯了。 服务器上没有名为“filename.pdf”的文件,可执行文件 get.cgi 下根本有目录。
但这似乎有效。 服务器忽略 filename.pdf 并且 pdf 阅读器忽略“get.cgi”
Dan
Try this, if your executable is "get.cgi"
http://server,org/get.cgi/filename.pdf?file=filename.pdf
Yes, it's completely insane. There is no file called "filename.pdf" on the server, there is directory at all under the executable get.cgi.
But it seems to work. The server ignores the filename.pdf and the pdf reader ignores the "get.cgi"
Dan
部分问题在于相关的 RFC 2183 并没有真正说明如何处理“内联”的处置类型和文件名。
另外,据我所知,唯一真正使用 type=inline 文件名的 UA 是 Firefox(参见 测试用例)。
最后,插件 API 是否确实使该信息可用并不明显(也许熟悉该 API 的人可以详细说明)。
话虽这么说,我已将这个问题的指针发送给 Adobe 人员; 也许合适的人会看看。
相关:请参阅 草案中澄清 HTTP 中的内容处置的尝试-reschke-rfc2183-in-http——这是正在进行的早期工作,感谢反馈。
更新:我添加了一个 测试用例,这似乎表明 Acrobat reader 插件不'不要使用响应标头(在 Firefox 中),尽管插件 API 提供了对它们的访问。
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.
还要在 ContentType 中设置文件名。 这应该可以解决问题。
设置内容处置标头后,还要添加内容长度标头,然后使用 binarywrite 流式传输 PDF。
Set the file name in ContentType as well. This should solve the problem.
After you set content-disposition header, also add content-length header, then use binarywrite to stream the PDF.
和你一样,我也尝试过并试图让它发挥作用。 最后我放弃了这个想法,只是选择了一种解决方法。
我正在使用 ASP.NET MVC 框架,因此我修改了该控制器/操作的路由,以确保提供的 PDF 文件是 URI 位置部分的最后一部分(在查询字符串之前),并传递所有内容else 在查询字符串中。
例如:
旧 URI:
http://server/app/ report/showpdf?param1=foo¶m2=bar&filename=myreport.pdf
新 URI:
http://server/app/report/showpdf/myreport.pdf?param1=foo¶m2=bar
生成的标头看起来与您所描述的完全一样(内容-类型是 application/pdf,配置是内联的,文件名是标题的无用部分)。 Acrobat 将其显示在浏览器窗口中(无“另存为”对话框),并且如果用户单击“Acrobat 保存”按钮,则自动填充的文件名就是报表文件名。
一些注意事项:
为了使文件名看起来像样,它们不应该有任何转义字符(即没有空格等)......这有点限制。 在这种情况下,我的文件名是自动生成的,之前有空格,在生成的保存对话框文件名中显示为“%20”。 我只是用下划线替换了空格,就成功了。
这绝对不是最好的解决方案,但它确实有效。 它还意味着您必须拥有可用的文件名才能使其成为原始 URI 的一部分,这可能会扰乱程序的工作流程。 如果当前正在生成 PDF 的服务器端调用期间从数据库中生成或检索它,则您可能需要将生成文件名的代码移动到 javascript 作为表单提交的一部分,或者如果它来自数据库,则将其作为表单提交的一部分。在构建生成内联 PDF 的 URL 时,快速使用 ajax 调用来获取文件名。
如果您从表单上的用户输入中获取文件名,则应验证该文件名不包含转义字符,这会惹恼用户。
希望有帮助。
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¶m2=bar&filename=myreport.pdf
New URI:
http://server/app/report/showpdf/myreport.pdf?param1=foo¶m2=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.
尝试将文件名放在 URL 末尾、任何其他参数之前。 这对我有用。
http://www.setasign.de/support/提示和技巧/浏览器插件中的文件名/
Try placing the file name at the end of the URL, before any other parameters. This worked for me.
http://www.setasign.de/support/tips-and-tricks/filename-in-browser-plugin/
在 ASP.NET 2.0 中,将 URL 从 更改
为
这适用于 Acrobat 8,默认的另存为文件名现在为
MySaveAsFileName.pdf
。但是,您必须限制
MySaveAsFileName
中允许的字符(无句点等)。In ASP.NET 2.0 change the URL from
to
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.).Apache 的
mod_rewrite
可以解决这个问题。我有一个 Web 服务,其端点位于
/foo/getDoc.service
。 当然,Acrobat 会将文件另存为getDoc.pdf
。 我在 apache.conf 中添加了以下几行:现在,当我请求
/foo/getDoc/filename.pdf?bar&qux
时,它会在内部重写为/ foo/getDoc.service?bar&qux
,因此我正在访问 Web 服务的正确端点,但 Acrobat 认为它将我的文件另存为filename.pdf
。Apache's
mod_rewrite
can solve this.I have a web service with an endpoint at
/foo/getDoc.service
. Of course Acrobat will save files asgetDoc.pdf
. I added the following lines inapache.conf
:Now when I request
/foo/getDoc/filename.pdf?bar&qux
, it gets internally rewritten to/foo/getDoc.service?bar&qux
, so I'm hitting the correct endpoint of the web service, but Acrobat thinks it will save my file asfilename.pdf
.