首页 > 其他分享 >vue根据页面标签生成图片打印

vue根据页面标签生成图片打印

时间:2024-09-23 19:23:34浏览次数:10  
标签:vue 标签 prop width labelName print label type 页面

安装npm install html2canvas
安装npm install print-js
import html2canvas from 'html2canvas';
import print from 'print-js'

在这里插入图片描述


<template>
  <div>
    <div id="content-to-convert" style="width: 772px;">
       <div>
         <div style="margin: 10px;" id="printBill">
           <div style="width: 100%;">
             <div style="text-align: center;"><h3>{{names}}</h3></div>
             <div style="text-align: right;margin-bottom: 3px;">打印时间:{{date}}</div>
           </div>
           <div v-for="(it,index) in tableDataPrunt" :key="index">
             <div style="display: flex;border-left: 1px solid #000000;" v-if="index == 0">
               <td style="padding: 5px;border-right: 1px solid #000000;border-bottom: 1px solid #000000;border-top: 1px solid #000000;"
               v-for="(column,index2) in tablesPrunt" :key="index2" v-if="column.print == 1" :width="column.width">{{column.labelName}}</td>
             </div>
             <div style="display: flex;border-left: 1px solid #000000;">
               <td style="padding: 5px;border-right: 1px solid #000000;border-bottom: 1px solid #000000;"
               v-for="(column,index2) in tablesPrunt" :key="index2" v-if="column.print == 1" :width="column.width">{{it[column.prop]}}888ww</td>
             </div>
           </div>
         </div>
       </div>
    </div>
    <button @click="convertToImageAndPrint">转换为图片打印</button>

    <div>
      <img :src="imgs"/>
    </div>
  </div>
</template>

