前端页面生成条形码,借助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>
相关推荐
方方洛12 分钟前
电子书阅读器:epub电子书文件的解析
前端·产品·电子书
idaibin13 分钟前
Rustzen Admin 前端简单权限系统设计与实现
前端·react.js
GISer_Jinger19 分钟前
Trae Solo模式生成一个旅行足迹App
前端·javascript
zhangbao90s20 分钟前
Intl API:浏览器原生国际化API入门指南
前端·javascript·html
艾小码22 分钟前
构建现代前端工程:Webpack/Vite/Rollup配置解析与最佳实践
前端·webpack·node.js
跟橙姐学代码27 分钟前
Python 集合:人生中最简单的真理,只有一次
前端·python·ipython
复苏季风29 分钟前
站在2025 年 来看,现在应该怎么入门CSS
前端·css
pepedd86430 分钟前
深度解剖 Vue3 架构:编译时 + 运行时的协作
前端·vue.js·trae
一枚前端小能手32 分钟前
🧪 改个代码就出Bug的恐惧,前端测试来帮忙
前端·测试
s3xysteak33 分钟前
我要成为vue高手02:数据传递
前端·javascript·vue.js