javascript 生成pdf后如何打开打印对话框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8733276/
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
How to open print dialog after pdf generated?
提问by Yichaoz
I wrote some actions which generates dynamically PDF files. something like: reports/reportGenerator.action
我写了一些动态生成PDF文件的动作。类似:reports/reportGenerator.action
when I call reports/reportGenerator?param=dialy
it will open the generated pdf in a popup, and then I can press the print button to open the printer dialog and print it.
当我调用reports/reportGenerator?param=dialy
它时会在弹出窗口中打开生成的pdf,然后我可以按打印按钮打开打印机对话框并打印它。
what I want to do now is to open the printer dialog directly. so when I call reports/reportGenerator?param=dialy
and after the pdf file is fully generated, open the printer dialog. is that possible? (I know the printer dialog can not be skipped)
我现在想做的是直接打开打印机对话框。因此,当我调用reports/reportGenerator?param=dialy
并完全生成 pdf 文件后,打开打印机对话框。那可能吗?(我知道打印机对话框不能跳过)
more info: action is something like this:
更多信息:动作是这样的:
sout = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-disposition","inline; filename=\"myReport.pdf\"");
sout.write(pdfBytes);
sout.flush();
sout.close();
回答by Yichaoz
Thanks to Alex K I found the answer:
感谢 Alex KI 找到了答案:
according to JRPdfExporterParameter.html#PDF_JAVASCRIPT
根据JRpdfExporterParameter.html#PDF_JAVASCRIPT
you can use PDF_JAVASCRIPT
property to add javascript to the pdf when you generate it.
您可以PDF_JAVASCRIPT
在生成 pdf 时使用属性将 javascript 添加到 pdf 中。
so I added
所以我补充说
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRPdfExporterParameter.PDF_JAVASCRIPT, "this.print();");
when exporting PDF and it worked
导出 PDF 时它起作用了
回答by Yichaoz
The solution is simple, add property to your jrxml
file by clicking right on your jrxml
in the iReport designer and press properties then press add :
解决方案很简单,jrxml
通过jrxml
在 iReport 设计器中右键单击您的文件,然后按属性,然后按添加,将属性添加到您的文件中:
Property name
物业名称
net.sf.jasperreports.export.pdf.javascript
Property value
适当的价值
this.print({bUI: true,bSilent: true,bShrinkToFit: false});
This property will print automatically in the client side, I'd add it and it work perfectly.
此属性将在客户端自动打印,我会添加它并且它可以完美运行。
回答by Prafful Panwar
Add this script in your loadView blade file (PDF blade file)
在您的 loadView 刀片文件(PDF 刀片文件)中添加此脚本
<script type="text/javascript"> try { this.print(); } catch (e) { window.onload = window.print; } </script>