前端hiprint打印

官方文档:hiprint.io

首先需要在页面中根据自己的路径引入hiprint,一开始没引入css导致打印时当页面超过1页 布局就会有问题

    <script src="../src/assets/js/hiprint/polyfill.min.js"></script>
    <script src="../src/assets/js/hiprint/hiprint.bundle.js"></script>
    <script src="../src/assets/js/hiprint/plugins/jquery.hiwprint.js"></script>
    <link href="../src/assets/js/hiprint/css/hiprint.css" rel="stylesheet" />
    <link href="../src/assets/js/hiprint/css/print-lock.css" rel="stylesheet" />

js代码

printForm1() {
    //这边是处理数据格式的,不用管
    for (var i = 0; i < this.detailForm.length; i++) {
        this.detailForm[i]["PB_BorrowBy_N"] = (this.detailForm[i]["PB_BorrowBy"] == 0 || this.detailForm[i]["PB_BorrowBy"]==null) ? null : (this.selectList.find(item => item.User_ID == this.detailForm[i]["PB_BorrowBy"]).User_Name);
        this.detailForm[i]["PB_ApprovedBy_N"] = (this.detailForm[i]["PB_ApprovedBy"] == 0 || this.detailForm[i]["PB_ApprovedBy"]==null) ? null:(this.userList.find(item => item.User_ID == this.detailForm[i]["PB_ApprovedBy"]).User_Name);
        this.detailForm[i]["PB_WGID_N"] = (this.detailForm[i]["PB_WGID"] == 0 || this.detailForm[i]["PB_WGID"] == null) ? null:(this.userList.find(item => item.User_ID == this.detailForm[i]["PB_WGID"]).User_Name).toString();
    }
    //这边需要对应模版中的数据,字段名称与field的值对应
    //比如pic对应"field": "pic" { "options": { "left": 67.5, "top": 66, "height": 91.5, "width": 96, "src": "/Content/assets/hi.png", "field": "pic" }, "printElementType": { "type": "image" } }
    const data = {
        amount: this.amount,
        pic: "../FileUpload/" + this.detailForm[0].PB_PicUrl,
        BorrowTime: this.getFormattedDate(this.detailForm[0].PB_BorrowTime.format),
        Number: this.detailForm[0].PB_Number,
        pjname: this.pjname,
        username: this.username,
        UpdatedTime: this.getFormattedTime(this.detailForm[0].PB_UpdatedTime),
        table: this.detailForm
    };
    //这边是创建打印样式模版,具体见官方文档
    let hiprintTemplate = new hiprint.PrintTemplate({
        template: { "panels": [{ "index": 0, "paperType": "A4", "height": 297, "width": 210, "paperHeader": 43.5, "paperFooter": 842, "printElements": [{ "options": { "left": 229.5, "top": 40.5, "height": 9.75, "width": 120, "title": "工人借款单", "testData": "工人借款单", "fontSize": 21, "fontWeight": "bold", "showInPage": "first" }, "printElementType": { "type": "text" } }, { "options": { "left": 67.5, "top": 66, "height": 91.5, "width": 96, "src": "/Content/assets/hi.png", "field": "pic" }, "printElementType": { "type": "image" } }, { "options": { "left": 48, "top": 169.5, "height": 9, "width": 502.5 }, "printElementType": { "type": "hline" } }, { "options": { "left": 50, "top": 211.5, "height": 9.75, "width": 120, "title": "日期", "field": "BorrowTime" }, "printElementType": { "type": "text" } }, { "options": { "left": 200, "top": 213, "height": 9.75, "width": 180, "title": "单据编号", "field": "Number" }, "printElementType": { "type": "text" } }, { "options": { "left": 390, "top": 213, "height": 9.75, "width": 120, "title": "项目工地", "field": "pjname" }, "printElementType": { "type": "text" } }, { "options": { "left": 49.5, "top": 262.5, "height": 36, "width": 490.5, "field": "table", "tableBodyRowHeight": 25, "tableBodyCellBorder": "border", "columns": [[{ "title": "工人姓名", "width": 75, "colspan": 1, "rowspan": 1, "field": "PB_BorrowBy_N" }, { "title": "借款金额", "width": 75, "colspan": 1, "rowspan": 1, "field": "PB_Amount" }, { "title": "批准人", "width": 75, "colspan": 1, "rowspan": 1, "field": "PB_ApprovedBy_N" }, { "title": "借款人签字", "width": 75, "colspan": 1, "rowspan": 1 }, { "title": "组长", "width": 75, "colspan": 1, "rowspan": 1, "field": "PB_WGID_N" }, { "title": "备注", "width": 110, "colspan": 1, "rowspan": 1, "field": "PB_Name", "columnId": "PB_Name" }]] }, "printElementType": { "title": "表格", "type": "tableCustom" } }, { "options": { "left": 80, "top": 331.5, "height": 9.75, "width": 120, "title": "合计", "field": "amount" }, "printElementType": { "type": "text" } }, { "options": { "left": 155, "top": 331.5, "height": 9.75, "width": 120, "title": "制单人", "field": "username" }, "printElementType": { "type": "text" } }, { "options": { "left": 320, "top": 331.5, "height": 9.75, "width": 220, "title": "制单时间", "field": "UpdatedTime" }, "printElementType": { "type": "text" } }, { "options": { "left": 46.5, "top": 355.5, "height": 9, "width": 504 }, "printElementType": { "type": "hline" } }], "paperNumberLeft": 565, "paperNumberTop": 819, "firstPaperFooter": null }] }
    });
    //把数据传给模版打印
    hiprintTemplate.print(data)
},

