Day14 - PDF 加浮水印

前言

工作上在处理 API 取得的 PDF 後,要在上面增加浮水印,进而研究与纪录的处理方式

实作

会需要用到 combine_pdfprawn 这两个 Gem 处理

# Gemfile

gem 'combine_pdf', '~> 1.0', '>= 1.0.21'
gem 'prawn', '~> 2.4'

将增加浮水写成一个方法,范例的 PDF ,和实作的 pr

# app/features/pdf/add_watermark_pdf.rb

module Pdf
  class AddWatermarkPdf

    # input_file = Rails.root.join('data/river_demo_watermark.pdf')
    # output_file = input_file.dirname.join('done.pdf')
    # watermark_texts = %w[river_1 river_2 river_3]
    def execute(input_file, output_file, watermark_texts = [])
      tmp_watermark_file(input_file)              # 暂存的浮水印 PDF path
      generate_watermark_content(watermark_texts) # 产浮水印的 PDF
      combine_pdf(input_file, output_file)        # 将 input_file 与浮水印 PDF 合并
      FileUtils.rm_rf(@tmp_watermark_file)        # 移除暂存的浮水印 PDF
    end

    private

    def tmp_watermark_file(input_file)
      @tmp_watermark_file ||= input_file.dirname.join('tmp_watermark.pdf')
    end

    # 可自行设定字型大小、颜色等
    # Prawn Documentation: https://prawnpdf.org/api-docs
    # Prawn example: https://prawnpdf.org/manual.pdf
    def generate_watermark_content(watermark_texts)
      position, amount_per_page = watermark_args
      Prawn::Document.generate(@tmp_watermark_file) do
        watermark_texts.each_with_index do |watermark_text, index|
          page = index / amount_per_page + 1
          coordinate = position[index % amount_per_page]
          start_new_page if page != 1 && (index % amount_per_page).zero?
          go_to_page(page)
          float do
            bounding_box(coordinate, height: 14, width: 48) do
              fill_color '000000'
              fill_rectangle [0, bounds.height], bounds.width, bounds.height
              stroke_bounds
              fill_color 'FFFFFF'
              text watermark_text.to_s, align: :center, valign: :center, size: 12
            end
          end
        end
      end
    end

    # 浮水印在页面上的位置、每页的数量
    def watermark_args
      position = [[0, 769], [0, 392]] # xy 轴座标 (需自行计算适合的位置)
      amount_per_page = 2             # 每页的浮水印数量

      [position, amount_per_page]
    end

    def combine_pdf(input_file, output_file)
      pdf_pages = CombinePDF.load(@tmp_watermark_file).pages
      pdf = CombinePDF.load(input_file)
      pdf.pages.each_with_index { |page, index| page << pdf_pages[index] }
      pdf.save(output_file)
    end
  end
end

完成上述步骤後,接着在 rails console 输入以下,便大功告成

# rails console

input_file = Rails.root.join('data/river_demo_watermark.pdf')
output_file = input_file.dirname.join('done.pdf')
watermark_texts = %w[river_1 river_2 river_3]
Pdf::AddWatermarkPdf.new.execute(input_file, output_file, watermark_texts)

示范的 PDF

加上浮水印後

小结

上面的范例为一页有多处需要加浮水印,且每个位置的浮水印内容皆不一样,因此在处理上会略为复杂些,若每页只需加一处且浮水印内容一样,处理上会简单一点

目前想到在 PDF 上加浮水印的做法是,将来源的 PDF、浮水印的 PDF 合并,合并後再把暂存浮水印 PDF 删除,若有更好的做法,欢迎留言和我说

至於如何知道浮水印的位置,「trial and error」会是一个好方法 xD

参考资料

  1. combine_pdf GitHub
  2. Prawn GitHub
  3. Prawn example

铁人赛文章连结:https://ithelp.ithome.com.tw/articles/10271878
medium 文章连结:https://link.medium.com/uqXuO7EVPjb
本文同步发布於 小菜的 Blog https://riverye.com/

备注:之後文章修改更新,以个人部落格为主


<<:  [从0到1] C#小乳牛 练成基础程序逻辑 Day 12 - 四大套路 读懂程序码 Sequence

>>:  Day 27 : 使用 TensorFlow Serving 部署 REST API

用 Line LIFF APP 实现信箱验证绑定功能(2) - 使用 Vite 快速打造输入页面

昨天提到,LIFF APP 有可能因为使用者没有绑定 email,或是不授权 email 使用导致无...

Day 10:让你见识我的一小部分力量,Ktor的网路请求

Keyword: Ktor, Suspend Function 到Day11使用Ktor进行网路请求...

[Day15] CSV Injection(Formula Injection)

前言 写完一半了,洒花 你的使用的Sheet Application安全吗? 正文 概念 CSV是一...

#7 Web Layout: CSS Fundamentals

Final Design CSS Web Layout Tips 1. Horizontally c...

[询问]网路分析仪

前辈们, 是否有推荐的网路分析仪呢? 目前察到LinkRunner™ G2 Network Auto...