代码示例js实现浏览器打印功能


本文摘自PHP中文网,作者coldplay.xixi,侵删。

最近接触到一个新需求,实现打印机打印小票的功能。打的一桌子小票(惭愧),不过也基本满足了业务上的需求,现在分享一下如何实现(好记性不如烂笔头)

先上代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

// 布局代码

<p id="print">

    <p id="print_content"></p>

</p>

//js 部分代码var f = document.getElementById('printf');

   if (f) {

    document.getElementById("print_content").removeChild(f);

   }

   var printhtml = `

   <p style="font-size:12px;margin-left: -6px;">

    <p style="margin-left:40px;">${this.ticket.title}</p>

    <p>--------------------------------------</p>

    <p>提货点:${this.ticket.pickUpAddress}</p>

    <p>商品名称:${this.ticket.commodityName}</p>

    <p>下单时间:${this.ticket.paymentTime}</p>

    <p>提货人:${this.ticket.receiver}</p>

    <p>联系电话:${this.ticket.receiverPhone}</p>

    <p>提货码:${this.ticket.pickUpCode}</p>

    <p>提货时间:${this.ticket.submissionTime}</p>

    <p style="color:#FFFFFF">.</p>

   </p>`

   if (!!window.ActiveXObject || "ActiveXObject" in window) { // 针对IE进行适配

    var HKEY_Root, HKEY_Path, HKEY_Key;

    HKEY_Root = "HKEY_CURRENT_USER";

    HKEY_Path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";

    //设置网页打印的页眉页脚为空

    function PageSetup_Null() {

     var Wsh = new ActiveXObject("WScript.Shell");

     HKEY_Key = "header";

     Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "");

     HKEY_Key = "footer";

     Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "");

     HKEY_Key = "margin_left"

     Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "0"); //键值设定--左边边界

    

     HKEY_Key = "margin_top"

     Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "0"); //键值设定--上边边界

    

     HKEY_Key = "margin_right"

     Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "0"); //键值设定--右边边界

    

     HKEY_Key = "margin_bottom"

     Wsh.RegWrite(HKEY_Root + HKEY_Path + HKEY_Key, "0"); //键值设定--下边边界

    }

    printhtml = `

    <p style="font-size:12px;font-weight: 800;height:150px;width:300px">

     <p style="margin-left:35px">${this.ticket.title}</p>

     <p>------------------------------------------------</p>

     <p>提货点:${this.ticket.pickUpAddress}</p>

     <p>商品名称:${this.ticket.commodityName}</p>

     <p>下单时间:${this.ticket.paymentTime}</p>

     <p>提货人:${this.ticket.receiver}</p>

     <p>联系电话:${this.ticket.receiverPhone}</p>

     <p>提货码:${this.ticket.pickUpCode}</p>

     <p>提货时间:${this.ticket.submissionTime}</p>

     <p style="color:#FFFFFF;font-weight: 100;">.</p>

    </p>`

   }

   var iframe = document.createElement('iframe');

   iframe.id = 'printf';

   iframe.style.width = '0';

   iframe.style.height = '0';

   iframe.style.border = "none";

   document.getElementById("print_content").appendChild(iframe);

   setTimeout(() => {

    iframe.contentDocument.write(printhtml);

    iframe.contentDocument.close();

    iframe.contentWindow.focus();

    iframe.contentWindow.print();

   }, 100)

因为要求不能把打印的数据显示在页面上,所以通过iframe的方式去实现。单纯的截取字符串重新赋值body内容能进行打印却把打印的内容展现在页面中了,所以不行。

阅读剩余部分

相关阅读 >>

javascript将canvas内容转化成图片的方法详解

javascript中的打印方法有几种

bootstrap如何解决浏览器兼容性问题

浏览器不支持javascript怎么办

怎么开启浏览器的cookie设置?

哪些浏览器支持html5

详解html5浏览器兼容性解决方案

javascript怎么实现打印操作

javascript常用的系统函数有哪些

通过手机浏览器打开app或者跳转到下载页面的实现

更多相关阅读请进入《javascrip》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...