<script>
import html2canvas from 'html2canvas';
import print from 'print-js'
export default {
  components: {

  },
  data() {
    return {
      imgs:"",
      date:"",
      names:"打印测试",
      tableDataPrunt:[
        {
          "billcode": "CS-000-2014-03-01-0002",
          "billdate": "2014-03-01",
          "billkind": "\u91c7\u8d2d\u6536\u8d27",
          "contractno": null,
          "detaildisc": 0,
          "empid": 4,
          "exrate": 0,
          "goodsid": 1119,
          "id": 3382,
          "lcunitprice": 0,
          "moneyName": null,
          "moneyid": 0,
          "o.traderid": 3,
          "shopid": 0,
          "shopname": "\u603b\u90e8",
          "traderid": 3,
          "tradername": "\u897f\u5b89\u6d5a\u901a\u7535\u5668\u516c\u53f8",
          "unitprice": 0,
          "unitquantity": 0
        },
      ],
      tablesPrunt:[{"label": "供应商", "prop": "tradername", "width": "118", "print": 1, "boot": 1, "labelName": "供应商", "table": "wb_trader", "tableProp": "name", "zid": "traderid", "type": "1"}, {"label": "供应商欠款", "prop": "scamt", "width": 120, "print": 0, "boot": 1, "labelName": "供应商欠款", "table": "wb_trader", "tableProp": "scamt", "zid": "traderid", "type": "1"}, {"label": "分支机构名称", "prop": "shopname", "width": "100", "print": 1, "boot": 1, "labelName": "分支机构名称", "table": "wb_shop", "tableProp": "name", "zid": "shopid", "type": "1"}, {"label": "单据类型", "prop": "billkind", "width": 80, "print": 1, "boot": 1, "labelName": "单据类型", "table": "", "tableProp": "", "zid": "billkind", "type": "1"}, {"label": "单据日期", "prop": "billdate", "width": "100", "print": 1, "boot": 1, "labelName": "单据日期", "table": "", "tableProp": "", "zid": "billdate", "type": "1"}, {"label": "单据编号", "prop": "billcode", "width": "170", "print": 1, "boot": 1, "labelName": "单据编号", "table": "", "tableProp": "", "zid": "billcode", "type": "1"}, {"label": "合同号", "prop": "contractno", "width": 80, "print": 1, "boot": 1, "labelName": "合同号", "table": "", "tableProp": "", "zid": "contractno", "type": "2"}, {"label": "币种", "prop": "moneyName", "width": "80", "print": 1, "boot": 1, "labelName": "币种", "table": "wb_money", "tableProp": "name", "zid": "moneyid", "type": "1"}, {"label": "汇率", "prop": "exrate", "width": "80", "print": 1, "boot": 1, "labelName": "汇率", "table": "", "tableProp": "", "zid": "exrate", "type": "1"}, {"label": "货品编码", "prop": "extendcode", "width": "100", "print": 0, "boot": 1, "labelName": "货品编码", "table": "wb_goods", "tableProp": "code", "zid": "goodsid", "type": "2"}, {"label": "货品名称", "prop": "extendname", "width": "150", "print": 0, "boot": 1, "labelName": "货品名称", "table": "wb_goods", "tableProp": "name", "zid": "goodsid", "type": "2"}, {"label": "规格", "prop": "specs", "width": "140", "print": 0, "boot": 1, "labelName": "规格", "table": "wb_goods", "tableProp": "name", "zid": "goodsid", "type": "2"}, {"label": "货品简称", "prop": "shortname", "width": 120, "print": 0, "boot": 0, "labelName": "货品简称", "table": "wb_goods", "tableProp": "shortname", "zid": "goodsid", "type": "2"}, {"label": "业务单位", "prop": "bunit", "width": "80", "print": 0, "boot": 1, "labelName": "业务单位", "table": "wb_goodsunit", "tableProp": "unitname", "zid": "unitid", "type": "2"}, {"label": "辅助单位", "prop": "punit", "width": "80", "print": 0, "boot": 1, "labelName": "辅助单位", "table": "wb_goodsunit", "tableProp": "unitname", "zid": "goodsid", "type": "2"}, {"label": "辅助单位数量", "prop": "pquantity", "width": "115", "print": 0, "boot": 1, "labelName": "辅助单位数量", "table": "wb_goodsunit", "tableProp": "unitname", "zid": "goodsid", "type": "2"}, {"label": "业务单位单价", "prop": "unitprice", "width": "115", "print": 0, "boot": 1, "labelName": "业务单位单价", "table": "", "tableProp": "", "zid": "unitprice", "type": "2"}, {"label": "业务单位单价(本)", "prop": "lcunitprice", "width": "115", "print": 0, "boot": 1, "labelName": "业务单位单价(本)", "table": "", "tableProp": "", "zid": "lcunitprice", "type": "2"}, {"label": "业务单位数量", "prop": "unitquantity", "width": "115", "print": 0, "boot": 1, "labelName": "业务单位数量", "table": "", "tableProp": "", "zid": "unitquantity", "type": "2"}, {"label": "采购货款", "prop": "goodsamt", "width": "100", "print": 0, "boot": 1, "labelName": "采购货款", "table": "", "tableProp": "", "zid": "goodsamt", "type": "2"}, {"label": "采购货款(本)", "prop": "lcgoodsamt", "width": "100", "print": 0, "boot": 1, "labelName": "采购货款(本)", "table": "", "tableProp": "", "zid": "lcgoodsamt", "type": "2"}, {"label": "税额", "prop": "taxamt", "width": "100", "print": 0, "boot": 1, "labelName": "税额", "table": "", "tableProp": "", "zid": "taxamt", "type": "2"}, {"label": "税额(本)", "prop": "lctaxamt", "width": "100", "print": 0, "boot": 1, "labelName": "税额(本)", "table": "", "tableProp": "", "zid": "lctaxamt", "type": "2"}, {"label": "金额", "prop": "amount", "width": "100", "print": 0, "boot": 1, "labelName": "金额", "table": "", "tableProp": "", "zid": "amount", "type": "2"}, {"label": "金额(本)", "prop": "lcamount", "width": "100", "print": 0, "boot": 1, "labelName": "金额(本)", "table": "", "tableProp": "", "zid": "lcamount", "type": "2"}, {"label": "采购费用", "prop": "alreadyapt", "width": "100", "print": 0, "boot": 1, "labelName": "采购费用", "table": "", "tableProp": "", "zid": "alreadyapt", "type": "2"}, {"label": "备注", "prop": "remark", "width": 120, "print": 0, "boot": 1, "labelName": "备注", "table": "", "tableProp": "", "zid": "remark", "type": "1"}, {"label": "凭证号", "prop": "credid", "width": 120, "print": 0, "boot": 1, "labelName": "凭证号", "table": "", "tableProp": "", "zid": "credid", "type": "1"}, {"label": "发票类型", "prop": "notetypename", "width": 120, "print": 0, "boot": 0, "labelName": "发票类型", "table": "wb_kind", "tableProp": "name", "zid": "notetype", "type": "1"}, {"label": "发票号", "prop": "noteno", "width": 120, "print": 0, "boot": 0, "labelName": "发票号", "table": "", "tableProp": "", "zid": "noteno", "type": "1"}, {"label": "付款方式", "prop": "payname", "width": 120, "print": 0, "boot": 0, "labelName": "付款方式", "table": "wb_paymethod", "tableProp": "name", "zid": "paymethodid", "type": "1"}, {"label": "结算帐户", "prop": "accountname", "width": 120, "print": 0, "boot": 0, "labelName": "结算帐户", "table": "wb_account", "tableProp": "name", "zid": "accountid", "type": "1"}, {"label": "票据号", "prop": "checkno", "width": 120, "print": 0, "boot": 0, "labelName": "票据号", "table": "", "tableProp": "", "zid": "checkno", "type": "1"}, {"label": "运输方式", "prop": "shiptypename", "width": 120, "print": 0, "boot": 0, "labelName": "运输方式", "table": "wb_kind", "tableProp": "name", "zid": "shiptype", "type": "1"}, {"label": "整单折扣", "prop": "disc", "width": 120, "print": 0, "boot": 0, "labelName": "整单折扣", "table": "", "tableProp": "", "zid": "disc", "type": "1"}, {"label": "已结算金额", "prop": "payamt", "width": 120, "print": 0, "boot": 0, "labelName": "已结算金额", "table": "", "tableProp": "", "zid": "payamt", "type": "1"}, {"label": "付款期限", "prop": "termdays", "width": 120, "print": 0, "boot": 0, "labelName": "付款期限", "table": "", "tableProp": "", "zid": "termdays", "type": "1"}, {"label": "付款日期", "prop": "paydate", "width": 120, "print": 0, "boot": 0, "labelName": "付款日期", "table": "", "tableProp": "", "zid": "paydate", "type": "1"}, {"label": "送货地址", "prop": "billto", "width": 120, "print": 0, "boot": 0, "labelName": "送货地址", "table": "", "tableProp": "", "zid": "billto", "type": "1"}, {"label": "送货人", "prop": "linkman", "width": 120, "print": 0, "boot": 0, "labelName": "送货人", "table": "", "tableProp": "", "zid": "linkman", "type": "1"}, {"label": "联系电话", "prop": "contactphone", "width": 120, "print": 0, "boot": 0, "labelName": "联系电话", "table": "", "tableProp": "", "zid": "contactphone", "type": "1"}, {"label": "仓库编码", "prop": "storecode", "width": 120, "print": 0, "boot": 0, "labelName": "仓库编码", "table": "wb_store", "tableProp": "code", "zid": "storeid", "type": "2"}, {"label": "仓库名称", "prop": "storename", "width": 120, "print": 0, "boot": 0, "labelName": "仓库名称", "table": "wb_store", "tableProp": "name", "zid": "storeid", "type": "1"}, {"label": "单价", "prop": "unitprice", "width": 120, "print": 0, "boot": 0, "labelName": "单价", "table": "", "tableProp": "", "zid": "unitprice", "type": "2"}, {"label": "折扣率", "prop": "detaildisc", "width": "80", "print": 0, "boot": 1, "labelName": "折扣率", "table": "", "tableProp": "", "zid": "disc", "type": "2"}, {"label": "折扣金额", "prop": "taxamt", "width": 120, "print": 0, "boot": 0, "labelName": "折扣金额", "table": "", "tableProp": "", "zid": "taxamt", "type": "2"}, {"label": "批号", "prop": "batchcode", "width": 120, "print": 0, "boot": 0, "labelName": "批号", "table": "", "tableProp": "", "zid": "batchcode", "type": "2"}, {"label": "被引用辅助数量", "prop": "referextqty", "width": 120, "print": 0, "boot": 0, "labelName": "被引用辅助数量", "table": "", "tableProp": "", "zid": "", "type": ""}, {"label": "批号", "prop": "batchcode", "width": 120, "print": 0, "boot": 0, "labelName": "批号", "table": "", "tableProp": "", "zid": "batchcode", "type": "2"}, {"label": "生产日期", "prop": "productdate", "width": 120, "print": 0, "boot": 0, "labelName": "生产日期", "table": "", "tableProp": "", "zid": "productdate", "type": "2"}, {"label": "有效日期", "prop": "validdate", "width": 120, "print": 0, "boot": 0, "labelName": "有效日期", "table": "", "tableProp": "", "zid": "validdate", "type": "2"}, {"label": "部门名称", "prop": "departmentname", "width": 120, "print": 0, "boot": 0, "labelName": "部门名称", "table": "wb_department", "tableProp": "name", "zid": "departmentid", "type": "1"}, {"label": "业务员", "prop": "empname", "width": 120, "print": 0, "boot": 1, "labelName": "业务员", "table": "wb_employ", "tableProp": "name", "zid": "empid", "type": "1"}, {"label": "制单人", "prop": "opname", "width": 120, "print": 0, "boot": 0, "labelName": "制单人", "table": "wb_operator", "tableProp": "opname", "zid": "opid", "type": "1"}, {"label": "审核人", "prop": "checkor", "width": 120, "print": 0, "boot": 0, "labelName": "审核人", "table": "wb_operator", "tableProp": "opname", "zid": "checkorid", "type": "1"}, {"label": "自定义字段1", "prop": "userdef1", "width": 120, "print": 0, "boot": 0, "labelName": "自定义字段1", "table": "", "tableProp": "", "zid": "userdef1", "type": "2"}, {"label": "自定义字段2", "prop": "userdef2", "width": 120, "print": 0, "boot": 0, "labelName": "自定义字段2", "table": "", "tableProp": "", "zid": "userdef2", "type": "2"}, {"label": "自定义字段3", "prop": "userdef3", "width": 120, "print": 0, "boot": 0, "labelName": "自定义字段3", "table": "", "tableProp": "", "zid": "userdef3", "type": "2"}, {"label": "明细备注", "prop": "dremark", "width": 120, "print": 0, "boot": 0, "labelName": "明细备注", "table": "", "tableProp": "", "zid": "remark", "type": "2"}, {"label": "自定义字段4", "prop": "userdef4", "width": 120, "print": 0, "boot": 0, "labelName": "自定义字段4", "table": "", "tableProp": "", "zid": "userdef4", "type": "2"}, {"label": "自定义字段5", "prop": "userdef5", "width": 120, "print": 0, "boot": 0, "labelName": "自定义字段5", "table": "", "tableProp": "", "zid": "userdef5", "type": "2"}, {"label": "自定义字段6", "prop": "userdef6", "width": 120, "print": 0, "boot": 0, "labelName": "自定义字段6", "table": "", "tableProp": "", "zid": "userdef6", "type": "2"}, {"label": "自定义字段7", "prop": "userdef7", "width": 120, "print": 0, "boot": 0, "labelName": "自定义字段7", "table": "", "tableProp": "", "zid": "userdef7", "type": "2"}, {"label": "自定义字段8", "prop": "userdef8", "width": 120, "print": 0, "boot": 0, "labelName": "自定义字段8", "table": "", "tableProp": "", "zid": "userdef8", "type": "2"}, {"label": "自定义字段9", "prop": "userdef9", "width": 120, "print": 0, "boot": 0, "labelName": "自定义字段9", "table": "", "tableProp": "", "zid": "userdef9", "type": "2"}, {"label": "自定义字段10", "prop": "userdef10", "width": 120, "print": 0, "boot": 0, "labelName": "自定义字段10", "table": "", "tableProp": "", "zid": "userdef10", "type": "2"}, {"label": "自定义字段11", "prop": "userdef11", "width": 120, "print": 0, "boot": 0, "labelName": "自定义字段11", "table": "", "tableProp": "", "zid": "userdef11", "type": "2"}, {"label": "自定义字段12", "prop": "userdef12", "width": 120, "print": 0, "boot": 1, "labelName": "自定义字段12", "table": "", "tableProp": "", "zid": "userdef12", "type": "2"}, {"label": "自定义字段13", "prop": "userdef13", "width": 120, "print": 0, "boot": 1, "labelName": "自定义字段13", "table": "", "tableProp": "", "zid": "userdef13", "type": "2"}, {"label": "自定义字段14", "prop": "userdef14", "width": 120, "print": 0, "boot": 1, "labelName": "自定义字段14", "table": "", "tableProp": "", "zid": "userdef14", "type": "2"}, {"label": "自定义字段15", "prop": "userdef15", "width": 120, "print": 0, "boot": 1, "labelName": "自定义字段15", "table": "", "tableProp": "", "zid": "userdef15", "type": "2"}],
    }
  },
  watch: {

  },
  //界面没出来前加载
  created() {
    let date = new Date();
    this.date = date.getFullYear() + '-' +
          ('0' + (date.getMonth() + 1)).slice(-2) + '-' +
          ('0' + date.getDate()).slice(-2) + ' ' +
          ('0' + date.getHours()).slice(-2) + ':' +
          ('0' + date.getMinutes()).slice(-2) + ':' +
          ('0' + date.getSeconds()).slice(-2);
  },
  mounted() {

  },
  computed: {

  },
  methods: {
     // 标签生成图片保存
     // convertToImage() {
     //   html2canvas(document.getElementById('content-to-convert')).then(canvas => {
     //     // 创建一个临时的 a 标签用于下载
     //     const link = document.createElement('a');
     //     link.href = canvas.toDataURL();
     //     link.download = 'converted_image.png';
     //     link.click();
     //   });
     // }
     convertToImageAndPrint() {
       html2canvas(document.getElementById('content-to-convert'), { scale: 2 }).then(canvas => {
         const img = new Image();
         img.src = canvas.toDataURL();
         this.imgs = img.src
         // 跳转窗口
         //const previewWindow = window.open('', '_blank');
         //previewWindow.document.write(`<img src="${img.src}" />`);
         // this.pr(this.imgs)
         printJS({
            printable: img.src,
            type: 'image',
            header: '',
            targetStyles: [],
            scanStyles: false,
          });
       });
     },
  }
};
</script>

