gdelugre / origami

Origami is a pure Ruby library to parse, modify and generate PDF documents.
GNU Lesser General Public License v3.0
325 stars 110 forks source link

Encryption not compatible with XFA forms (undefined method `string_encryption_cipher' for nil:NilClass) #27

Closed salpha2004 closed 5 years ago

salpha2004 commented 7 years ago

Origami v2.0.4

encryption.rb:

#!/usr/bin/env ruby
require 'origami'
include Origami

pdf = PDF.new.encrypt(cipher: 'aes', key_size: 256)

contents = ContentStream.new
contents.write "Encrypted document sample",
    x: 100, y: 750, rendering: Text::Rendering::STROKE, size: 30
pdf.append_page Page.new.setContents(contents)

form1 = Field::Subform.new(T: "form1[0]")
form1.add_fields(subform = Field::Subform.new(T: "#subform[0]"))
xdp = Origami::XDP::Package.new('').to_s

pdf.create_xfa_form(xdp, form1) # commenting out this line removes the problem

pdf.save("encryption.pdf")
$ ruby encryption.rb
/usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/encryption.rb:470:in `encrypt!': undefined method `string_encryption_cipher' for nil:NilClass (NoMethodError)
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/encryption.rb:429:in `post_build'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:756:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/encryption.rb:418:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:778:in `block in build_compound_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:777:in `each_value'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:777:in `build_compound_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:753:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/encryption.rb:418:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:778:in `block in build_compound_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:777:in `each'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:777:in `build_compound_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:753:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/encryption.rb:418:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:778:in `block in build_compound_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:777:in `each_value'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:777:in `build_compound_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:753:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/encryption.rb:418:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:771:in `block in build_compound_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:764:in `map!'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:764:in `build_compound_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:753:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/encryption.rb:418:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:778:in `block in build_compound_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:777:in `each_value'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:777:in `build_compound_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:753:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/encryption.rb:418:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:771:in `block in build_compound_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/dictionary.rb:138:in `block in map!'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/dictionary.rb:137:in `each_pair'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/dictionary.rb:137:in `map!'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:764:in `build_compound_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:753:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/encryption.rb:418:in `build_object'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:733:in `block in physicalize'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:732:in `each'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:732:in `physicalize'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/encryption.rb:397:in `physicalize'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:713:in `compile'
    from /usr/local/rvm/gems/ruby-2.4.1/gems/origami-2.0.4/lib/origami/pdf.rb:221:in `save'
    from encryption.rb:18:in `<main>'
gdelugre commented 7 years ago

This should be fixed in version 2.1