Module: PDF::Impose::Ext::Page

Defined in:
lib/pdf/impose/ext.rb

Instance Method Summary collapse

Instance Method Details

#import_page(reader, page_number, dx, dy, width, height, mirror) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/pdf/impose/ext.rb', line 49

def import_page(reader, page_number, dx, dy, width, height, mirror)
  ref = reader.objects.page_references[page_number]
  return unless ref

  object = document.state.store.
           send(:load_object_graph, reader.objects, ref)

  merge_object_resources object

  contents = object.data[:Contents]
  contents = [contents] unless contents.is_a?(Array)

  contents.each do |content|
    content = content.extract_content_stream
    box = object.data[:MediaBox]

    scale = 1.0
    width_ratio = width.to_f / box[2]
    height_ratio = height.to_f / box[3]

    scale = [width_ratio, height_ratio].min

    document.save_graphics_state do
      document.translate dx, dy
      document.scale scale

      if mirror
        document.translate box[2], box[3]
        document.rotate 180
      end

      document.add_content content
    end
  end
end

#merge_object_resources(object) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pdf/impose/ext.rb', line 29

def merge_object_resources(object)
  object_resources = document.deref(object.data[:Resources])

  object_resources.keys.each do |resource|
    case object_resources[resource]
    when ::Hash then
      resources[resource] ||= {}
      resources[resource].update(object_resources[resource])
    when ::Array then
      resources[resource] ||= []
      resources[resource] |= object_resources[resource]
    when PDF::Core::Reference then
      resources[resource] = object_resources[resource]
    else
      klass = object_resources[resource].class
      abort "unknown resource type #{klass} for #{resource}"
    end
  end
end