<style scoped>
  @media print {
    html,
    body {
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0;
   }
    #printBill {
      /* 确保打印的元素占据整个页面 */
      width: 100%;
      height: 100%;
    }
  }
</style>


标签:vue,标签,prop,width,labelName,print,label,type,页面
From: https://blog.csdn.net/qq_34631220/article/details/142462407

相关文章

  • vue生成图片打印
    <divid="content-to-convert"style="width:772px;color:#000000;">生成图片内容</div>html2canvas(document.getElementById('content-to-convert'),{scale:2}).then(canvas=>{//html2canvas(document.getEle......
  • 【开题报告】基于django+vue旅游景点预约系统(论文+源码) 计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。系统程序文件列表开题报告内容研究背景随着旅游业的蓬勃发展,人们对旅游体验的需求日益多样化与个性化。传统的旅游预约方式往往存在信息不对称、预约流程繁琐、效率低下等问题,已......
  • springboot+vue自驾游信息管理系统【开题+程序+论文】
    系统程序文件列表开题报告内容研究背景随着生活水平的提高和私家车的普及,自驾游已成为现代人追求自由、个性化旅行方式的首选。然而,自驾游过程中涉及的信息繁多且复杂,包括路线规划、景点选择、美食探索、住宿预订等多个方面,传统的手工查询和记录方式已难以满足游客高效、便......
  • springboot+vue智慧外贸平台7l0u0【开题+程序+论文】
    系统程序文件列表开题报告内容研究背景随着全球经济的深度融合与信息技术的飞速发展,外贸行业正经历着前所未有的变革。传统外贸模式受限于信息不对称、流程繁琐、效率低下等问题,难以满足日益增长的国际贸易需求。在此背景下,智慧外贸平台应运而生,旨在通过集成大数据、云计算......
  • springboot+vue中药实验管理系统【开题+程序+论文】
    系统程序文件列表开题报告内容研究背景随着中医药事业的蓬勃发展,中药实验作为中医药教育与科研的重要组成部分,其管理水平直接影响着教学质量与科研成果的转化效率。然而,传统的中药实验管理模式多依赖于人工记录与纸质文档,存在信息更新不及时、资源共享难度大、管理流程繁琐......
  • springboot+vue羽毛球场管理系统【开题+程序+论文】
    系统程序文件列表开题报告内容研究背景随着全民健身热潮的兴起,羽毛球作为一项广受欢迎的运动项目,其场地需求日益增长。然而,传统的人工管理模式在面对大量用户预约、场地分配、储物柜管理以及商品销售等复杂需求时,显得力不从心。这不仅影响了用户体验,也增加了管理成本。因此......
  • 设计师要讲“武德“,不然搞成的可视化大屏页面,让前端如何办?
    设计师讲武德,本意是UI设计师要熟悉可视化大屏的规范,不能为了设计而设计,要为后面前端开发可实现预留好空间,如果从心所欲,内心没有尺度,后面工作开展起来就非常费劲。在当今数字化时代,用户界面(UI)设计师的角色变得越发重要。他们不仅需要具备设计技能,还需要熟悉可视化大屏的规范,以......
  • springboot基于Java的高校学生食堂系统(源码+vue+部署文档+前后端分离等)
    收藏关注不迷路!!......
  • vue3.0使用v-md-editor预览markdown文件
    1.安装依赖npmi@kangc/v-md-editor-S2.在main.js文件中引用注册组件import{createApp}from'vue';//预览组件以及样式importVMdPreviewfrom'@kangc/v-md-editor/lib/preview';import'@kangc/v-md-editor/lib/style/preview.css';importgithubThe......
  • 【艾思科蓝】前端框架巅峰对决:React、Vue与Angular的全面解析与实战指南
    【JPCS独立出版】​第三届能源与动力工程国际学术会议(EPE2024)_艾思科蓝_学术一站式服务平台更多学术会议请看:https://ais.cn/u/nuyAF3 引言在快速发展的前端技术领域,选择合适的框架或库对于项目的成功至关重要。React、Vue和Angular作为当前最流行的三大前端框架/库,各自......