Files

41 lines
1.2 KiB
Ruby
Raw Permalink Normal View History

2023-09-01 01:27:13 +03:00
# frozen_string_literal: true
module Templates
module BuildAnnotations
module_function
def call(data)
2024-03-14 21:30:57 +02:00
pdf = HexaPDF::Document.new(io: StringIO.new(data))
2023-09-01 01:27:13 +03:00
pdf.pages.flat_map.with_index do |page, index|
2024-03-14 21:30:57 +02:00
(page[:Annots] || []).filter_map do |annot|
2024-03-21 16:25:49 +02:00
next if annot.blank?
2026-03-23 09:27:12 +02:00
next if annot.is_a?(Integer) || annot.is_a?(Symbol) || annot.is_a?(HexaPDF::PDFArray)
2023-09-01 01:27:13 +03:00
next if annot[:A].blank? || annot[:A][:URI].blank?
next unless annot[:Subtype] == :Link
next if !annot[:A][:URI].starts_with?('https://') && !annot[:A][:URI].starts_with?('http://')
build_external_link_hash(page, annot).merge('page' => index)
end
end
2023-11-20 00:04:20 +02:00
rescue StandardError => e
Rollbar.error(e) if defined?(Rollbar)
2023-09-17 17:00:21 +03:00
[]
2023-09-01 01:27:13 +03:00
end
def build_external_link_hash(page, annot)
left, bottom, right, top = annot[:Rect]
{
'type' => 'external_link',
'value' => annot[:A][:URI],
2024-03-14 21:30:57 +02:00
'x' => left / page.box.width.to_f,
'y' => (page.box.height - top) / page.box.height.to_f,
'w' => (right - left) / page.box.width.to_f,
'h' => (top - bottom) / page.box.height.to_f
2023-09-01 01:27:13 +03:00
}
end
end
end