Class: Pdfrb::Document::Files

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pdfrb/document/files.rb

Overview

Attached-files facade. Embeds files as /Type /EmbeddedFile streams referenced by /Type /FileSpec dicts, stored in the Catalog's /Names /EmbeddedFiles name tree.

Per PDF 2.0 App Note 002, files can also be associated with specific PDF objects (pages, annotations) via the /AF array.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Files

Returns a new instance of Files.



16
17
18
# File 'lib/pdfrb/document/files.rb', line 16

def initialize(document)
  @document = document
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



14
15
16
# File 'lib/pdfrb/document/files.rb', line 14

def document
  @document
end

Instance Method Details

#[](name) ⇒ Object



75
76
77
# File 'lib/pdfrb/document/files.rb', line 75

def [](name)
  find { |n, _spec| n == name.to_s }&.last
end

#add(data, name:, mime_type: nil, description: nil, relationship: nil, associated_object: nil) ⇒ Pdfrb::Model::Cos::Dictionary

Embed a file. Returns the /FileSpec object.

Parameters:

  • data (String, IO)

    raw file contents (binary).

  • name (String)

    filename (used for /F and /UF).

  • mime_type (String, nil) (defaults to: nil)

    MIME type for /Subtype on EmbeddedFile.

  • description (String, nil) (defaults to: nil)

    human-readable /Desc.

  • relationship (Symbol, nil) (defaults to: nil)

    :Source, :Data, :Alternative, :Supplement, :EncryptedPayload (PDF 2.0 /AF relationship).

  • associated_object (Pdfrb::Model::Object, nil) (defaults to: nil)

    if set, adds this file to that object's /AF array.

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pdfrb/document/files.rb', line 31

def add(data, name:, mime_type: nil, description: nil,
        relationship: nil, associated_object: nil)
  raw = read_data(data)

  ef_stream = @document.add(
    { Type: :EmbeddedFile, Subtype: mime_type },
    type: Pdfrb::Model::Cos::Stream
  )
  ef_stream.stream = raw

  filespec = @document.add(
    {
      Type: :FileSpec,
      UF: name.to_s,
      EF: { UF: Pdfrb::Model::Reference.new(ef_stream.oid, ef_stream.gen),
            F: Pdfrb::Model::Reference.new(ef_stream.oid, ef_stream.gen) },
    },
    type: Pdfrb::Model::Cos::Dictionary
  )
  filespec.value[:F] = name.to_s if name.to_s.ascii_only?
  filespec.value[:Desc] = description if description

  register_in_names(name.to_s, filespec)

  if associated_object
    add_to_af(associated_object, filespec, relationship)
  end

  filespec
end

#countObject



79
80
81
# File 'lib/pdfrb/document/files.rb', line 79

def count
  to_a.length
end

#eachObject



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pdfrb/document/files.rb', line 62

def each
  return enum_for(:each) unless block_given?

  names_array = embedded_files_names_array
  return self unless names_array

  names_array.each_slice(2) do |name, ref|
    resolved = ref.is_a?(Pdfrb::Model::Reference) ? @document.object(ref) : ref
    yield(name.to_s, resolved) if resolved
  end
  self
end

#empty?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/pdfrb/document/files.rb', line 83

def empty?
  embedded_files_names_array.nil?
end