当前位置:首页 > 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 实现中台系统的关键步骤 技术选型与基础搭建 使用 Vue 3(Composition API)或 Vue 2(Options API)作为前端框架,搭配 Vue Router 实现路由管理,V…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在模…

简单实现vue

简单实现vue

Vue 的基本实现 创建一个简单的 Vue 应用需要引入 Vue 库,并编写基本的模板和数据绑定逻辑。 <!DOCTYPE html> <html lang="en"> &l…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signature_p…

vue实现长按

vue实现长按

Vue 实现长按功能的方法 在 Vue 中实现长按功能可以通过原生事件监听或自定义指令完成。以下是几种常见实现方式: 使用原生事件监听 通过 @mousedown 和 @mouseup 或 @tou…

vue实现浮标

vue实现浮标

Vue 实现浮动按钮(浮标) 使用 Vue 实现浮动按钮可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 定位和 Vue 组件 创建 Vue 组件并配合 CSS 固定定位实现浮动按钮:…