如何获取PDF内容(通过Spring MVC控制器方法提供)以显示在新窗口中 [英] How to get PDF content (served from a Spring MVC controller method) to appear in a new window

查看:188
本文介绍了如何获取PDF内容(通过Spring MVC控制器方法提供)以显示在新窗口中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring MVC的新手,但其功能给我留下了深刻的印象.

I am a newbie with Spring MVC but I'm quite impressed with its capabilities.

我正在使用3.1.0-RELEASE,我必须显示PDF以响应form:form提交.

I am using 3.1.0-RELEASE and I have to show a PDF in response to form:form submission.

这是我在控制器中编写的(小)代码:

Here is the (small) code I wrote in the controller:

@RequestMapping(value = "new_product", method = RequestMethod.POST, params = "print")
@ResponseBody
public void saveAndShowPDF(ModelMap map, ShippingRequestInfo requestInfo, HttpServletRequest request, HttpServletResponse httpServletResponse) throws IOException {
    saveProductChanges(map, requestInfo, request, httpServletResponse);
    httpServletResponse.setContentType("application/pdf");
    byte[] pdfImage = productService.getPDFImage(requestInfo.getRequestId());
    httpServletResponse.getOutputStream().write(pdfImage);
}

此代码将PDF字节[]发送回原始窗口.

This code sends the PDF byte[] back to the original window.

如何使PDF显示在单独的窗口中,以便仍可以使原始浏览器窗口显示其他内容?最好的方法是使用客户端PDF查看程序(Adobe Reader,FoxIt等)显示PDF,但可以在单独的浏览器窗口中显示PDF.

How do I get the PDF to be shown in a separate window so that I can still have the original browser window to show some other content? The best way would be to have the PDF shown using the client PDF view program (Adobe Reader, FoxIt etc.) but I would be fine with the PDF showing up in a separate browser window.

我决定设置Content-Disposition,以便浏览器打开一个保存/打开框,用户可以在其中打开Adobe(而不会丢失浏览器主页面).

I decided to set the Content-Disposition so that the browser brings up a save/open box where the user can open Adobe (with losing the main browser page).

httpServletResponse.setHeader("Content-Disposition","attachment;filename=cool.pdf");

谢谢大家!

推荐答案

在提交表单的form:form标签中指定target="_blank".

Specify target="_blank" in the form:form tag that submits your form.

这篇关于如何获取PDF内容(通过Spring MVC控制器方法提供)以显示在新窗口中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