Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method `[]=' for nil:NilClass in trailer.rb for PDF#sign #16

Open
JasonBarnabe opened this issue May 23, 2017 · 4 comments
Open

Comments

@JasonBarnabe
Copy link

require 'origami'
include Origami
input_file = 'test.pdf'
output_file = '/tmp/test_signed.pdf'
private_key = File.read('keys/server.key')
certificate = File.read('keys/server.crt')
passphrase = ''
key = OpenSSL::PKey::RSA.new private_key, passphrase
cert = OpenSSL::X509::Certificate.new(certificate)
pdf = PDF.read(input_file)
pdf.sign(cert, key,
         :method => 'adbe.pkcs7.sha1',
         :location => "Portugal",
         :contact => "myemail@email.tt",
         :reason => "Proof of Concept"
)

test.pdf

jasons-mbp:jason jason$ ruby test.rb 
[info ] ...Reading header...
[info ] ...Parsing revision 1...
[info ] ...Parsing xref table...
[warn ] Unable to parse xref table! Xrefs might be stored into an XRef stream.
[info ] ...Parsing trailer...
[info ] ...Parsing revision 2...
[info ] ...Parsing xref table...
[warn ] Unable to parse xref table! Xrefs might be stored into an XRef stream.
[info ] ...Parsing trailer...
[warn ] Found a XRefStream for this revision at 274 0 R
[warn ] This file has been linearized.
[info ] ...Propagating types...
/Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.2/lib/origami/trailer.rb:143:in `[]=': undefined method `[]=' for nil:NilClass (NoMethodError)
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.2/lib/origami/object.rb:193:in `block in define_field_methods'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.2/lib/origami/pdf.rb:1044:in `block in rebuild_dummy_xrefs'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.2/lib/origami/pdf.rb:1035:in `each'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.2/lib/origami/pdf.rb:1035:in `rebuild_dummy_xrefs'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.2/lib/origami/signature.rb:177:in `sign'
	from test.rb:11:in `<main>'

The error seems to happen because dictionary is nil and nil && Dictionary.new(dictionary) is nil, but I'm not sure if this is only happening because my document is invalid or needs to be preprocessed first...

@JasonBarnabe
Copy link
Author

I found that I could read this document with origami by running it through gs -o output.pdf -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress input.pdf first. So not sure if this is a known "limitation" or what, but perhaps this would be a good suggestion for the readme?

@gdelugre
Copy link
Owner

This problem should now be fixed in 2.0.3.

@JasonBarnabe
Copy link
Author

With 2.0.3 the pdf.sign call no longer fails, but if I add pdf.save(output_file) at the end of the script above, I get:

TypeError: no implicit conversion of nil into String
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/filters/predictors.rb:191:in `+'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/filters/predictors.rb:191:in `png_pre_prediction'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/filters/predictors.rb:96:in `apply_pre_prediction'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/filters/predictors.rb:65:in `pre_prediction'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/filters/flate.rb:44:in `encode'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/filters.rb:353:in `encode'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/stream.rb:481:in `encode_data'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/stream.rb:379:in `block in encode!'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/stream.rb:367:in `downto'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/stream.rb:367:in `encode!'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/stream.rb:289:in `encoded_data'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/stream.rb:392:in `to_s'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/pdf.rb:929:in `block (2 levels) in output'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/pdf.rb:854:in `each'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/pdf.rb:854:in `block in output'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/pdf.rb:819:in `each'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/pdf.rb:819:in `output'
	from /Users/jason/.rvm/gems/ruby-2.3.3/gems/origami-2.0.3/lib/origami/pdf.rb:223:in `save'
	from (irb):17
	from /Users/jason/.rvm/rubies/ruby-2.3.3/bin/irb:11:in `<main>'

@zymtx5g79k
Copy link

2.1.0 Not resolved. But it works when lazy: true

Origami::PDF.read INPUTFILE, lazy: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants