使用我的spring控制器我想直接打开生成的pdf的打印视图。现在我使用iTextPDF生成一个pdf并将其放到OutputStream(HttpServletResponse.getOutputStream())。它正在下载pdf。我可以在浏览器中打开它并使用打印按钮打印。 我想要的是从控制器获取打印UI或没有打印UI发送到打印机。 我添加了一些控制器方法,
String mimeType = "application/pdf";
System.out.println("MIME type: " + mimeType);
response.setContentType(mimeType);
String headerKey = "Content-Disposition";
String headerValue = String.format("theCoder379.PDF");
response.setHeader(headerKey, headerValue);
OutputStream outStream = response.getOutputStream();
createPdf(outStream, theObject);
outStream.close();
在createPdf(outStream,theObject)方法中,它使用' theObject'添加生成的iText pdf。进入' outStream'。 我怎样才能做到这一点。
答案 0 :(得分:0)
不可能以您想要的方式(从服务器端)。
到目前为止,我正在使用iTextPDF生成pdf并将其放到OutputStream
好。从服务器端代码中可以做到这一切。
我可以在浏览器中打开它并使用打印按钮进行打印 浏览器加载外部应用程序(pdf插件/查看器)并让插件显示您生成的文件。你无法控制它。
但是,您可以使用embedded plugin创建自己的页面。
<object data="./pdfServlet" type="application/pdf" width="100%" height="100%">
<p>Alternative text - include a link <a href="myfile.pdf">to the PDF!</a>
</p>
</object>
并尝试在客户端javascript中调用 print 事件。
window.print();
但是 - 我看到了一些潜在的问题