vue前端打印代码加遇到的问题

安装 vue-print-nb-jeecg 插件

npm install vue-print-nb-jeecg –save

全局挂载main.js

import Print from 'vue-print-nb-jeecg'

Vue.use(Print);

写一个打印按钮,添加 v-print=“’#printContent’”为下面div的id

<a-button v-print="'#printContent'" type="primary">
打印</a-button>

打印的内容,div的id为printContent

<section ref="print" id="printContent" class="">
    <div>内容</div>
</section>

遇到的问题

1.打印预览时多一张空白页

解决:

<style @media="printContent">
@page {
size: auto;
/* margin: 3mm; */
}
html {
background-color: #ffffff;
height: auto;
/* margin: 0px; */
}
</style>

2.希望在打印时显示 id="printContent" 的内容,但在组件引用时隐藏它

使用 Vue.js 的条件渲染结合 CSS 来实现,使用了 v-show="printMode" 来根据 printMode 的值决定是否显示 id="printContent" 的元素。在组件初始化时,printMode 的初始值为 false,因此 id="printContent" 的元素会被隐藏。

然后,使用 CSS 的 @media 查询来定义打印样式。在 @media print 查询中,我们将 #printContent 的显示设置为 block,以确保在打印时显示特定内容。

同时,通过 window 对象的 beforeprintafterprint 事件监听器来切换 printMode 的值。在打印开始前,beforeprint 事件会触发 beforePrint 方法,将 printMode 设置为 true,以便在打印时显示 id="printContent" 的内容。在打印完成后,afterprint 事件会触发 afterPrint 方法,将 printMode 设置为 false,以便在组件引用时隐藏 id="printContent" 的内容。

这样,当点击打印按钮时,id="printContent" 的元素会显示出来,并在打印时正常显示,而在组件引用时会隐藏。

<template>
  <a-card :bordered="false" :class="{ abcdefg: true}">
    <div class="no-print" style="text-align: right">
      <a-button @click="togglePrintMode" type="primary">打印</a-button>
    </div>
    <section ref="print" id="printContent" class="abcdefg" v-show="printMode">
      <div>
        内容
      </div>
    </section>
  </a-card>
</template>

<script>
export default {
  data() {
    return {
      printMode: false
    }
  },
  methods: {
    togglePrintMode() {
      this.printMode = !this.printMode;
    },
    beforePrint() {
      this.printMode = true;
    },
    afterPrint() {
      this.printMode = false;
    }
  },
  mounted() {
    window.addEventListener("beforeprint", this.beforePrint);
    window.addEventListener("afterprint", this.afterPrint);
  },
  beforeDestroy() {
    window.removeEventListener("beforeprint", this.beforePrint);
    window.removeEventListener("afterprint", this.afterPrint);
  }
}
</script>

<style>
@media print {
  #printContent {
    display: block !important;
  }
}
@media screen {
  #printContent {
    display: none;
  }
}
</style>

另,还有其他的插件,如vue-print-nb类似使用 

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现Vue前端打印的方法有多种。其中一种方法是通过引入打印插件来实现。你可以在后台功能中查询合适的插件并进行下载,然后在前端中使用该插件来进行打印操作。 另外,需要注意的是在处理打印逻辑之前,要确保样式已经正确引入,否则打印的页面可能会没有样式。你可以单独引入一个print.scss文件或者使用@media print {}来调整一些样式。这样可以确保打印页面的样式与预期一致。 如果打印的内容中包含线上图片资源,并且你不想进行服务器配置,可以考虑在浏览器关闭获取缓存文件的方式来解决。这样可以保证每一次获取的都是线上文件,避免打印时出现图片跨域的错误。需要注意的是,前端无法获取你点击打印或者取消的事件,只能获取你是否关闭打印框的事件。 综上所述,你可以通过下载适合的打印插件来实现Vue前端打印功能,并在处理打印逻辑前确保样式已正确引入。如果打印内容中包含线上图片资源,可以通过浏览器关闭获取缓存文件的方式来避免跨域错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue 前端实现打印功能](https://blog.csdn.net/m0_64344940/article/details/128405517)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue前端实现打印功能](https://blog.csdn.net/love521334421/article/details/125163942)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值