html和js通过window.print()实现调用浏览器打印带背景图片和生成二维码的文件功能

先上效果图:

打印页面(数据皆为瞎扯):

打印预览:

打印功能是调用window.print()实现;

二维码生成是使用jquery.qrcode插件实现;

完整页面代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Home</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="assets/cssPrint/bootstrap.min.css">
    <link rel="stylesheet" href="assets/cssPrint/font-awesome.min.css">
    <link rel="stylesheet" href="assets/cssPrint/bootstrap-table.min.css">
    <link rel="stylesheet" href="assets/cssPrint/animate.css">
    <link rel="stylesheet" href="assets/cssPrint/newStyle.css">
</head>
<style type="text/css">
    td {
        text-align: center;
        background: white;
        border: 1px solid #000000;
        color: black;
    }

    @media print {
        .print-padding {
            position: absolute;
            padding: 12% 8% 0 3.1%;
            z-index: 10;
        }
    }

    @page {
        margin-top: 0;
        margin-bottom: 0;
    }

    #print_box {
        position: static;
    }

    #print_box > img {
        position: absolute;
        z-index: 5;
        margin: 15px -40px;
    }

    .table {
        margin-bottom: 5px;
    }

    #qrcode {
        position: absolute;
        margin-top: -127px;
        margin-bottom: 0;
    }
</style>
<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <!--startprint-->
    <div class="ibox-content" id="print_box">
        <img src="assets/img/licensebg.png" class="visible-print-block"/>
        <div class="print-padding">
            <div style="float: contour">
                <div style="margin:0 -15px;text-align: center;">
                    <p style="font-family: 宋体; font-weight: bold; font-size: 21pt;color: black;letter-spacing: 0.2em;margin-bottom: 10px;">窝而窝俱乐部</p>
                    <p style="font-family: 宋体;font-weight: bold;font-size: 36pt;color: black;"> 通 行 证</p>
                </div>
            </div>
            <div class="row u3-container" style="margin-bottom: 10px;">
                <div class="col-md-6 col-sm-6 col-xs-6 text-center" style="font-weight: bold;color: black;">许可类型:一般通行许可</div>
                <div class="col-md-6 col-sm-6 col-xs-6 text-center" style="font-weight: bold;color: black;">许可证号:<span>ajax1024</span></div>
            </div>
            <div id="qrcode" class="pull-left">
            </div>
            <div id="contentDiv">
                <table class="table">
                    <tr>
                        <td colspan="2" style="border: 1px solid #000000;">验证网址</td>
                        <td colspan="6" style="border: 1px solid #000000;">https://blog.csdn.net/weixin_36323996/article/details/103480268</td>
                    </tr>
                    <tr>
                        <td colspan="2" style="border: 1px solid #000000;">有效范围</td>
                        <td colspan="6" style="border: 1px solid #000000;">窝而窝俱乐部</td>
                    </tr>
                    <tr>
                        <td colspan="2" style="width: 25%;">通行时间</td>
                        <td colspan="2" style="width: 25%;">2020年10月22日至2020年10月31日</td>
                        <td colspan="2" style="width: 25%;">通行次数</td>
                        <td colspan="2" style="width: 25%;">一次</td>
                    </tr>
                    <tr>
                        <td colspan="2">承运单位</td>
                        <td colspan="6">窝而窝俱乐部</td>
                    </tr>
                    <tr>
                        <td colspan="2">汽车型号</td>
                        <td colspan="2">XC60</td>
                        <td colspan="2">汽车车牌</td>
                        <td colspan="2">窝Ajax1024</td>
                    </tr>
                    <tr>
                        <td colspan="2">车身颜色</td>
                        <td colspan="2">耀目沙金属</td>
                        <td colspan="2">内饰颜色</td>
                        <td colspan="2">米色</td>
                    </tr>
                    <tr>
                        <td colspan="2" style="border: 1px solid #000000;">通行线路</td>
                        <td colspan="6" style="border: 1px solid #000000;">318川藏线、青藏线、滇藏线、丝绸之路河西走廊线、甘南川西线、青海环线、新疆南疆线、四川线、西藏山南线、云贵线。
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2" style="border: 1px solid #000000;">备 注</td>
                        <td colspan="6" style="border: 1px solid #000000;"></td>
                    </tr>
                    <tr>
                        <td rowspan="2" colspan="2">发证单位</td>
                        <td colspan="2">窝而窝俱乐部</td>
                        <td>签 发 人</td>
                        <td colspan="3">窝而窝俱乐部</td>
                    </tr>
                    <tr>
                        <td rowspan="1" colspan="2" style="text-align: right;border-top: none;">(签章)</td>
                        <td>发证时间</td>
                        <td colspan="3">2020年09月27日 11时42分</td>
                    </tr>
                    <tr>
                        <td colspan="2" style="border: 1px solid #000000;">注意事项</td>
                        <td colspan="6" style="border: 1px solid #000000;text-align: left;">
                            <p style="margin-bottom: 2px;">1.申请人必须服从管理人员的检查、鉴定、指挥;</p>
                            <p style="margin-bottom: 2px;">2.必须按照本证规定的时间、路线行驶,不得擅自变更;</p>
                            <p style="margin-bottom: 2px;">3.该证不得涂改、伪造、租借、转让,过期无效,违者按有关规定处理;</p>
                            <p style="margin-bottom: 2px;">4.申请人可在网络自行打印本证;</p>
                            <p>5.扫描本证左上角二维码或登录验证网址输入许可证号实现验证。</p>
                        </td>
                    </tr>
                </table>
                <div class="row">
                    <div class="col-md-12">
                        <div class="pull-left u3-container" style="font-size:12pt;">窝而窝俱乐部监制</div>
                        <div class="pull-right u3-container" style="font-size:12pt;">打印日期:<span id="printTime">2020年09月27日 11时42分</span></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--endprint-->
    <div class="container hidden-print" style="margin-bottom: 15px;padding-right: 0;text-align: right;">
        <input id="protocolCheck" name="checkbox1" type="checkbox" style="vertical-align:top;cursor:pointer;"><span> 我已仔细阅读并接受<a target="_blank"><ins>《免责声明》</ins></a></span>
        <button id="printBtn" type="button" disabled="disabled" class="btn btn-success btn-xs col-md-offset-1">
            <i class="fa fa-print" aria-hidden="true"></i> 打印
        </button>
    </div>
