当前位置:首页 > VUE

vue实现打印模板

2026-02-23 22:56:44VUE

Vue 实现打印模板的方法

使用 window.print() 方法

在 Vue 中可以通过调用浏览器原生的 window.print() 方法实现打印功能。创建一个隐藏的打印区域,将需要打印的内容放入其中,调用打印时只显示该区域。

<template>
  <div>
    <button @click="print">打印</button>
    <div class="print-area" ref="printArea">
      <!-- 打印内容 -->
      <h1>打印模板</h1>
      <p>这里是需要打印的内容</p>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    print() {
      const printArea = this.$refs.printArea;
      const printWindow = window.open('', '', 'width=800,height=600');
      printWindow.document.write(printArea.innerHTML);
      printWindow.document.close();
      printWindow.focus();
      printWindow.print();
      printWindow.close();
    }
  }
};
</script>

<style>
@media print {
  body * {
    visibility: hidden;
  }
  .print-area, .print-area * {
    visibility: visible;
  }
  .print-area {
    position: absolute;
    left: 0;
    top: 0;
  }
}
</style>

使用第三方库 vue-print-nb

vue-print-nb 是一个专门为 Vue 设计的打印插件,使用简单方便。

安装:

npm install vue-print-nb --save

使用:

<template>
  <div>
    <button v-print="printObj">打印</button>
    <div id="printContent">
      <!-- 打印内容 -->
      <h1>打印模板</h1>
      <p>使用 vue-print-nb 打印的内容</p>
    </div>
  </div>
</template>

<script>
import print from 'vue-print-nb'
export default {
  directives: {
    print
  },
  data() {
    return {
      printObj: {
        id: 'printContent',
        popTitle: '打印标题'
      }
    }
  }
}
</script>

使用 CSS 控制打印样式

通过 CSS 的 @media print 可以专门设置打印时的样式,隐藏不需要打印的元素,调整打印布局。

@media print {
  .no-print {
    display: none;
  }
  .print-only {
    display: block;
  }
  body {
    font-size: 12pt;
    color: #000;
  }
}

动态生成打印内容

对于需要动态生成的打印内容,可以使用 Vue 的数据绑定和计算属性来生成打印模板。

<template>
  <div>
    <button @click="printDynamic">打印动态内容</button>
    <div ref="dynamicPrintArea">
      <h1>{{ title }}</h1>
      <ul>
        <li v-for="item in items" :key="item.id">{{ item.name }}</li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: '动态打印内容',
      items: [
        { id: 1, name: '项目1' },
        { id: 2, name: '项目2' }
      ]
    }
  },
  methods: {
    printDynamic() {
      const printWindow = window.open('', '', 'width=800,height=600');
      printWindow.document.write(`
        <html>
          <head>
            <title>打印预览</title>
            <style>
              body { font-family: Arial; }
              h1 { color: #333; }
              ul { list-style: none; }
            </style>
          </head>
          <body>
            ${this.$refs.dynamicPrintArea.innerHTML}
          </body>
        </html>
      `);
      printWindow.document.close();
      printWindow.print();
    }
  }
}
</script>

打印 PDF 文件

如果需要生成 PDF 而非直接打印,可以使用 jsPDF 或 html2pdf 等库。

安装 html2pdf:

npm install html2pdf.js

使用:

vue实现打印模板

<template>
  <div>
    <button @click="exportToPDF">导出PDF</button>
    <div id="pdfContent">
      <!-- PDF内容 -->
      <h1>PDF模板</h1>
      <p>将内容导出为PDF文件</p>
    </div>
  </div>
</template>

<script>
import html2pdf from 'html2pdf.js'
export default {
  methods: {
    exportToPDF() {
      const element = document.getElementById('pdfContent');
      const opt = {
        margin: 10,
        filename: 'document.pdf',
        image: { type: 'jpeg', quality: 0.98 },
        html2canvas: { scale: 2 },
        jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' }
      };
      html2pdf().from(element).set(opt).save();
    }
  }
}
</script>

以上方法可以根据具体需求选择使用,简单打印需求可使用原生方法,复杂需求可借助第三方库实现更专业的打印效果。

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

相关文章

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue实现computed

vue实现computed

Vue 中 computed 的实现原理 computed 属性是 Vue 的一个核心特性,它基于响应式依赖进行缓存,仅在依赖发生变化时重新计算。以下是 computed 的实现方式: 基本用法 在…

vue实现刷新

vue实现刷新

Vue 实现页面刷新的方法 在Vue中实现页面刷新可以通过多种方式,以下是几种常见的方法: 使用 window.location.reload() 通过调用浏览器的原生方法强制刷新当前页面:…

vue实现文章滚动

vue实现文章滚动

Vue 实现文章滚动效果 使用 CSS 实现基础滚动 通过 CSS 的 overflow 和 height 属性控制滚动区域,适用于静态内容滚动。在 Vue 模板中定义固定高度的容器,内容超出后自动滚…

vue实现肢体识别

vue实现肢体识别

Vue 中实现肢体识别的方法 在 Vue 项目中实现肢体识别通常需要结合第三方库或 API,例如 TensorFlow.js、MediaPipe 或 OpenCV。以下是几种常见的实现方式: 使用…

vue实现数据渲染

vue实现数据渲染

Vue 数据渲染的实现方式 Vue.js 提供了多种数据渲染的方式,包括插值表达式、指令和计算属性等。以下是常见的实现方法: 插值表达式 使用双大括号 {{ }} 将数据绑定到模板中,数据变化时会自…