Class: HexaPDF::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/writer.rb

Overview

Writes the contents of a PDF document to an IO stream.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, io) ⇒ Writer

Creates a new writer object for the given HexaPDF document that gets written to the IO object.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/hexapdf/writer.rb', line 59

def initialize(document, io)
  @document = document
  @io = io

  @io.binmode
  @io.seek(0, IO::SEEK_SET) # TODO: incremental update!

  @serializer = Serializer.new
  @serializer.encrypter = @document.encrypted? ? @document.security_handler : nil
  @rev_size = 0
end

Class Method Details

.write(document, io, incremental: false) ⇒ Object

Writes the document to the IO object. If incremental is true and the document was created from an existing PDF file, the changes are appended to a full copy of the source document.



49
50
51
52
53
54
55
# File 'lib/hexapdf/writer.rb', line 49

def self.write(document, io, incremental: false)
  if incremental && document.revisions.parser
    new(document, io).write_incremental
  else
    new(document, io).write
  end
end

Instance Method Details

#writeObject

Writes the document to the IO object.



72
73
74
75
76
77
78
79
80
# File 'lib/hexapdf/writer.rb', line 72

def write
  write_file_header

  pos = nil
  @document.trailer.info[:Producer] = "HexaPDF version #{HexaPDF::VERSION}"
  @document.revisions.each do |rev|
    pos = write_revision(rev, pos)
  end
end

#write_incrementalObject

Writes the complete source document and one revision containing all changes to the IO.

For this method to work the document must have been created from an existing file.



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hexapdf/writer.rb', line 85

def write_incremental
  @document.revisions.parser.io.seek(0, IO::SEEK_SET)
  IO.copy_stream(@document.revisions.parser.io, @io)

  @rev_size = @document.revisions.current.next_free_oid

  revision = Revision.new(@document.revisions.current.trailer)
  @document.revisions.each do |rev|
    rev.each_modified_object {|obj| revision.send(:add_without_check, obj) }
  end
  write_revision(revision, @document.revisions.parser.startxref_offset)
end