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

前端实现浏览器打印

浏览器的print方法直接调用会打印当前页面的所有元素,使用iframe可以实现局部打印所需要的模块。

组件printView,将传入的信息放入iframe中,调用浏览器的打印功能

<template><div class="print"><iframeid="iframe"style="display: none; width: 100%"frameborder="0"></iframe></div>
</template>
<script>
export default {name: 'printView',methods: {setBodyHtml(html) {const document = window.documentconst iframe = window.frames[0]iframe.document.head.innerHTML = document.head.innerHTML // 获取当前文档的头部给iframeiframe.document.body.innerHTML = html // 把传过来的html给iframe头部iframe.document.body.style.background = '#fff'let arr = document.getElementsByTagName('tr')let heightNum = 0let onePage = 800 //第一页的高度for (let i in arr) {heightNum += arr[i].offsetHeightif (heightNum > onePage) {heightNum = arr[i].offsetHeightonePage = 1500 //第二页高度}}iframe.window.print()}}
}
</script>

<div v-if="detail.work_order_id" class="before" id="print_info">

        <work-order-detail :detail="detail"></work-order-detail>

</div>

// 打印

const viewRef = ref(null)

const print = () => {

  const html = document.getElementById('print_info').innerHTML

  viewRef.value.setBodyHtml(html)

}

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

相关文章:

  • iOS卡顿原因与优化
  • 关于synchronized介绍
  • NCDA设计大赛获奖作品剖析:UI设计如何脱颖而出?
  • 软考中级 软件设计师备考经验
  • Python猜数字小游戏
  • SQL面试题(2)
  • python常用pandas函数nlargest 和 nsmallest及其手动实现
  • 第六课:NIO简介
  • 在vue2中使用饼状图
  • 面经(五)南京 软通动力 一面
  • 线段树模型及例题整理
  • 揭秘Java性能调优的层次 | 综合多方向提升应用程序性能与系统高可用的关键(架构层次规划)
  • 事件循环解析
  • 物联网技术助力智慧城市安全建设:构建全方位、智能化的安全防护体系
  • mac打不开xxx软件, 因为apple 无法检查其是否包含恶意
  • 《深入浅出红黑树:一起动手实现自平衡的二叉搜索树》
  • C++——模版
  • 《TCP/IP详解 卷一》第9章 广播和组播
  • 备战蓝桥杯---动态规划的一些思想1
  • 基于BERTopic模型的中文文本主题聚类及可视化
  • MySQL:函数
  • C/C++内存管理及内存泄漏详解
  • 什么是系统工程(字幕)41
  • 测开新手:pytest+requests+allure自动化测试接入Jenkins学习
  • 学习网络编程No.11【传输层协议之UDP】
  • 向爬虫而生---Redis 基石篇6 <拓展HyperLogLog>
  • JavaScript中的this
  • 宝塔php站点设置伪静态规则 访问 a.com 时候跳转到 a.com/b.html
  • git介绍4.2
  • 【深入了解设计模式】组合设计模式