因为之前被实施商的前端童鞋理直气壮的卡了脖子告诉我写不出来,让我很不爽,所以还是搜集了一些资料最终测试成功了。
注意一下这个只能在IE10及以上的IE内核环境下可用(我只测试了这个环境,但是IE是没跑了,别的浏览器要用IE兼容模式才行),前提是安装了Adobe Reader插件,这个可以去官网下载。
下面就直接甩代码了,没啥可解释的……
<!DOCTYPE html>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
function directpdfprint1() {
try {
var prt = document.getElementById("ob");
prt.printAllFit(1);
} catch (e) {
alert("未安装adobe reader插件,请联系管理员安装!");
}
}
window.onload = function () {
directpdfprint1()
}
</script>
<head>
</head>
<body>
<div>
<OBJECT id="ob" width="0" height="0" border="0" CLASSID="CLSID:CA8A9780-280D-11CF-A24D-444553540000">
<param name="SRC" value="http://localhost:9000/1.pdf">
</OBJECT>
</div>
</body>
上述代码可实现web静默打印(页面加载后自动执行),由于我一直也没有找到动态加载组件src属性的方法,所以选择了将页面由后端直接生成代码,并通过response直接回写,在回写过程中由后端对src进行修改达成动态效果。
@GetMapping("/test")
public void test(HttpServletRequest request , HttpServletResponse response) throws IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.write("<!DOCTYPE html>\n" +
"<script src=\"https://code.jquery.com/jquery-3.1.1.min.js\"></script>\n" +
"<script type=\"text/javascript\">\n" +
" \n" +
"function directpdfprint1() { \n" +
" try { \n" +
" var prt = document.getElementById(\"ob\"); \n" +
" prt.printAllFit(1); \n" +
" } catch (e) { \n" +
" \talert(\"未安装adobe reader插件,请联系管理员安装!\"); \n" +
" }\n" +
"}\n" +
"window.onload = function () {\n" +
" directpdfprint1() \n" +
"} \n" +
"</script>\n" +
"<head>\n" +
"\n" +
"</head>\n" +
"<body> \n" +
"<div>\n" +
" <OBJECT id=\"ob\" width=\"0\" height=\"0\" border=\"0\" CLASSID=\"CLSID:CA8A9780-280D-11CF-A24D-444553540000\">\n" +
" <param name=\"SRC\" value=\"");
//业务中可以动态替换30行数据
out.write("http://localhost:9000/3.pdf");
out.write("\"> \n" +
" </OBJECT>\n" +
"</div>\n" +
"</body>\n" +
"<style type=\"text/css\">\n" +
" .one{\n" +
" transform:rotate(90dge);\n" +
" }\n" +
"</style>");
out.flush();
}
好了,就是这样=。=
注意这个只会使用用户本地设置的默认打印机。
拜拜