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

Vue-打印组件页面

场景: 需要将页面的局部信息打印出来,只在前端实现,不要占用后端的资源。经过百度经验,决定使用 print-jshtml2canvas组件。

1. 下载包

npm install print-js --save
npm install --save html2canvas

2. 组件内引用

<script>import printJS from 'print-js'import 'print-js/dist/print.css'import html2canvas from 'html2canvas'</script>

3. 执行打印方法

<div><el-card style="height: 780px; overflow: auto;page-break-after:always;"><div ref="printPaperRef"><template v-for="index in 15"><!-- 题目: 序号、类型、题干 --><div><div class="num">{{index}}</div>【单选题】<div style="padding-left: 10px;">这是一道很难很难很难很难的单选题,{{index}}}</div></div><!-- 选项 --><el-radio-group style="width: 100%" ><el-radio v-for="item in ['A', 'B', 'C', 'D']" borderclass="answer_radio"><!-- 选项flex浮动 --><div style="display: inline-flex;width: 90%;"><div class="answer_tag">{{ item }}.</div></div><div style="float: right;"><i class="el-icon-success" style="color:#1aac1a;">答案</i></div></el-radio></el-radio-group></template></div></el-card>
</div>import printJS from 'print-js'import 'print-js/dist/print.css'import html2canvas from 'html2canvas'
export default {name: 'ExamProcess',methods: {// 打印试卷printPaper() {html2canvas(this.$refs.printPaperRef, {backgroundColor: 'white',useCORS: true,foreignObjectRendering: false,windowWidth: document.body.scrollWidth,windowHeight: document.body.scrollHeight}).then((canvas) => {const url = canvas.toDataURL()this.img = urlprintJS({printable: url,type: 'image',documentTitle: "--",base64: 'true'})})}}
}

遇到的问题:

1. html2canvas 文字向下偏移

 解决:  使用html2canvas@^1.0.0的版本

2. html2canvas转图片不清晰的问题

 

解决: 利用增大dpi

dpi:DPI是指某些设备分辨率的度量单位。DPI越低,扫描的清晰度越低,DPI越高,清晰度越高。
由于受网络传输速度的影响,web上使用的图片都是72dpi,照片使用300dpi或者更高350dpi,会很清晰。

html2canvas(template, {dpi: 300,//加了一个这个设置 useCORS: true, //(图片跨域相关)allowTaint: false, //允许跨域(图片跨域相关)x: 0,//页面在横向方向上的滚动距离  横向上没有超过 所以设置为0y: window.pageYOffset,//页面在垂直方向上的滚动距离 设置了以后 超过一屏幕的内容也可以截取windowWidth: document.body.scrollWidth,//获取在x轴上滚动条中内容windowHeight: document.body.scrollHeight,//获取在y轴上滚动条中内容});

 解决后的效果: 

 

http://www.lryc.cn/news/125921.html

相关文章:

  • Python爬虫——scrapy_基本使用
  • 30 | 中国高校数据分析
  • 开源低代码平台Openblocks
  • 每日汇评:黄金在 200 日移动平均线附近似乎很脆弱,关注美国零售销售
  • DFT笔记 DC/AC mode与Func
  • docker核心操作
  • 《电路》基础知识入门学习笔记
  • 什么是P2P?
  • matlab RANSAC拟合多项式曲线
  • 微信小程序nfc指令异常记录
  • 10 - 把间隔的几个commit整理成1个
  • 关于 Eclipse 的一场 “三角关系”
  • koa 使用 Mongoose 查询数据
  • 前后端分离------后端创建笔记(10)用户修改
  • K8S系列文章之 Docker安装使用Kafka
  • js如何获取字符串大小是几M
  • 服务器托管中1U是什么意思?
  • Golang自定义类型与类型别名
  • golang环境搭建
  • 一套优质的MES系统,应该具备哪些特质?
  • 常见的路由协议之RIP协议与OSPF协议
  • 使用ip2region获取客户端地区
  • RH850从0搭建Autosar开发环境【24】- Davinci Configurator之DEM模块配置详解(上)
  • uniapp封装接口
  • Android布局【TableLayout】
  • C/C++中static关键字详解
  • Memory Analyzer(MAT)分析内存
  • 计算机网络面试题
  • 【LeetCode75】第三十题 奇偶链表
  • docker 学习--03 环境安装(本人使用的win10 Linux也是在win10下模拟)