使用Window.print打印HTML页面

时间:2016-08-05 10:31:32

标签: javascript jquery html css angularjs

我一直试图让这件事做得很久。我有一个带有少量图标的HTML页面,点击打印后我想要打印包含图标的页面。

我根据Print specific div

中建议的解决方案做了

但在此我无法加载图标,页面格式也变得混乱。有关如何在点击时打印页面的任何建议,而无需使用已经格式化/样式化的页面。

该应用程序有一个cshtml文件,其中包含样式,引用的图标在我创建的每个html页面中都不需要引用。因此,当我调用window.print()时,样式未加载为我想要打印的特定div,不会加载要求操作的图标。我的问题是在这种情况下,为整个应用程序定义一次样式,如何调用window.print()

1 个答案:

答案 0 :(得分:2)

var win = window.open('','printwindow');
    win.document.write('<html><head><title>Print it!</title><link rel="stylesheet" href="http://localhost:8080/fin/pcd/css/printlib.css" type="text/css" /><link rel="stylesheet" href="css/main.css" type="text/css" /></head><body>');
    win.document.write($("#"+divid+"key").html());
    win.document.write($("#"+divid2+"line").html());
    win.document.write('<div class="testPrint"></div><script>;</script></body></html>');
    win.print();

使用上面你可以打印特定的div并导入css,你可以将必要的样式(包括你的图标)应用到div。

添加了更多信息:

查看以下工作示例链接,您会有所了解:https://plnkr.co/edit/f1JkCgYvI10rsdm5msmM?p=preview

注意:如果为要打印的页面的css提供了background-color,那么只有在为background graphics启用chrome时,相关属性才有效其他浏览器不需要。

Check below image on how to enable background graphics in chrome:

enter image description here