Method: Origami::PDF#attach_file

Defined in:
lib/origami/filespec.rb

#attach_file(path, register: true, name: nil, filter: :FlateDecode) ⇒ Object

Attachs an embedded file to the PDF.

path

The path to the file to attach.

register

Whether the file shall be registered in the name directory.

name

The embedded file name of the attachment.

filter

The stream filter used to store the file contents.



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
61
62
# File 'lib/origami/filespec.rb', line 32

def attach_file(path, register: true, name: nil, filter: :FlateDecode)

    if path.is_a? FileSpec
        filespec = path
        name ||= ''
    else
        if path.respond_to?(:read)
            data = path.read.force_encoding('binary')
            name ||= ''
        else
            data = File.binread(File.expand_path(path))
            name ||= File.basename(path)
        end

        fstream = EmbeddedFileStream.new
        fstream.data = data

        fstream.Filter = filter
        filespec = FileSpec.new(:F => fstream)
    end

    fspec = FileSpec.new.setType(:Filespec).setF(name.dup).setEF(filespec)

    self.register(
        Names::EMBEDDED_FILES,
        name.dup,
        fspec
    ) if register

    fspec
end