前端页面生成条形码,借助layui打印标签

借助JsBarcode生成条形码

官网:https://lindell.me/JsBarcode/

github: https://github.com/lindell/JsBarcode

html 复制代码
<div class="table-div" style="display: block;width: 300px; height: 241px; margin: auto;">
    <table border="1" cellspacing="0" cellpadding="3" style=" width: 300px; height: 230px;">
        <tr>
            <td colspan="2" height="80px">
                <div style="text-align: center">
                    <img id="barcode"/>
                </div>
            </td>
        </tr>
        <tr>
            <td colspan="2" height="25px">
                <span style="float: left;position: relative;left:3px;font-size: 16px">开发员:${logistics_id}</span>
            </td>
        </tr>
        <tr>
            <td colspan="2" height="25px">
                <span style="float: left;position: relative;left:3px;font-size: 16px">SQE工程师:${logistics_id}</span>
            </td>
        </tr>
        <tr>
            <td colspan="2" height="25px">
                <span style="float: left;position: relative;left:3px;font-size: 16px"> 取样类型:${logistics_id} </span>
            </td>
        </tr>
        <tr>
            <td colspan="2" height="25px">
                <span style="float: left;position: relative;left:3px;font-size: 16px"> 收货地址:${work_address} </span>
            </td>
        </tr>
        <tr>
            <td colspan="2" height="25px">
                <span style="float: left;position: relative;left:3px;font-size: 16px">建资料流水号:${logistics_id}</span>
            </td>
        </tr>
    </table>
</div>
<script src="https://unpkg.com/jsbarcode@latest/dist/JsBarcode.all.min.js"></script>
<script>
    JsBarcode("#barcode", "212313");
    let test = document.getElementById('barcode').getAttribute('src')
    console.log(test)
</script>

打印代码,使用 window.print() 打印页面上局部的数据

html 复制代码
<script>
function print_labels(developer, sqe_person, sampling_type_display, work_address, pid) {
            layui.use(['form', 'layer', 'laydate', 'table', 'laytpl', 'element'], function () {
                var form = layui.form,
                    layer = layui.layer,
                    $ = layui.jquery,
                    laydate = layui.laydate,
                    laytpl = layui.laytpl,
                    element = layui.element,
                    table = layui.table,
                    formSelects = layui.formSelects;
                let logistics_id = $('input[name="logistics_id"]').val()
                if(!logistics_id){
                    layer.msg("请先填写物流单号!");
                }else{
                    JsBarcode("#barcode", logistics_id);
                    let barcode_res = $('#barcode').attr('src')
                    console.log(barcode_res)
                    var headstr = "<html><head></head><body>";
                    var footstr = "</body>";
                    //获得 print_div   里的所有 html 数据(把要打印的数据嵌套在 一个 div 里,获得 div)
                    //var printData = document.getElementById("print").innerHTML;
                    var printData = `
                    <div class="table-div" style="display: block;width: 300px; height: 241px; margin: auto;">
                        <table border="1" cellspacing="0" cellpadding="3" style=" width: 300px; height: 230px;">
                            <tr>
                                <td colspan="2" height="80px">
                                    <div style="text-align: center">
                                        <img id="barcode" src="${barcode_res}"/>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" height="25px">
                                    <span style="float: left;position: relative;left:3px;font-size: 16px">开发员:${developer}</span>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" height="25px">
                                    <span style="float: left;position: relative;left:3px;font-size: 16px">SQE工程师:${sqe_person}</span>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" height="25px">
                                    <span style="float: left;position: relative;left:3px;font-size: 16px"> 取样类型:${sampling_type_display} </span>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" height="25px">
                                    <span style="float: left;position: relative;left:3px;font-size: 16px"> 收货地址:${work_address} </span>
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" height="25px">
                                    <span style="float: left;position: relative;left:3px;font-size: 16px">建资料流水号:${pid}</span>
                                </td>
                            </tr>
                        </table>
                    </div>
                    `
                    //获取当前页面的html
                    var oldstr = document.body.innerHTML;
                    //把 headstr+printData+footstr 里的数据 复制给 body 的 html 数据 ,相当于重置了 整个页面的 内容
                    document.body.innerHTML = headstr + printData + footstr;
                    //使用方法为 window.print() 打印页面上局部的数据
                    window.print();
                    document.body.innerHTML = oldstr;
                    setTimeout("parent.layer.closeAll(\"iframe\")", 1000);
                    // parent.layer.closeAll("iframe");

                }

            });
        }
</script>
相关推荐
一颗花生米。9 分钟前
深入理解JavaScript 的原型继承
java·开发语言·javascript·原型模式
学习使我快乐0113 分钟前
JS进阶 3——深入面向对象、原型
开发语言·前端·javascript
bobostudio199514 分钟前
TypeScript 设计模式之【策略模式】
前端·javascript·设计模式·typescript·策略模式
勿语&1 小时前
Element-UI Plus 暗黑主题切换及自定义主题色
开发语言·javascript·ui
黄尚圈圈1 小时前
Vue 中引入 ECharts 的详细步骤与示例
前端·vue.js·echarts
浮华似水2 小时前
简洁之道 - React Hook Form
前端
正小安4 小时前
如何在微信小程序中实现分包加载和预下载
前端·微信小程序·小程序
_.Switch6 小时前
Python Web 应用中的 API 网关集成与优化
开发语言·前端·后端·python·架构·log4j
一路向前的月光6 小时前
Vue2中的监听和计算属性的区别
前端·javascript·vue.js
长路 ㅤ   6 小时前
vite学习教程06、vite.config.js配置
前端·vite配置·端口设置·本地开发