vue使用vue-plugin-hiprint打印(代码模式)最简单!!最实用!!!

该文章已生成可运行项目,

vue-plugin-hiprint这个打印是一个拖拽模式下的打印,基于hiprint打印二次封装的,但上面一些功能也能继续使用

这个打印最方便的是拖拽成自己想要的样式,并生成一个模版JSON,然后在对应的id赋值就可以了,如果你刚接触就先看原版文章

他可以生成模版json,后续更改可以根据模版来拖拽就行

红框里的就是你生成的模版,也是你要打印的样式,数据要自己写(数据要和json中的id对应)

首先需要先进行下载插件

npm install vue-plugin-hiprint
<!--【必须】在index.html 文件中添加打印所需样式(cdn可能不稳定):-->
<link
  rel="stylesheet"
  type="text/css"
  media="print"
  href="https://npmmirror.com/package/vue-plugin-hiprint/files/dist/print-lock.css"
/>
<!-- OR -->
<link
  rel="stylesheet"
  type="text/css"
  media="print"
  href="https://cdn.jsdelivr.net/npm/vue-plugin-hiprint@latest/dist/print-lock.css"
/>
<!-- 可以调整成 相对链接/自有链接, 【重要】名称需要一致 【print-lock.css】-->
<link rel="stylesheet" type="text/css" media="print" href="/print-lock.css" />

这个css以及后来用到的io.js都是 在这里   下载  的

配置完成后需要再main.js引入

// 新打印的插件
import { hiPrintPlugin } from "vue-plugin-hiprint";
Vue.use(hiPrintPlugin, "$pluginName"); // $pluginName 为自定义名称
hiPrintPlugin.disAutoConnect();

引入之后,就可以先做你的打印模版  模版制作

做好之后会有一个查看JSON模版,然后需要把json复制保存

做完之后,就可以直接在组件内引用

首先在组件引入所需的打印方法


import {
    hiprint,
    defaultElementTypeProvider,
    disAutoConnect,
    autoConnect,
} from "vue-plugin-hiprint";
import { io } from "socket.io-client";

并在data中定义

 hiprintTemplate: null,  //接受打印的
            printObj: {
                         //打印的模版JSON  就是你拖拽的
                         }

然后就定义方法直接使用

 printRender() {
            let data = {
                headTitle: "标题",
                name: this.ybMisPayInfo.hospital,
                merchantNumber: this.ybMisPayInfo.merchantno,
                orderId: this.ybMisPayInfo.terminalno,
                startTime: this.ybMisPayInfo.startTransDate,
                endTime: this.ybMisPayInfo.endTransDate,
                total: this.totalCount1,
                allNum: this.totalAmount1,
                table: this.tableData
            }


            // 下列方法都是没有拖拽设计页面的, 相当于代码模式, 使用代码设计页面
            var hiprintTemplate = new hiprint.PrintTemplate({
                template: this.printObj, // 模板json [对象]
                settingContainer: "#templateDesignDiv",
            });
            //打印
            hiprintTemplate.print(data);
          
        },

这个是预览打印的,如果需要直接打印则需要把print改为 print2

再需要安装一个打印控件  控件地址

下载dome然后打包安装运行

连接成功是绿色,未连接是蓝色

链接的代码是

hiprint.init({
  ...otherOptions,
  host: "http://localhost:17521", // 桌面客户端地址
  token: "vue-plugin-hiprint", // 客户端设置的令牌
});


或者

hiwebSocket.setHost(
  "http://localhost:17521",
  "vue-plugin-hiprint",
  // callback
  (connect) => {
    if (connect) {
      console.log("连接成功");
      // do something after connected
    } else {
      console.log("连接失败");
    }
  }
);

链接一般写在mounted里面

