当前位置:首页 > VUE

vue实现模板打印

2026-02-17 04:16:28VUE

使用vue-print-nb插件实现打印

安装vue-print-nb插件

npm install vue-print-nb --save

在main.js中引入并注册插件

import Print from 'vue-print-nb'
Vue.use(Print)

在组件中使用v-print指令

<template>
  <div>
    <button v-print="printObj">打印</button>
    <div id="printContent">
      <!-- 需要打印的内容 -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      printObj: {
        id: 'printContent',
        popTitle: '打印标题'
      }
    }
  }
}
</script>

使用window.print()原生方法

创建打印样式

@media print {
  body * {
    visibility: hidden;
  }
  #printArea, #printArea * {
    visibility: visible;
  }
  #printArea {
    position: absolute;
    left: 0;
    top: 0;
  }
}

实现打印方法

methods: {
  print() {
    const printContent = document.getElementById('printArea').innerHTML
    const originalContent = document.body.innerHTML
    document.body.innerHTML = printContent
    window.print()
    document.body.innerHTML = originalContent
  }
}

使用html2canvas+jspdf实现PDF导出

安装依赖

npm install html2canvas jspdf --save

实现PDF导出

import html2canvas from 'html2canvas'
import jsPDF from 'jspdf'

methods: {
  exportPDF() {
    const element = document.getElementById('pdfContent')
    html2canvas(element).then(canvas => {
      const imgData = canvas.toDataURL('image/png')
      const pdf = new jsPDF('p', 'mm', 'a4')
      const imgWidth = 210
      const pageHeight = 295
      const imgHeight = canvas.height * imgWidth / canvas.width
      let heightLeft = imgHeight
      let position = 0

      pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight)
      heightLeft -= pageHeight

      while (heightLeft >= 0) {
        position = heightLeft - imgHeight
        pdf.addPage()
        pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight)
        heightLeft -= pageHeight
      }
      pdf.save('document.pdf')
    })
  }
}

使用Vue插件vue-html-to-paper

安装插件

npm install vue-html-to-paper

配置和使用

vue实现模板打印

import VueHtmlToPaper from 'vue-html-to-paper'
Vue.use(VueHtmlToPaper)

methods: {
  print() {
    this.$htmlToPaper('printContent', {
      name: '_blank',
      specs: ['fullscreen=yes','titlebar=yes','scrollbars=yes'],
      styles: ['/path/to/print.css']
    })
  }
}

标签: 模板vue
分享给朋友:

相关文章

vue实现setinterval

vue实现setinterval

在 Vue 中使用 setInterval Vue 中可以通过生命周期钩子和方法结合 setInterval 实现定时任务。以下是一个完整的实现示例: <template> <…

vue实现groupbox

vue实现groupbox

Vue 实现 GroupBox 组件 在 Vue 中实现类似 GroupBox 的效果可以通过自定义组件完成。GroupBox 通常是一个带有标题的边框容器,用于将相关控件分组显示。 基本实现方法…

vue filter 实现

vue filter 实现

在 Vue.js 中,过滤器(Filter)是一种用于文本格式化的功能,可以在模板中直接使用。以下是实现过滤器的详细方法: 全局过滤器注册 通过 Vue.filter 方法注册全局过滤器,可在所有组…

vue实现拖拉

vue实现拖拉

实现拖拽功能的基本步骤 在Vue中实现拖拽功能可以通过原生HTML5的拖放API或第三方库如vuedraggable完成。以下是两种方法的详细说明。 使用HTML5原生拖放API HTML5提供了原…

vue实现Pop

vue实现Pop

Vue 实现 Popover 组件的方法 使用 Vue 内置指令 v-show/v-if 和事件监听 通过 Vue 的指令和事件绑定实现基础的 Popover 功能。定义一个布尔值控制 Popover…

vue实现指引

vue实现指引

Vue 实现指引功能 Vue 中实现指引功能可以通过多种方式完成,常见的有使用第三方库或自定义实现。以下是几种常见方法: 使用第三方库(如 driver.js) 安装 driver.js: np…