<think>我们正在讨论的是在UniApp中使用hiprint打印插件。hiprint是一个基于jQuery的打印插件,通常用于Web环境中。但是UniApp是一个使用Vue.js开发跨平台应用(如小程序、H5、App)的框架。因此,在UniApp中使用hiprint需要特别注意环境兼容性。 由于hiprint依赖于jQuery和DOM操作,而UniApp的小程序环境没有DOM,所以hiprint不能直接在小程序中使用。但是,如果我们在UniApp中开发的是H5版本,那么就可以像在普通Web页面中一样使用hiprint。 因此,本教程仅适用于UniApp的H5平台。 ### 步骤1:安装hiprint 在UniApp项目中,我们可以通过npm安装hiprint,或者通过引入CDN链接的方式使用hiprint。这里我们推荐使用npm安装。 在项目根目录下执行: ```bash npm install hiprint ``` ### 步骤2:在页面中引入hiprint 由于hiprint需要操作DOM,所以我们需要在页面加载完成后初始化hiprint。在Vue单文件组件中,我们可以在`mounted`生命周期钩子中初始化。 ```html <template> <view> <!-- 用于显示打印内容的容器 --> <div id="printElement"></div> <button @click="doPrint">打印</button> </view> </template> <script> // 引入hiprint import { hiprint } from 'hiprint'; export default { data() { return { hiprintTemplate: null }; }, mounted() { this.initPrint(); }, methods: { initPrint() { // 定义打印模板 let options = { // 这里定义模板的样式和内容 // 具体可参考hiprint官方文档 }; this.hiprintTemplate = new hiprint.PrintTemplate(options); // 将模板渲染到容器 this.hiprintTemplate.print($("#printElement")); }, doPrint() { // 调用打印 this.hiprintTemplate.print(); } } }; </script> ``` ### 步骤3:设计打印模板 hiprint允许我们通过代码或者设计器来设计模板。为了便于开发,我们可以使用hiprint的设计器。但是注意,设计器只能在浏览器环境中运行,所以我们需要在H5页面中引入设计器。 我们可以创建一个单独的页面用于设计模板,然后保存模板数据,再在打印页面中加载。 ### 示例代码:使用设计器 ```html <template> <view> <div id="hiprint-printTemplate"></div> <button @click="saveTemplate">保存模板</button> </view> </template> <script> import { hiprint } from 'hiprint'; import 'hiprint/dist/hiprint.css'; export default { mounted() { this.initDesigner(); }, methods: { initDesigner() { // 初始化设计器 $('#hiprint-printTemplate').hiprintPrintTemplate({ // 一些配置 }); }, saveTemplate() { // 获取模板数据 let templateData = $('#hiprint-printTemplate').getPrintTemplate(); // 保存templateData到本地或服务器 } } }; </script> ``` ### 注意事项 1. 由于hiprint依赖于jQuery,所以需要在项目中引入jQuery。可以通过npm安装并在main.js中引入: ```bash npm install jquery ``` 在main.js中: ```javascript import $ from 'jquery'; global.$ = global.jQuery = $; ``` 2. hiprint在H5环境中使用,所以确保你的UniApp项目运行在H5平台。 3. 打印时,hiprint会生成一个iframe来承载打印内容,并调用浏览器的打印功能。 4. 在App和小程序平台,由于环境限制,不能使用hiprint。如果需要打印功能,可以考虑使用各平台的原生打印能力(如小程序的云打印)或者使用原生插件。 ### 替代方案 如果你需要在App或小程序中使用打印功能,可以考虑以下方案: - 小程序:使用云打印(需要小程序支持) - App:使用原生打印插件,如uni-app官方插件市场中的打印插件(例如:https://ext.dcloud.net.cn/plugin?id=7492) ### 总结 在UniApp的H5环境中,我们可以通过引入hiprint并配合jQuery来实现打印功能。但是需要注意,hiprint不适用于小程序和App环境。 §§相关问题§§ 1. 在UniApp中,除了hiprint还有哪些打印方案? 2. 如何在小程序环境中实现打印功能? 3. hiprint打印模板如何设计? 4. 如何将hiprint模板数据保存到服务器?</think>在 UniApp 中使用 hiprint 打印插件需要特别注意跨平台兼容性,以下是详细教程和实现方案: ### 一、hiprint 插件简介 hiprint 是基于 jQuery 的 Web 打印解决方案,主要用于浏览器环境。UniApp 的 H5 平台可直接使用,但小程序/App 平台需特殊处理。 ### 二、H5 平台实现方案 #### 1. 安装依赖 ```bash npm install jquery hiprint ``` #### 2. 创建打印组件 (`components/hiprint.vue`) ```html <template> <view> <!-- 打印内容容器 --> <div id="printArea"></div> <button @click="print">打印</button> </view> </template> <script> import { hiprint } from 'hiprint'; import $ from 'jquery'; export default { mounted() { this.initTemplate(); }, methods: { initTemplate() { // 创建打印模板 this.template = new hiprint.PrintTemplate({ template: { panels: [{ gridColumn: 1, gridRow: "1/3", elements: [ { type: 'text', options: { text: '订单号', style: { fontSize: 16 } }}, { type: 'text', options: { field: 'orderNo' }} ] }] } }); }, print() { // 填充数据 const data = [{ orderNo: "DD20230801001" }]; // 渲染到DOM this.template.print(data, { callback: () => { // 调用浏览器打印 window.print(); } }); } } } </script> ``` #### 3. 页面中使用组件 ```html <template> <view> <hiprint ref="printer" /> </view> </template> ``` ### 三、小程序/App 平台方案 由于 hiprint 依赖 DOM,需通过 WebView 实现: ```html <template> <web-view v-if="url" :src="url"></web-view> </template> <script> export default { data() { return { url: '' } }, onLoad() { // 从服务端加载打印页 this.url = 'https://your-server.com/print.html?data=' + encodeURIComponent(JSON.stringify({orderNo: "DD20230801001"})); } } </script> ``` 服务端 `print.html` 示例: ```html <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/hiprint@latest/dist/hiprint.bundle.js"></script> <script> $(() => { const urlParams = new URLSearchParams(window.location.search); const data = JSON.parse(decodeURIComponent(urlParams.get('data'))); const template = new hiprint.PrintTemplate({ ... }); // 模板配置 template.print(data, { callback: () => setTimeout(window.print, 500) }); }); </script> </head> <body> <div id="printArea"></div> </body> </html> ``` ### 四、关键注意事项 1. **跨平台处理** - H5:直接使用 hiprint - 小程序/App:通过 WebView 嵌套 H5 页面 - 需在 `manifest.json` 配置 WebView 域名白名单 2. **模板设计技巧** ```javascript // 动态字段绑定 elements: [{ type: 'table', options: { fields: [ { field: 'name', title: '姓名' }, { field: 'age', title: '年龄' } ] } }] ``` 3. **打印样式优化** ```css @media print { body { margin: 0 } .no-print { display: none } } ``` ### 五、替代方案建议 如果项目复杂度高,推荐使用: 1. **原生插件**:如 App 端用 `AndroidPrint` / `IOSPrintPlugin` 2. **云打印**:通过服务端生成 PDF 返回打印链接 3. **小程序专用**:`wx.getSystemInfo` + `wx.print` API > **提示**:hiprint 官方文档提供了完整的模板设计器和 API 参考:[hiprint 文档](https://github.com/CcSimple/hiprint)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值