可以参考一下我这个

  // 打印创建
        printCreate() {
            const socket = io("http://192.168.1.17:17521", {
                transports: ["websocket"],
                auth: {
                    token: "vue-plugin-hiprint",
                },
            });

            socket.on("connect", () => {
                globalThis.connect = true;
                // TODO: Do something for your project
            });
            hiwebSocket.setHost(
                "http://localhost:17521",
                "vue-plugin-hiprint",
                // callback
                (connect) => {
                    if (connect) {
                        console.log("连接成功");
                        // do something after connected
                    } else {
                        console.log("连接失败");
                    }
                }
            );
        },
        printRender() {
            let data = {
                headTitle: "MIS省医保卡刷卡汇总",
                name: this.ybMisPayInfo.hospital,
                merchantNumber: this.ybMisPayInfo.merchantno,
                orderId: this.ybMisPayInfo.terminalno,
                startTime: this.ybMisPayInfo.startTransDate,
                endTime: this.ybMisPayInfo.endTransDate,
                total: this.totalCount1,
                allNum: this.totalAmount1,
                table: this.tableData
            }


            // 下列方法都是没有拖拽设计页面的, 相当于代码模式, 使用代码设计页面
            var hiprintTemplate = new hiprint.PrintTemplate({
                template: this.printObj, // 模板json [对象]
                settingContainer: "#templateDesignDiv",
            });
            //打印
            hiprintTemplate.print2(data,{printer: "GP-U80300 Series"});
            hiprintTemplate.on("printSuccess", (res) => {
                console.log("打印成功", res);
            });
            hiprintTemplate.on("printError", (err) => {
                console.error("打印失败", err);
            });
        },

