Skip to content

Commit

Permalink
Made trailer ID deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
Backbone81 committed Jul 13, 2017
1 parent c94f468 commit 0cae866
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/pdf/core/renderer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'stringio'
require 'securerandom'
require 'digest'

module PDF
module Core
Expand Down Expand Up @@ -163,9 +163,9 @@ def render(output = StringIO.new)
finalize_all_page_contents

render_header(output)
render_body(output)
body_hash = render_body(output)
render_xref(output)
render_trailer(output)
render_trailer(output, body_hash)
if output.instance_of?(StringIO)
str = output.string
str.force_encoding(::Encoding::ASCII_8BIT)
Expand Down Expand Up @@ -198,7 +198,13 @@ def render_header(output)
# Write out the PDF Body, as per spec 3.4.2
#
def render_body(output)
body_start = output.tell
state.render_body(output)
body_end = output.tell
output.seek body_start

This comment has been minimized.

Copy link
@pointlessone

pointlessone Jul 13, 2017

Member

Not all IOs are seekable. Sor example, STDOUT is not.

I'd rather split hashing out as well. One method renders body, another hashes it.

This comment has been minimized.

Copy link
@Backbone81

Backbone81 Jul 13, 2017

Author

Trailer ID creation does not use seek any more. Hashing is now separated from body render.

body_hash = Digest::MD5.digest output.read(body_end - body_start)
output.seek body_end
body_hash
end

# Write out the PDF Cross Reference Table, as per spec 3.4.3
Expand All @@ -216,8 +222,8 @@ def render_xref(output)

# Write out the PDF Trailer, as per spec 3.4.4
#
def render_trailer(output)
trailer_id = PDF::Core::ByteString.new(SecureRandom.random_bytes(16))
def render_trailer(output, body_hash)
trailer_id = PDF::Core::ByteString.new(body_hash)
trailer_hash = {
Size: state.store.size + 1,
Root: state.store.root,
Expand Down

0 comments on commit 0cae866

Please sign in to comment.