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
|