当前位置: 首页 > news >正文

【弹框组件封装】展示、打印、下载XX表(Base64格式图片)

目录

  • 打印、下载弹框组件
  • 组件使用
  • 弹框展示

打印、下载弹框组件

@/components/PrintDialog.vue

<!-- 打印、下载弹框 -->
<template>
  <el-dialog
    :title="title"
    top="3vh"
    append-to-body
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :before-close="handleClose"
    width="50%"
  >
    <div ref="printRef">
      <el-image :src="src" :preview-src-list="[src]" />
    </div>
    <div v-if="!isPreview" slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button :loading="downloadLoading" type="primary" @click="handleDownload">下载</el-button>
      <el-button :loading="printLoading" type="primary" @click="handlePrint">打印</el-button>
    </div>
  </el-dialog>
</template>

<script>
import html2canvas from 'html2canvas'
import printJS from 'print-js'

export default {
  name: 'PrintDialog',
  components: {},

  data() {
    return {
      dialogVisible: false,
      downloadLoading: false,
      printLoading: false,
      title: '',
      src: '',
      isPreview: false
    }
  },

  computed: {},

  watch: {},

  created() {},

  methods: {
    open(title, src, isPreview = false) {
      this.title = title
      this.src = src
      this.isPreview = isPreview
      this.dialogVisible = true
    },
    handlePrint() {
      this.printLoading = true
      html2canvas(this.$refs.printRef).then((canvas) => {
        const saveUrl = canvas.toDataURL('image/png')
        printJS({
          printable: [saveUrl],
          type: 'image'
        })
      })
      this.printLoading = false
    },
    handleDownload() {
      this.downloadLoading = true
      html2canvas(this.$refs.printRef)
        .then((canvas) => {
          const imgUrl = canvas.toDataURL('image/png')
          let image = document.createElement('img')
          image.src = imgUrl
          let a = document.createElement('a')
          a.href = imgUrl
          a.download = this.title
          a.click()
          a = null
          image = null
        })
        .finally(() => {
          this.downloadLoading = false
        })
    },
    handleClose() {
      this.title = ''
      this.src = ''
      this.isPreview = false
      this.dialogVisible = false
    }
  }
}
</script>

<style lang='scss' scoped>
@import '@/styles/dialog-scss.scss';
.el-image {
  width: 100%;
}
</style>

组件使用

<template>
	<el-button
	  :disabled="selectList.length !== 1"
	  icon="iconfont ico-daochu"
	  type="text"
	  size="mini"
	  @click="printHandler"
	>
	  打印记分决定操作表
	</el-button>
	<!-- 打印 -->
	<PrintDialog ref="PrintDialogRef" />
</template>

<script>
import { previewScore } from '@/api/twelve-points'
import OperationDialog from './components/OperationDialog'

export default {
 components: { PrintDialog },
  data() {
    return {
      jfbh: ''
    }
  },

  methods: {
    printHandler() {
      console.log('打印记分告知单')

      previewScore({ jdbh: this.jfbh }).then((res) => {
        this.$common.CheckCode(res, null, () => {
          if (!res.data) return
          this.$refs.PrintDialogRef.open('操作表', 'data:image/jpg;base64,' + res.data)
          // this.$refs.PrintDialogRef.open('文件名/弹框title', 'base64前缀' + Base64格式图片)
        })
      })
    },
  }
}
</script>

弹框展示

在这里插入图片描述

相关文章:

  • win11编译pytorchvision cuda128版本流程
  • C++中读取与保存不同维度的csv数据
  • 计算机毕业设计SpringBoot+Vue.js图书馆管理系统(源码+文档+PPT+讲解)
  • Red Hat Enterprise Linux 发行日期!
  • 开源ocr
  • 希音(Shein)运维面试题集锦和参考答案
  • aiohttp、httpx 和 requests 的区别
  • Tomcat 8 安装包下载
  • Leetcode 206 -反转链表
  • io学习----->标准io
  • 解锁ApplicationContext vs BeanFactory: 谁更具选择性?
  • Mac 下载安装并卸载多jdk教程
  • HTTP/1.1 和 HTTP/2 的区别,HTTP/2 有哪些新特性?
  • Transformer结构和注意力机制
  • 数组中的逆序对(C++)
  • 六十天前端强化训练之第八天之JavaScript核心之ES6变量与箭头函数(第8-14天:JavaScript核心)
  • 【Python运维】 Python与日志管理:构建高效的集中式日志收集与分析系统
  • android bp构建编译C++代码
  • arxiv论文信息爬取与论文pdf下载
  • C语言——结构体、联合体/共用体、枚举类型、typedef关键字
  • AI赋能科学红毯,机器人与科学家在虚实之间叩问“科学精神”
  • 上海交大曾小勤:科技传播不应停留于知识搬运,要做科学思维的播种机
  • 韶关一企业将消防安装工程肢解发包,广东住建厅:罚款逾五万
  • 临港新片区将新设5亿元启航基金:专门投向在临港发展的种子期、初创型企业
  • 联合国报告:全球经济前景恶化,面临高度不确定性
  • 龚正市长调研闵行区,更加奋发有为地稳增长促转型,久久为功增强发展后劲