module PdfWatermark

Constants

BASEDIR
FONT_DIR
LIB_DIR
REPEAT_X_OFFSET
REPEAT_Y_OFFSET
VERSION

Public Class Methods

page_size(page) click to toggle source
# File lib/pdf_watermark.rb, line 53
def self.page_size(page)
  mediabox = page.mediabox
  [mediabox[2]-mediabox[0], mediabox[3] - mediabox[1]]
end
watermark(mark_string, source, destination = nil, options: {}, validate: true) click to toggle source
# File lib/pdf_watermark.rb, line 14
def self.watermark(mark_string, source, destination = nil, options: {}, validate: true)
  default={
    angle: :diagonal,
    margin: [10, 10, 10, 10],
    font: "#{FONT_DIR}/SourceHanSansSC-Regular.ttf",
    font_size: '12px',
    font_color: "999999",
    transparent: 0.2,
    align: :left,
    valign: :center,
    mode: :fill,
    max_font_size: 50,
    min_font_size: 15,
    repeat_offset: 4,
  }
  options = default.merge(options)

  if options[:mode].to_sym == :repeat
    document = PdfWatermark::WaterMark::Repeated.new(mark_string, source, options).render
  else
    document = PdfWatermark::WaterMark::Single.new(mark_string, source, options).render
  end

  if options[:read_only]
    permissions = 1
    document.encrypt(name: :Standard, owner_password: options[:password], permissions: permissions)
  end


  if destination
    document.write(destination, validate: validate)
  else
    StringIO.open('', 'wb') do |io|
      document.write(io, validate: validate)
      io.string
    end
  end
end