Module: PdfWatermark

Includes:
HexaPDF::Encryption::StandardSecurityHandler::Permissions
Defined in:
lib/pdf_watermark.rb,
lib/pdf_watermark/font.rb,
lib/pdf_watermark/version.rb,
lib/pdf_watermark/water_mark/base.rb,
lib/pdf_watermark/water_mark/single.rb,
lib/pdf_watermark/water_mark/repeated.rb

Defined Under Namespace

Modules: Canvas, Font, FontDescriptor, WaterMark

Constant Summary collapse

LIB_DIR =
File.dirname(File.realpath(__FILE__))
BASEDIR =
File.realpath File.join(LIB_DIR, '..')
FONT_DIR =
File.realpath File.join(LIB_DIR, '..', 'fonts')
REPEAT_X_OFFSET =
80
REPEAT_Y_OFFSET =
80
VERSION =
"0.2.3"

Class Method Summary collapse

Class Method Details

.page_size(page) ⇒ Object



53
54
55
56
# 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) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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