optimize build annotations

pull/220/head^2
Pete Matsyburka 2 years ago
parent 0171b0c2dd
commit e79306f8a3

@ -5,12 +5,10 @@ module Templates
module_function module_function
def call(data) def call(data)
pdf = PDF::Reader.new(StringIO.new(data)) pdf = HexaPDF::Document.new(io: StringIO.new(data))
pdf.pages.flat_map.with_index do |page, index| pdf.pages.flat_map.with_index do |page, index|
annotations = page.objects.deref!(page.attributes[:Annots]) || [] (page[:Annots] || []).filter_map do |annot|
annotations.filter_map do |annot|
next if annot.nil?
next if annot[:A].blank? || annot[:A][:URI].blank? next if annot[:A].blank? || annot[:A][:URI].blank?
next unless annot[:Subtype] == :Link next unless annot[:Subtype] == :Link
next if !annot[:A][:URI].starts_with?('https://') && !annot[:A][:URI].starts_with?('http://') next if !annot[:A][:URI].starts_with?('https://') && !annot[:A][:URI].starts_with?('http://')
@ -30,10 +28,10 @@ module Templates
{ {
'type' => 'external_link', 'type' => 'external_link',
'value' => annot[:A][:URI], 'value' => annot[:A][:URI],
'x' => left / page.width.to_f, 'x' => left / page.box.width.to_f,
'y' => (page.height - top) / page.height.to_f, 'y' => (page.box.height - top) / page.box.height.to_f,
'w' => (right - left) / page.width.to_f, 'w' => (right - left) / page.box.width.to_f,
'h' => (top - bottom) / page.height.to_f 'h' => (top - bottom) / page.box.height.to_f
} }
end end
end end

Loading…
Cancel
Save