</div>
<!-- /footer -->
<!-- Scripts -->
<script type="text/javascript" src="assets/js/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/bootstrap.min.js"></script>
<script type="text/javascript" src="assets/js/jquery.qrcode.min.js"></script>
<!-- /Scripts -->
<script>
    //打印通行证
    $("#printBtn").click(function () {
        window.print();
    });

    // 免责声明多选框的点击事件
    $("#protocolCheck").click(function () {
        var checked = $(this).prop("checked");
        if (checked) {
            $("#printBtn").removeAttr("disabled");
            $("#saveEnclosure").removeAttr("disabled");
        } else {
            $("#printBtn").attr("disabled", "disabled");
            $("#saveEnclosure").attr("disabled", "disabled");
        }
    });

    //中文编码格式转换
    function utf16to8(str) {
        var out, i, len, c;
        out = "";
        len = str.length;
        for (i = 0; i < len; i++) {
            c = str.charCodeAt(i);
            if ((c >= 0x0001) && (c <= 0x007F)) {
                out += str.charAt(i);
            } else if (c > 0x07FF) {
                out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            } else {
                out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
                out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
            }
        }
        return out;
    }

    //生成二维码
    $("#qrcode").qrcode({
        //render: "table",  渲染方式有table方式(IE兼容)和canvas方式
        width: 100, //宽度
        height: 100, //高度
        text: utf16to8("ajax1024"), //内容
        typeNumber: -1,//计算模式
        correctLevel: 1,//二维码纠错级别
        background: "#ffffff",//背景颜色
        foreground: "#000000"  //二维码颜色

    });
</script>
</body>
</html>

demo已在csdn上传,下载地址:

https://download.csdn.net/download/weixin_36323996/13026257

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现获取二维码图片并打印,可以按照以下步骤: 1. 使用 JavaScript 将二维码图片转换为 base64 编码的字符串。可以使用一个第三方库如 `qrcode.js` 来生成二维码图片并将其转换为 base64 编码。 2. 调用 `window.print()` 方法打印页面,其中二维码图片作为一个 `<img>` 元素插入到页面中。 3. 监听 `window` 对象的 `beforeprint` 和 `afterprint` 事件,以便在打印前和打印后执行相应的操作。 4. 在 `beforeprint` 事件中,可以弹出一个确认框,让用户确认是否要打印二维码图片。如果用户选择取消打印,则可以使用 `event.preventDefault()` 方法取消打印操作。 下面是一个示例代码: ```html <!DOCTYPE html> <html> <head> <title>Print QR Code</title> </head> <body> <h1>Print QR Code</h1> <div id="qrcode"></div> <button id="print-btn">Print</button> <script src="https://cdn.staticfile.org/qrcode-generator/1.4.4/qrcode.min.js"></script> <script> // 生成二维码图片并转换为 base64 编码 var qrcode = new QRCode(document.getElementById("qrcode"), { width: 200, height: 200 }); qrcode.makeCode("https://www.example.com"); var qrimg = document.getElementById("qrcode").getElementsByTagName("img")[0]; var qrdata = qrimg.src.split(",")[1]; // 监听打印事件 window.addEventListener("beforeprint", function(event) { // 弹出确认框,让用户确认是否要打印二维码图片 if (!confirm("Do you want to print the QR code?")) { // 取消打印操作 event.preventDefault(); } }); window.addEventListener("afterprint", function(event) { // 打印完成后的操作 alert("Printing completed."); }); // 绑定打印按钮的点击事件 document.getElementById("print-btn").addEventListener("click", function() { // 插入二维码图片 var img = document.createElement("img"); img.src = "data:image/png;base64," + qrdata; document.body.appendChild(img); // 打印页面 window.print(); }); </script> </body> </html> ``` 在上面的示例中,当用户点击“Print”按钮时,会将二维码图片插入到页面中,并且调用 `window.print()` 方法打印页面。在打印前会弹出一个确认框,让用户确认是否要打印二维码图片。如果用户选择取消打印,则会取消打印操作。在打印完成后,会弹出一个提示框提示打印已完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值