当前位置:首页 > VUE

vue实现pdf打印开单

2026-01-23 15:59:48VUE

使用vue实现PDF打印开单功能

安装依赖库

需要安装html2canvasjspdf两个库用于将HTML转换为PDF

npm install html2canvas jspdf --save

创建打印组件

创建一个可复用的PDF打印组件,包含需要打印的内容区域和打印按钮

vue实现pdf打印开单

<template>
  <div>
    <div id="printArea" class="print-content">
      <!-- 这里是需要打印的内容 -->
      <h1>销售单据</h1>
      <table>
        <tr>
          <th>商品名称</th>
          <th>数量</th>
          <th>单价</th>
          <th>金额</th>
        </tr>
        <tr v-for="(item, index) in items" :key="index">
          <td>{{ item.name }}</td>
          <td>{{ item.quantity }}</td>
          <td>{{ item.price }}</td>
          <td>{{ item.price * item.quantity }}</td>
        </tr>
      </table>
    </div>
    <button @click="generatePDF">打印单据</button>
  </div>
</template>

<script>
import html2canvas from 'html2canvas'
import jsPDF from 'jspdf'

export default {
  data() {
    return {
      items: [
        { name: '商品A', quantity: 2, price: 100 },
        { name: '商品B', quantity: 1, price: 200 }
      ]
    }
  },
  methods: {
    generatePDF() {
      const element = document.getElementById('printArea')
      html2canvas(element).then(canvas => {
        const imgData = canvas.toDataURL('image/png')
        const pdf = new jsPDF('p', 'mm', 'a4')
        const imgWidth = 190
        const imgHeight = canvas.height * imgWidth / canvas.width

        pdf.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight)
        pdf.save('销售单据.pdf')
      })
    }
  }
}
</script>

<style>
.print-content {
  width: 100%;
  padding: 20px;
  background: white;
}
table {
  width: 100%;
  border-collapse: collapse;
}
th, td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}
</style>

优化打印样式

为打印内容添加专用样式,确保打印效果符合预期

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

添加打印参数配置

扩展生成PDF方法,支持自定义文件名、纸张方向和边距

vue实现pdf打印开单

generatePDF(fileName = '销售单据', orientation = 'p', margin = 10) {
  const element = document.getElementById('printArea')
  html2canvas(element, {
    scale: 2,
    logging: false,
    useCORS: true
  }).then(canvas => {
    const imgData = canvas.toDataURL('image/png')
    const pdf = new jsPDF(orientation, 'mm', 'a4')
    const imgWidth = pdf.internal.pageSize.getWidth() - margin * 2
    const imgHeight = canvas.height * imgWidth / canvas.width

    pdf.addImage(imgData, 'PNG', margin, margin, imgWidth, imgHeight)
    pdf.save(`${fileName}.pdf`)
  })
}

处理长内容分页

对于超出一页的内容,自动分页处理

generatePDF() {
  const element = document.getElementById('printArea')
  const pdf = new jsPDF('p', 'mm', 'a4')
  const margin = 10
  const pageWidth = pdf.internal.pageSize.getWidth()
  const pageHeight = pdf.internal.pageSize.getHeight()

  html2canvas(element, {
    scale: 2
  }).then(canvas => {
    const imgData = canvas.toDataURL('image/png')
    const imgWidth = pageWidth - margin * 2
    const imgHeight = canvas.height * imgWidth / canvas.width

    let heightLeft = imgHeight
    let position = margin
    let page = 1

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

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

    pdf.save('销售单据.pdf')
  })
}

使用注意事项

确保打印区域的内容在DOM加载完成后才执行转换操作,避免内容未完全渲染导致的打印不完整问题。对于动态数据,建议在数据加载完成后再触发打印操作。

打印样式建议使用固定单位(如mm、pt)而非响应式单位(如%、vw),以确保打印尺寸的准确性。测试不同浏览器的兼容性,特别是IE浏览器可能需要额外的polyfill支持。

标签: vuepdf
分享给朋友:

相关文章

vue请求实现进度条

vue请求实现进度条

实现请求进度条的方法 在Vue中实现请求进度条可以通过多种方式完成,常见的有使用第三方库如axios结合nprogress或自定义进度条组件。以下是几种实现方法: 使用axios和nprogress…

vue 实现blog

vue 实现blog

Vue 实现博客的基本步骤 使用 Vue 实现博客可以分为前端和后端两部分,前端使用 Vue.js 框架,后端可以选择 Node.js、Python 或其他服务端语言。以下是一个基于 Vue 的博客实…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API metho…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户名…

vue实现城市

vue实现城市

Vue 实现城市选择功能 使用 Element UI 的 Cascader 组件 Element UI 提供了一个 Cascader 级联选择器组件,非常适合实现城市选择功能。需要先安装 Elemen…

vue逻辑实现怎么实现

vue逻辑实现怎么实现

Vue 逻辑实现方法 Vue 的逻辑实现主要通过组件化、响应式数据、计算属性、方法、生命周期钩子等方式完成。以下是一些核心实现方法: 数据驱动与响应式 在 Vue 中,通过 data 选项定义响应式…