Class: Pdfrb::Document::Portfolio

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/document/portfolio.rb

Overview

PDF Portfolio facade. A portfolio is a PDF with a /Collection dictionary on the Catalog that provides a collection view of embedded files (ISO 32000-2 ยง12.3.5).

Inspired by mn2pdf's PDFPortfolio (which uses PDFBox). Provides field schema, default view configuration, and per-item metadata.

Defined Under Namespace

Classes: Field, Item

Constant Summary collapse

DEFAULT_FIELDS =
[
  { name: "Name", type: :text }.freeze,
  { name: "Description", type: :text, subtype: :Description }.freeze,
  { name: "Size", type: :number }.freeze,
  { name: "Modified", type: :date, subtype: :CreationDate }.freeze,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Portfolio

Returns a new instance of Portfolio.



34
35
36
37
38
# File 'lib/pdfrb/document/portfolio.rb', line 34

def initialize(document)
  @document = document
  @items = []
  @schema_fields = DEFAULT_FIELDS.map { |f| Field.new(**f) }
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



32
33
34
# File 'lib/pdfrb/document/portfolio.rb', line 32

def document
  @document
end

#itemsObject (readonly)

Returns the value of attribute items.



32
33
34
# File 'lib/pdfrb/document/portfolio.rb', line 32

def items
  @items
end

#schema_fieldsObject (readonly)

Returns the value of attribute schema_fields.



32
33
34
# File 'lib/pdfrb/document/portfolio.rb', line 32

def schema_fields
  @schema_fields
end

Instance Method Details

#add_field(name, type: :text, subtype: nil, order: nil) ⇒ Object

Add a schema field. Order matters for display.



41
42
43
44
45
46
47
48
# File 'lib/pdfrb/document/portfolio.rb', line 41

def add_field(name, type: :text, subtype: nil, order: nil)
  @schema_fields << Field.new(
    name: name,
    type: type,
    subtype: subtype,
    order: order || @schema_fields.length
  )
end

#add_item(name, file_bytes, mime_type: nil, description: nil, fields: {}) ⇒ Object

Add an item (embedded file) to the portfolio.



51
52
53
54
55
56
57
58
59
60
# File 'lib/pdfrb/document/portfolio.rb', line 51

def add_item(name, file_bytes, mime_type: nil,
             description: nil, fields: {})
  @items << Item.new(
    name: name,
    file_bytes: file_bytes,
    mime_type: mime_type,
    description: description,
    fields: fields
  )
end

#commit!Object

Commit the portfolio configuration to the document. Sets /Collection on the Catalog, creates /Filespec for each item, and embeds the file data via /EF.



65
66
67
68
# File 'lib/pdfrb/document/portfolio.rb', line 65

def commit!
  catalog = document.catalog
  catalog.value[:Collection] = collection_dict
end