当前位置:首页 > VUE

vue实现pdf水印

2026-02-18 14:08:19VUE

实现思路

在Vue中实现PDF水印通常涉及两种方式:前端生成水印后叠加到PDF上,或后端处理PDF文件添加水印。以下是两种方案的实现方法。

前端方案(基于PDF.js)

使用PDF.js库渲染PDF,通过Canvas叠加水印层。

安装依赖:

npm install pdfjs-dist

示例代码:

<template>
  <div class="pdf-container">
    <canvas ref="pdfCanvas"></canvas>
    <canvas ref="watermarkCanvas" class="watermark-layer"></canvas>
  </div>
</template>

<script>
import * as pdfjsLib from 'pdfjs-dist'

export default {
  props: ['pdfUrl'],
  mounted() {
    this.renderPdf()
  },
  methods: {
    async renderPdf() {
      const loadingTask = pdfjsLib.getDocument(this.pdfUrl)
      const pdf = await loadingTask.promise
      const page = await pdf.getPage(1)

      const viewport = page.getViewport({ scale: 1.5 })
      const canvas = this.$refs.pdfCanvas
      const context = canvas.getContext('2d')

      canvas.height = viewport.height
      canvas.width = viewport.width

      await page.render({
        canvasContext: context,
        viewport: viewport
      }).promise

      this.addWatermark()
    },

    addWatermark() {
      const canvas = this.$refs.watermarkCanvas
      const pdfCanvas = this.$refs.pdfCanvas
      canvas.width = pdfCanvas.width
      canvas.height = pdfCanvas.height

      const ctx = canvas.getContext('2d')
      ctx.font = '20px Arial'
      ctx.fillStyle = 'rgba(200, 200, 200, 0.5)'
      ctx.rotate(-0.785)

      for (let x = 0; x < canvas.width; x += 200) {
        for (let y = 0; y < canvas.height; y += 100) {
          ctx.fillText('CONFIDENTIAL', x, y)
        }
      }
    }
  }
}
</script>

<style>
.pdf-container {
  position: relative;
}
.watermark-layer {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
}
</style>

后端方案(Node.js)

使用pdf-lib等库处理PDF文件。

安装依赖:

npm install pdf-lib

示例代码:

const { PDFDocument, rgb } = require('pdf-lib')
const fs = require('fs')

async function addWatermark(inputPath, outputPath) {
  const pdfBytes = fs.readFileSync(inputPath)
  const pdfDoc = await PDFDocument.load(pdfBytes)

  const pages = pdfDoc.getPages()
  pages.forEach(page => {
    const { width, height } = page.getSize()
    page.drawText('CONFIDENTIAL', {
      x: width / 2,
      y: height / 2,
      size: 50,
      opacity: 0.5,
      rotate: Math.PI / -4,
      color: rgb(0.9, 0.9, 0.9)
    })
  })

  const modifiedPdf = await pdfDoc.save()
  fs.writeFileSync(outputPath, modifiedPdf)
}

注意事项

前端方案的水印容易被移除,适合展示场景。后端方案生成的水印更持久,但需要服务器支持。水印内容应考虑使用半透明、倾斜排版等方式增加美观性和防篡改性。

vue实现pdf水印

对于生产环境,建议结合两种方案:前端展示时添加临时水印,下载时通过后端API生成带永久水印的PDF。

标签: 水印vue
分享给朋友:

相关文章

vue权限实现

vue权限实现

Vue 权限实现方案 基于路由的权限控制 通过路由守卫实现权限验证,在路由配置中添加meta字段标记权限要求。在router.beforeEach中检查用户权限与路由要求的匹配情况。 // rout…

vue实现ssr

vue实现ssr

Vue 实现 SSR(服务端渲染) Vue 的服务端渲染(SSR)允许在服务器端生成完整的 HTML 内容,再发送到客户端,提升首屏加载速度和 SEO 友好性。以下是实现 Vue SSR 的关键步骤和…

vue实现链接

vue实现链接

Vue 实现链接的方法 在 Vue 中实现链接可以通过多种方式,包括使用 <router-link>、<a> 标签或编程式导航。以下是几种常见的方法: 使用 <rout…

vue实现注册

vue实现注册

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

vue实现上划

vue实现上划

Vue 实现上划功能 在Vue中实现上划功能,可以通过监听触摸事件(touchstart、touchmove、touchend)来判断用户的手势方向。以下是实现上划功能的几种方法: 监听触摸事件 通…

vue下载实现

vue下载实现

Vue 下载实现方法 在 Vue 项目中实现文件下载功能可以通过多种方式完成,以下是几种常见的方法: 使用 <a> 标签实现下载 通过动态创建 <a> 标签并设置 href…