vue3 实现自定义模板表单打印

写在前面

本项目基于 hiprint 和 vue-plugin-hiprint 实现,感谢大佬的分享

项目集成

安装

笔者项目是 vue3 的后台管理系统,使用包管理工具 pnpm 安装打印插件

bash 复制代码
pnpm add vue-plugin-hiprint

引入 css

安装成功后,需要在项目根目录的 index.html 引入打印组件样式文件print-lock.css,可以在node_modules/vue-plugin-hiprint/dist找到这个文件,复制出来放到根目录下,方便引入

html 复制代码
<link rel="stylesheet" type="text/css" media="print" href="/print-lock.css">

✨ 项目打包完后,可能会修改 css 文件名,这里需要保持文件名不变

引入插件

可以在项目入口 main 文件配置全局打印属性,也可以在需要的模块单独引入,因为涉及打印要抽离成单独模块,使用地方不多,只需要在需要的页面单独引入即可

vue 复制代码
import { hiprint } from "vue-plugin-hiprint"

页面设计

hiprint 只支持在官网设计好打印模板,导出 json 使用,不支持在系统内集成设计模块。vue-plugin-hiprint 示例项目已经实现了设计模块,我们可以把示例项目下载下来,根据需要选择设计模块,笔者选择的是自定义设计,最终实现页面大体如下:

初始化

在打印模板设计页面初始化完成后(onMounted),初始化插件

javascript 复制代码
function initHiprint() {
  hiprint.init({
    providers: [aProvider()]
  });

  $('.hiprintEpContainer').empty()
  hiprint.PrintElementTypeManager.build('.hiprintEpContainer', 'aProviderModule');
  $('#hiprint-printTemplate').empty()

  state.hiprintTemplate = new hiprint.PrintTemplate({
    template: state.templateInfo?.content || {},
    dataMode: 1,
    history: false,
    onDataChanged: (type, json) => {
      console.log(type, json)
    },
    onImageChooseClick: (target) => {
      console.log(target)
    },
    settingContainer: '#PrintElementOptionSetting',
    paginationContainer: '.hiprint-printPagination',
  })

  state.hiprintTemplate.design('#hiprint-printTemplate', {
    grid: true,
  });
  state.hiprintTemplate?.setPaper(state.curPaper.width, state.curPaper.height);
}

providers

这里设置打印模板的初始组件,也就是页面的左边区域,可以根据业务实际情况设置,vue-plugin-print也有导出默认组件defaultElementTypeProvider

template

这里面是一个大 json,保存打印模板的信息,默认情况为空,如果系统已经保存模板,则把已保存的模板放在这里

✨ 如果对模板要设置一些初始配置,可以先设计好模板,然后查看模板 json,把需要的配置信息提取出来,设置为初始化数据

其他

  • grid:true

页面显示网格线

  • setPaper

设置打印模板的尺寸

  • 页眉,页脚高亮显示

在 css 里覆盖样式

css 复制代码
// 修改 页眉/页脚线 样式
.hiprint-headerLine,
.hiprint-footerLine {
  border-color: red !important;
}

.hiprint-headerLine:hover,
.hiprint-footerLine:hover {
  border-top: 3px dashed red !important;
}

.hiprint-headerLine:hover:before {
  content: "页眉线";
  left: calc(50% - 18px);
  position: relative;
  background: #ffff;
  top: -12px;
  color: red;
  font-size: 12px;
}

.hiprint-footerLine:hover:before {
  content: "页脚线";
  left: calc(50% - 18px);
  position: relative;
  color: red;
  background: #ffff;
  top: -12px;
  font-size: 12px;
}
  • 图片

笔者模板里只有一个图片组件-logo,就直接在 css 将元素写死,这样拖拉组件时,也能实时显示图片

css 复制代码
// 默认图片样式
.hiprint-printElement-image-content {
  img {
    content: url('@/assets/logo.png');
  }
}

设计

仿照示例项目写好代码,接下来就是愉快🐶的拖拉拽环节

可以使用鼠标左键在页面内划取区域,批量移动已选组件的位置,如果需要精细化操作,也可以参考示例项目实现的组件对齐

✨ 组件如果需要一些默认设置,可以先在模板内设计好,通过查看 json 输出,然后在自定义 provider 中修改

预览

示例项目实现了预览组件,在模板页面引入组件<PrintView ref="printViewRef" />,通过printViewRef.value.show(state.hiprintTemplate, printData, state.curPaper.width)预览模板

选择打印,打开系统打印页面,完成打印

其他

最好根据票据尺寸,在设备管理里设置打印机的纸张尺寸,防止打印时候出现以外的结果

相关推荐
kyriewen9 小时前
编辑弹窗点了取消,列表里的数据怎么还是变了?
前端·javascript·人工智能
Karl_fang9 小时前
🥳 再也不用拿起手机抄验证码!我做了一个 Android → Mac 的「验证码接力」工具
前端·macos·ai编程
谭光志10 小时前
如何让 AI 高效工作
前端·后端·ai编程
FRing0311 小时前
vue2干货总结11(短信验证、登录功能、统一处理错误(响应拦截器)、将token信息存入vuex、vuex持久化处理)
开发语言·前端·javascript·vue.js·vue2
泯泷11 小时前
改一个字,整段都红了?——中文内容差异对比的算法与协议设计
前端·javascript·typescript
再吃一根胡萝卜11 小时前
DSH plugin 开发示例:五种插件形态完整代码整理
前端
Yk酱2284011 小时前
A2UI随笔-demo-01
前端
云起SAAS11 小时前
本地生活同城多商户商城系统源码源码 合伙人分红加区域代理加排队返现
前端·javascript·后端
奶油话梅糖12 小时前
3 天 1,705 Star:DeepSeek 开源官方聊天前端——零后端纯静态,浏览器直连 API
前端·开源
Linye12 小时前
HTML 流程图连线交叉看不清?聊聊画布避障折线与跳弧实现
前端·人工智能