Module: Prawn::Attachment

Includes:
PDF::Core::EmbeddedFiles
Defined in:
lib/prawn/attachment.rb,
lib/prawn/attachment/version.rb,
lib/prawn/attachment/filespec.rb,
lib/prawn/attachment/embedded_file.rb

Overview

Module that supports attachment of file data into a Prawn::Document

Defined Under Namespace

Classes: EmbeddedFile, Filespec, InvalidDataError, NoDataError

Constant Summary collapse

VERSION =
"0.3.1"

Constants included from PDF::Core::EmbeddedFiles

PDF::Core::EmbeddedFiles::NAME_TREE_CHILDREN_LIMIT

Instance Method Summary collapse

Methods included from PDF::Core::EmbeddedFiles

#add_embedded_file, #embedded_files

Instance Method Details

#attach(name, src, opts = {}) ⇒ Object

Attach a file’s data to the document. File IO Objects are expected.

Arguments:

name

name of attachment.

src

String or IO object containing source file data.

Options:

:created_at

timestamp when the file was created.

:modified_at

timestamp for when file was last modified.

:description

file description.

:hidden

if true, prevents the file from appearing in the

catalog. (default false)

Prawn::Document.generate("file1.pdf") do
  path = "#{Prawn::DATADIR}/images/dice.png"
  attach "dice.png", File.open(path), description: 'Example of an attached image file'
end

This method returns an instance of PDF::Core::NameTree::Value corresponding to the file in the attached files catalog entry node. If hidden, then nil is returned.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/prawn/attachment.rb', line 51

def attach(name, src, opts = {})
  data = extract_data_from_source(src)
  opts = prepare_options(name, opts)

  # Prepare embeddable representation of the source data
  file = EmbeddedFile.new(data, opts)

  filespec = Filespec.new(file_obj_from_registry(file), opts)
  filespec_obj = filespec.build_pdf_object(self)

  attach_file(filespec.file_name, filespec_obj) unless filespec.hidden?
end