lib/origami/parsers/pdf.rb in origami-2.0.0 vs lib/origami/parsers/pdf.rb in origami-2.0.1
- old
+ new
@@ -25,17 +25,18 @@
class PDF
class Parser < Origami::Parser
def initialize(params = {})
options =
{
- password: '', # Default password being tried when opening a protected document.
- prompt_password: lambda do # Callback procedure to prompt password when document is encrypted.
- require 'io/console'
- STDERR.print "Password: "
- STDIN.noecho(&:gets).chomp
- end,
- force: false # Force PDF header detection
+ decrypt: true, # Attempt to decrypt to document if encrypted (recommended).
+ password: '', # Default password being tried when opening a protected document.
+ prompt_password: lambda do # Callback procedure to prompt password when document is encrypted.
+ require 'io/console'
+ STDERR.print "Password: "
+ STDIN.noecho(&:gets).chomp
+ end,
+ force: false # Force PDF header detection
}.update(params)
super(options)
end
@@ -60,35 +61,57 @@
pdf
end
def parse_finalize(pdf) #:nodoc:
+ cast_trailer_objects(pdf)
+
warn "This file has been linearized." if pdf.linearized?
propagate_types(pdf) if Origami::OPTIONS[:enable_type_propagation]
#
# Decrypt encrypted file contents
#
if pdf.encrypted?
warn "This document contains encrypted data!"
- passwd = @options[:password]
- begin
- pdf.decrypt(passwd)
- rescue EncryptionInvalidPasswordError
- if passwd.empty?
- passwd = @options[:prompt_password].call
- retry unless passwd.empty?
- end
-
- raise
- end
+ decrypt_document(pdf) if @options[:decrypt]
end
warn "This document has been signed!" if pdf.signed?
pdf
+ end
+
+ def cast_trailer_objects(pdf) #:nodoc:
+ trailer = pdf.trailer
+
+ if trailer[:Root].is_a?(Reference)
+ pdf.cast_object(trailer[:Root], Catalog)
+ end
+
+ if trailer[:Info].is_a?(Reference)
+ pdf.cast_object(trailer[:Info], Metadata)
+ end
+
+ if trailer[:Encrypt].is_a?(Reference)
+ pdf.cast_object(trailer[:Encrypt], Encryption::Standard::Dictionary)
+ end
+ end
+
+ def decrypt_document(pdf) #:nodoc:
+ passwd = @options[:password]
+ begin
+ pdf.decrypt(passwd)
+ rescue EncryptionInvalidPasswordError
+ if passwd.empty?
+ passwd = @options[:prompt_password].call
+ retry unless passwd.empty?
+ end
+
+ raise
+ end
end
end
end
end