Class: Rpdfium::Attachment
- Inherits:
-
Object
- Object
- Rpdfium::Attachment
- Defined in:
- lib/rpdfium/structure/attachment.rb
Overview
File embedded nel PDF (allegati). PDFium li espone via FPDFDoc_GetAttachment.
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#handle ⇒ Object
readonly
Returns the value of attribute handle.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
Instance Method Summary collapse
-
#bytes ⇒ Object
Ritorna i bytes del file allegato.
-
#initialize(document, index) ⇒ Attachment
constructor
A new instance of Attachment.
- #name ⇒ Object
- #save(path) ⇒ Object
Constructor Details
#initialize(document, index) ⇒ Attachment
Returns a new instance of Attachment.
8 9 10 11 12 13 |
# File 'lib/rpdfium/structure/attachment.rb', line 8 def initialize(document, index) @document = document @index = index @handle = Raw.FPDFDoc_GetAttachment(document.handle, index) raise Error, "Attachment #{index} not found" if @handle.null? end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
6 7 8 |
# File 'lib/rpdfium/structure/attachment.rb', line 6 def document @document end |
#handle ⇒ Object (readonly)
Returns the value of attribute handle.
6 7 8 |
# File 'lib/rpdfium/structure/attachment.rb', line 6 def handle @handle end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
6 7 8 |
# File 'lib/rpdfium/structure/attachment.rb', line 6 def index @index end |
Instance Method Details
#bytes ⇒ Object
Ritorna i bytes del file allegato. Pattern probe-then-fetch.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/rpdfium/structure/attachment.rb', line 20 def bytes out_size = FFI::MemoryPointer.new(:ulong) Raw.FPDFAttachment_GetFile(@handle, FFI::Pointer::NULL, 0, out_size) n = out_size.read_ulong return "" if n.zero? buf = FFI::MemoryPointer.new(:uchar, n) Raw.FPDFAttachment_GetFile(@handle, buf, n, out_size) # Leggo n byte (la dimensione del MIO buffer), non out_size.read_ulong: # PDFium può aggiornare out_size con un valore diverso da n (es. dim # totale necessaria) che leggerebbe oltre il buffer → IndexError. # Se la write effettiva è < n, riempie il resto con NUL. buf.read_bytes(n) end |
#name ⇒ Object
15 16 17 |
# File 'lib/rpdfium/structure/attachment.rb', line 15 def name Raw.read_utf16_string(:FPDFAttachment_GetName, @handle) end |
#save(path) ⇒ Object
35 36 37 38 |
# File 'lib/rpdfium/structure/attachment.rb', line 35 def save(path) File.binwrite(path, bytes) path end |