Java 如何更改servlet将PDF流式传输到的浏览器页面的标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1058959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:55:11  来源:igfitidea点击:

How to change the title of a browser page which a servlet streamed a PDF to?

javapdfbrowserservletstitlebar

提问by

My Java based webapp has a servlet which streams PDF content back to the browser based on request parameter.

我的基于 Java 的 web 应用程序有一个 servlet,它根据请求参数将 PDF 内容流式传输回浏览器。

e.g. user clicks on an A tag with an href of "myApp/FetchPDFServlet?id=123". Servlet mapping picks up request, streams PDF data to response as mime-type application/pdf, closes flushes buffers.

例如,用户单击带有“myApp/FetchPDFServlet?id=123”href 的 A 标记。Servlet 映射接收请求,将 PDF 数据作为 mime 类型的应用程序/pdf 流式传输到响应,关闭刷新缓冲区。

However the browser title bar for the page displaying the PDF reads "FetchPDFServlet?id=123"

但是,显示 PDF 的页面的浏览器标题栏显示为“FetchPDFServlet?id=123”

How can I change the title the browser displays for the page that displays the PDF? So the browser title is "Here is the amazing PDF" not "FetchPDFServlet?id=123".

如何更改浏览器为显示 PDF 的页面显示的标题?所以浏览器标题是“这是惊人的 PDF”而不是“FetchPDFServlet?id=123”。

Is it possible at all? How best to do this?

有可能吗?如何最好地做到这一点?

回答by Francois Gravel

You could display the PDF in an iframe.
Something like this:

您可以在 iframe 中显示 PDF。
像这样的东西:

<html>
  <head>
      <title>Here is the amazing PDF</title>
      <style type="text/css">
       html, body, div, iframe { margin:0; padding:0; height:100%; }
       iframe { display:block; width:100%; border:none; }
      </style>
  </head>
  <body>
    <iframe width="100%" length="100%" src="myApp/FetchPDFServlet?id=123"/>
  </body>
</html>

So instead of linking to a pdf document using myApp/FetchPDFServlet?id=123, you would link to something that returns the above html. For example, a jsp page: myApp/ShowPDF.jsp?id=123&title=Here%20is%20the%20amazing%20PDF

因此,不是使用 链接到 pdf 文档myApp/FetchPDFServlet?id=123,而是链接到返回上述 html 的内容。例如一个jsp页面:myApp/ShowPDF.jsp?id=123&title=Here%20is%20the%20amazing%20PDF

回答by Gandalf

Add this header to your HttpServletResponse:

将此标头添加到您的 HttpServletResponse 中:

response.setHeader("Content-Disposition","inline; filename=Here is the Amazing PDF");

I believe the browser will pick it up and use it as the title of the window.

我相信浏览器会选择它并将其用作窗口的标题。

回答by Iain

I have hit this issue,and while my solution has quite a few restrictions I thought I would share it.

我遇到了这个问题,虽然我的解决方案有很多限制,但我想我会分享它。

Baiscally I will open the pdf in a new tab, and then change its title from the original page.

Baiscally 我将在新选项卡中打开 pdf,然后从原始页面更改其标题。

$('#aa').click(function(){
    ref = window.open('resume.pdf','mywindow');     
    ref.onload = function(){
        ref.document.title="New Title";
    }
    return false;
    });
  });

Note that the parent and child pages must be in the same domain.

请注意,父页面和子页面必须在同一域中。

I have tested this in a few browsers, and here are the results:

我已经在几个浏览器中测试过,结果如下:

  • Chrome 10 - Works like a treat
  • Safari 5 - Works, but by default opens in a new window not tab. User can change this to open all new windows in tabs.
  • IE 8 - Doesn't work, as expected :op
  • Firefox - Works like a treat
  • Chrome 10 - 像款待一样工作
  • Safari 5 - 有效,但默认情况下在新窗口而不是选项卡中打开。用户可以更改此设置以在选项卡中打开所有新窗口。
  • IE 8 - 不工作,如预期:op
  • Firefox - 像款待一样工作

回答by kunal saxena

I have tried a solution in java and it worked. You can set given headers in other languages

我在 java 中尝试了一个解决方案,它奏效了。您可以设置其他语言的给定标题

response.setHeader("Content-Disposition","inline; filename=\"MyFile.pdf\"");
response.setContentType("application/pdf; name=\"MyFile.pdf\"");
response.getOutputStream().write(pdfAsBytesArray);

回答by Weston Ganger

The tab title is taken from the PDF documents metadata, specifically the Title attribute. So if you generate the document in your app, your PDF library should have a way to set the documents metadata.

选项卡标题取自 PDF 文档元数据,特别是 Title 属性。因此,如果您在应用程序中生成文档,您的 PDF 库应该可以设置文档元数据。