打印的模版

 {
                "panels": [
                    {
                        "index": 0,
                        "name": 1,
                        "height": null,
                        "width": 78,
                        "paperHeader": 0,
                        "panelPageRule": "none",
                        "paperFooter": 340.15748031496065,
                        "printElements": [
                            {
                                "options": {
                                    "left": 4.5,
                                    "top": 127.5,
                                    "height": 37.5,
                                    "width": 207,
                                    "tableFooterRepeat": "no",
                                    "fields": [
                                        {
                                            "text": "名称",
                                            "field": "transType",

                                        },
                                        {
                                            "text": "数量",
                                            "field": "count"
                                        },
                                        {
                                            "text": "规格",
                                            "field": "GG"
                                        },
                                        {
                                            "text": "条码",
                                            "field": "TM"
                                        },
                                        {
                                            "text": "单价",
                                            "field": "DJ"
                                        },
                                        {
                                            "text": "金额",
                                            "field": "amount"
                                        }
                                    ],
                                    "field": "table",
                                    "tableHeaderRepeat": "first",
                                    "qid": "table",
                                    "coordinateSync": false,
                                    "widthHeightSync": false,
                                    "right": 216,
                                    "bottom": 142.5,
                                    "vCenter": 108.75,
                                    "hCenter": 115.5,
                                    "columns": [
                                        [
                                            {
                                                "width": 61.42460960921844,
                                                "title": "类型",
                                                "field": "transType",

                                                "checked": true,
                                                "columnId": "transType",
                                                "fixed": false,
                                                "rowspan": 1,
                                                "colspan": 1,
                                                "align": "center",
                                                "tableQRCodeLevel": 0,
                                                "tableSummaryTitle": true,
                                                "tableSummary": ""
                                            },
                                            {
                                                "width": 62.6094585270541,
                                                "title": "数量",
                                                "field": "count",
                                                "checked": true,
                                                "columnId": "count",
                                                "fixed": false,
                                                "rowspan": 1,
                                                "colspan": 1,
                                                "align": "center"
                                            },
                                            {
                                                "width": 82.96593186372746,
                                                "title": "金额",
                                                "field": "amount",
                                                "checked": true,
                                                "columnId": "amount",
                                                "fixed": false,
                                                "rowspan": 1,
                                                "colspan": 1,
                                                "align": "center"
                                            },
                                            {
                                                "width": 80,
                                                "title": "规格",
                                                "field": "GG",
                                                "checked": false,
                                                "columnId": "GG",
                                                "fixed": false,
                                                "rowspan": 1,
                                                "colspan": 1,
                                                "align": "center"
                                            },
                                            {
                                                "width": 100,
                                                "title": "条码",
                                                "field": "TM",
                                                "checked": false,
                                                "columnId": "TM",
                                                "fixed": false,
                                                "rowspan": 1,
                                                "colspan": 1,
                                                "align": "center"
                                            },
                                            {
                                                "width": 100,
                                                "title": "单价",
                                                "field": "DJ",
                                                "checked": false,
                                                "columnId": "DJ",
                                                "fixed": false,
                                                "rowspan": 1,
                                                "colspan": 1,
                                                "align": "center"
                                            }
                                        ]
                                    ]
                                },
                                "printElementType": {
                                    "title": "订单数据",
                                    "type": "table",
                                    "editable": true,
                                    "columnDisplayEditable": true,
                                    "columnDisplayIndexEditable": true,
                                    "columnTitleEditable": true,
                                    "columnResizable": true,
                                    "columnAlignEditable": true,
                                    "isEnableEditField": true,
                                    "isEnableContextMenu": true,
                                    "isEnableInsertRow": true,
                                    "isEnableDeleteRow": true,
                                    "isEnableInsertColumn": true,
                                    "isEnableDeleteColumn": true,
                                    "isEnableMergeCell": true
                                }
                            },
                            {
                                "options": {
                                    "left": 6,
                                    "top": 168,
                                    "height": 16,
                                    "width": 43.5,
                                    "title": "总计",
                                    "fontSize": 12.75,
                                    "fontWeight": "700",
                                    "textAlign": "left",
                                    "textContentVerticalAlign": "middle",
                                    "right": 52.5,
                                    "bottom": 183.25,
                                    "vCenter": 30.75,
                                    "hCenter": 175.25,
                                    "coordinateSync": false,
                                    "widthHeightSync": false,
                                    "qrCodeLevel": 0
                                },
                                "printElementType": {
                                    "title": "库管签字",
                                    "type": "text"
                                }
                            },
                            {
                                "options": {
                                    "left": 24,
                                    "top": 3,
                                    "height": 17,
                                    "width": 178.5,
                                    "testData": "刷卡,
                                    "fontSize": 16.5,
                                    "field": "headTitle",
                                    "fontWeight": "700",
                                    "textAlign": "center",
                                    "hideTitle": true,
                                    "title": "刷卡",
                                    "qid": "headTitle",
                                    "coordinateSync": false,
                                    "widthHeightSync": false,
                                    "qrCodeLevel": 0,
                                    "right": 204,
                                    "bottom": 19.25,
                                    "vCenter": 114.75,
                                    "hCenter": 10.75
                                },
                                "printElementType": {
                                    "title": "单据表头",
                                    "type": "text"
                                }
                            },
                            {
                                "options": {
                                    "left": 82.5,
                                    "top": 171,
                                    "height": 9.75,
                                    "width": 60,
                                    "title": "文本",
                                    "right": 142.5,
                                    "bottom": 141,
                                    "vCenter": 112.5,
                                    "hCenter": 136.125,
                                    "field": "total",
                                    "testData": "1231",
                                    "coordinateSync": false,
                                    "widthHeightSync": false,
                                    "hideTitle": true,
                                    "fontSize": 12.75,
                                    "qrCodeLevel": 0
                                },
                                "printElementType": {
                                    "title": "文本",
                                    "type": "text"
                                }
                            },
                            {
                                "options": {
                                    "left": 159,
                                    "top": 169.5,
                                    "height": 9.75,
                                    "width": 60,
                                    "right": 216.75,
                                    "bottom": 146.25,
                                    "vCenter": 186.75,
                                    "hCenter": 141.375,
                                    "field": "allNum",
                                    "testData": "3132132",
                                    "coordinateSync": false,
                                    "widthHeightSync": false,
                                    "hideTitle": true,
                                    "fontSize": 12.75,
                                    "qrCodeLevel": 0
                                },
                                "printElementType": {
                                    "title": "文本",
                                    "type": "text"
                                }
                            },
                            {
                                "options": {
                                    "left": 7.5,
                                    "top": 106.5,
                                    "height": 16,
                                    "width": 208.5,
                                    "field": "endTime",
                                    "testData": "2020-01-01",
                                    "fontSize": 12.75,
                                    "textAlign": "left",
                                    "textContentVerticalAlign": "middle",
                                    "title": "结束时间",
                                    "qid": "date",
                                    "coordinateSync": false,
                                    "widthHeightSync": false,
                                    "qrCodeLevel": 0
                                },
                                "printElementType": {
                                    "title": "业务日期",
                                    "type": "text"
                                }
                            },
                            {
                                "options": {
                                    "left": 7.5,
                                    "top": 87,
                                    "height": 16,
                                    "width": 208.5,
                                    "field": "startTime",
                                    "testData": "2020-01-01",
                                    "fontSize": 12.75,
                                    "textAlign": "left",
                                    "textContentVerticalAlign": "middle",
                                    "title": "开始时间",
                                    "qid": "date",
                                    "right": 126,
                                    "bottom": 64,
                                    "vCenter": 66,
                                    "hCenter": 56,
                                    "coordinateSync": false,
                                    "widthHeightSync": false,
                                    "qrCodeLevel": 0
                                },
                                "printElementType": {
                                    "title": "业务日期",
                                    "type": "text"
                                }
                            },
                            {
                                "options": {
                                    "left": 9,
                                    "top": 28.5,
                                    "height": 16,
                                    "width": 207,
                                    "field": "orderId",
                                    "testData": "XS888888888",
                                    "fontSize": 12.75,
                                    "textAlign": "left",
                                    "textContentVerticalAlign": "middle",
                                    "title": "商户名称",
                                    "qid": "orderId",
                                    "coordinateSync": false,
                                    "widthHeightSync": false,
                                    "qrCodeLevel": 0
                                },
                                "printElementType": {
                                    "title": "订单编号",
                                    "type": "text"
                                }
                            },
                            {
                                "options": {
                                    "left": 9,
                                    "top": 48,
                                    "height": 16,
                                    "width": 207,
                                    "field": "orderId",
                                    "testData": "XS888888888",
                                    "fontSize": 12.75,
                                    "textAlign": "left",
                                    "textContentVerticalAlign": "middle",
                                    "title": "订单编号",
                                    "qid": "orderId_1",
                                    "coordinateSync": false,
                                    "widthHeightSync": false,
                                    "qrCodeLevel": 0
                                },
                                "printElementType": {
                                    "title": "订单编号",
                                    "type": "text"
                                }
                            },
                            {
                                "options": {
                                    "left": 9,
                                    "top": 67.5,
                                    "height": 16,
                                    "width": 207,
                                    "field": "orderId",
                                    "testData": "XS888888888",
                                    "fontSize": 12.75,
                                    "textAlign": "left",
                                    "textContentVerticalAlign": "middle",
                                    "title": "订单编号",
                                    "qid": "orderId_2",
                                    "coordinateSync": false,
                                    "widthHeightSync": false,
                                    "qrCodeLevel": 0
                                },
                                "printElementType": {
                                    "title": "订单编号",
                                    "type": "text"
                                }
                            }
                        ],
                        "paperNumberLeft": 190.5,
                        "paperNumberTop": 204,
                        "paperNumberDisabled": true,
                        "paperNumberContinue": true,
                        "watermarkOptions": {
                            "content": "",
                            "fillStyle": "rgba(184, 184, 184, 0.3)",
                            "fontSize": "14px",
                            "rotate": 25,
                            "width": 200,
                            "height": 200,
                            "timestamp": false,
                            "format": "YYYY-MM-DD HH:mm"
                        },
                        "panelLayoutOptions": {
                            "layoutType": "column",
                            "layoutRowGap": 0,
                            "layoutColumnGap": 0
                        }
                    }
                ]
            }

最后就是直接调用就行了,对了在赋值的时候看一下JSON模版的field字段,要和你的数据相对应

本文章已经生成可运行项目
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值