当前位置:首页 > VUE

vue实现打印和预览

2026-02-23 08:24:41VUE

Vue 实现打印功能

使用 window.print() 方法结合 CSS 控制打印样式是最基础的打印方案。Vue 中可以封装为组件或方法。

安装打印依赖库(可选):

npm install vue-print-nb

注册插件(全局):

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

模板中使用指令:

<button v-print="'#printArea'">打印</button>
<div id="printArea">
  <!-- 需要打印的内容 -->
</div>

自定义打印样式:

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

Vue 实现预览功能

使用 iframe 或 PDF 预览库实现内容预览。

vue实现打印和预览

安装 PDF 预览库:

npm install vue-pdf

组件中使用 PDF 预览:

<template>
  <pdf :src="pdfUrl" style="width: 100%; height: 800px;"></pdf>
</template>

<script>
import pdf from 'vue-pdf'
export default {
  components: { pdf },
  data() {
    return {
      pdfUrl: '/example.pdf'
    }
  }
}
</script>

HTML 内容预览方案:

vue实现打印和预览

<div class="preview-container" v-html="previewContent"></div>

<style>
.preview-container {
  border: 1px solid #eee;
  padding: 20px;
  background: white;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
</style>

打印与预览组合实现

创建可复用组件:

<template>
  <div>
    <button @click="showPreview">预览</button>
    <button v-print="printConfig">打印</button>

    <div v-if="isPreviewVisible" class="preview-modal">
      <div class="preview-content" id="printContent">
        <!-- 动态插槽内容 -->
        <slot></slot>
      </div>
      <button @click="hidePreview">关闭</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isPreviewVisible: false,
      printConfig: {
        id: 'printContent',
        popTitle: '文档打印',
        extraCss: 'https://example.com/print.css'
      }
    }
  },
  methods: {
    showPreview() {
      this.isPreviewVisible = true
    },
    hidePreview() {
      this.isPreviewVisible = false
    }
  }
}
</script>

<style>
.preview-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.preview-content {
  background: white;
  padding: 20px;
  max-width: 80%;
  max-height: 80vh;
  overflow: auto;
}
</style>

高级打印控制

分页打印控制:

@media print {
  .page-break {
    page-break-after: always;
  }
}

打印事件监听:

window.addEventListener('beforeprint', () => {
  console.log('打印前触发')
})
window.addEventListener('afterprint', () => {
  console.log('打印后触发')
})

打印参数配置示例:

const printSettings = {
  silent: false,
  styles: [
    'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css'
  ],
  timeout: 1000,
  debug: false
}

标签: vue
分享给朋友:

相关文章

vue实现分屏

vue实现分屏

Vue 实现分屏的方法 分屏功能通常用于展示多个独立内容区域,以下是几种常见的实现方式: 使用 CSS Grid 布局 通过 CSS Grid 可以轻松创建分屏布局,在 Vue 组件中直接应用样式:…

vue实现框架

vue实现框架

Vue 框架实现的核心方法 基础项目搭建 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 适合传统项目,Vite 更适合现代轻量级应用。安装后通过命令行工具选择所需配置(如 Rou…

vue实现grid

vue实现grid

Vue 实现 Grid 布局的方法 使用 CSS Grid 布局 Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。 &l…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安装…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…

vue实现popper

vue实现popper

Vue 实现 Popper 的方法 使用 Tippy.js 库 Tippy.js 是一个轻量级的 Popper.js 封装库,提供丰富的工具提示功能。安装 Tippy.js 及其 Vue 封装: n…