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

vue print 打印

vue 点击打印页面部分内容,或者打印弹窗内的内容

打印页面部分内容

<template>
    <div>
        <div id="print">
            <div class="info">
                <div class="bx_title">费用报销单<span class="code">NO.<span class="code-num">2008359</span></span></div>
                <div class="list">
                    <div class="list_item" style="width: 22%;">部门:财务部</div>
                    <div class="list_item" style="width: 22%;">报销人:李四</div>
                    <div class="list_item" style="width: 30%;">日期:2021 年 06 月 01 日</div>
                    <div class="list_item" style="width: 22%;">领款人:李四</div>
                </div>
                <div class="approval">
                    <div>XXX审批:</div>
                    <div>XXX审批:</div>
                    <div>XXX审批:</div>
                    <div>XXX审批:</div>
                </div>
            </div>
        </div>
        <div style="display: flex; justify-content: flex-end; padding-top: 20px">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" @click="handleClick">打印</el-button>
        </div>
    </div>
</template>
<script>
export default {
    name: "Detail",
    data() {
        return {
            data: []
        };
    },
    methods: {
		handleClick() {
		       const printContents = document.getElementById('print').innerHTML;
		       const printWindow = window.open('', '', 'width=800,height=1000');
		       printWindow.document.open();
		       printWindow.document.write('<html><head><title>打印</title>');
		       // 复制原页面的样式
		       const links = document.getElementsByTagName('link');
		       for (let i = 0; i < links.length; i++) {
		           if (links[i].rel === 'stylesheet') {
		               printWindow.document.write(`<link rel="stylesheet" type="text/css" href="${links[i].href}">`);
		           }
		       }
		       // 复制原页面的内联样式
		       const styleTags = document.getElementsByTagName('style');
		       for (let i = 0; i < styleTags.length; i++) {
		           printWindow.document.write(styleTags[i].outerHTML);
		       }
		
		       printWindow.document.write('</head><body>');
		       printWindow.document.write(printContents);
		       printWindow.document.write('</body></html>');
		       printWindow.document.close();
		       // // 等待样式加载完成后再调用打印
		       printWindow.onload = function () {
		           printWindow.print();
		           printWindow.close();
		       };
		   }
 	}
}
</script>

遇到问题

1、css 样式丢失

检测是否使用less/scss嵌入式样式风格,全部改成原生的css的写法。

2、指定具体标签内元素打印,css 样式丢失

<style>
   #print {
     //样式如果写在这个下面,就会造成样式丢失
   }
</style>

若要打印 print 里面的,则样式只能在 info下面,不能写在 print 下,不然会丢失


http://www.kler.cn/a/538853.html

相关文章:

  • Centos Ollama + Deepseek-r1+Chatbox运行环境搭建
  • UE5.5 PCGFrameWork--GPU CustomHLSL
  • SpringAI系列 - 使用LangGPT编写高质量的Prompt
  • C# 封送和远程编程介绍
  • 计算机毕业设计Python+Vue.js游戏推荐系统 Steam游戏推荐系统 Django Flask 游 戏可视化 游戏数据分析 游戏大数据 爬虫
  • 四种垄断组织形式的简洁解释-AI
  • 11.递归遍历、迭代遍历、统一迭代、层序遍历
  • Excel大数据量导入导出
  • React 生命周期函数详解
  • antd-react日期组件disabledDate不可选择的日期 (置灰)属性
  • Nginx进阶篇 - nginx多进程架构详解
  • SpringBoot速成(八)登录实战:未登录不能访问 P5-P8
  • Vue 3 + Vite + JS 项目中实现组件全局自动化注册的魔法,极致组件自动化注册方案,开发效率飙升300%。
  • linux 基础知识点之工作队列workqueue
  • (done) openMP学习 (Day9: 优化链表操作)
  • DeepSeek介绍,以及本地部署和API使用
  • 内容中台赋能人工智能技术提升业务创新能力
  • Jenkins 使用教程:从入门到精通
  • 如何使用JLINK连接雅特力MCU
  • Ada语言的云计算
  • LeetCode 力扣热题100 将有序数组转换为二叉搜索树
  • fps动作系统7:武器摇摆
  • 人工智能应用实例-自动驾驶A*算法高级应用
  • LS-SDMTSP:粒子群优化算法(PSO)求解大规模单仓库多旅行商问题(LS-SDMTSP),MATLAB代码
  • 写综述小论文的反思
  • 2.9寒假作业