10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/docpdf/adapters/stampers/hexapdf.rb', line 10
def stamp(data, stamps, page_indices: nil)
doc = HexaPDF::Document.new(io: StringIO.new(data))
source_page = doc.pages[0]
raise ConversionError, "Cannot stamp a PDF with no pages" unless source_page
page_w = source_page.box.width
page_h = source_page.box.height
stamp_page_data = generate_stamp_page(stamps, page_w, page_h)
stamp_doc = HexaPDF::Document.new(io: StringIO.new(stamp_page_data))
stamp_form = doc.import(stamp_doc.pages[0].to_form_xobject)
doc.pages.each_with_index do |page, idx|
page.canvas(type: :overlay).xobject(stamp_form, at: [0, 0]) if page_indices.nil? || page_indices.include?(idx)
end
write_to_string(doc)
rescue HexaPDF::Error, Errno::ENOENT => e
raise ConversionError, "HexaPDF failed to stamp PDF: #{e.message}"
end
|