【问题标题】:Generating PDF's with signatures using Ruby or Ruby on Rails使用 Ruby 或 Ruby on Rails 生成带有签名的 PDF
【发布时间】:2011-03-07 14:50:19
【问题描述】:

我正在开发一个使用 prawn 生成 PDF 的 Rails 应用程序。长话短说,我们希望能够对生成的 PDF 进行数字签名。我不确定从哪里开始阅读。只是想问问以前是否有其他人能够做到这一点,如果有,我应该使用什么样的资源来做到这一点。

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby pdf-generation digital-signature prawn


    【解决方案1】:

    我建议你看看https://github.com/joseicosta/odf-report。 它是生成 ODF 的瑰宝,但它们可以轻松转换为 PDF,而且它支持模板。

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    【解决方案2】:

    虽然这个问题有答案并且很老,但我想添加相关链接以供参考。

    MrWater 已将他的代码分享给Insert digital signature into existing pdf file

    版本 1 - 生成证书和密钥文件,并将它们直接插入到文档中

    require 'openssl'
    
    begin
      require 'origami'
    rescue LoadError
      ORIGAMIDIR = "C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\origami-1.2.4\lib"
      $: << ORIGAMIDIR
      require 'origami'
    end
    include Origami
    
    # Code below is based on documentation available on
    # http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL.html
    key = OpenSSL::PKey::RSA.new 2048
    
    open 'private_key.pem', 'w' do |io| io.write key.to_pem end
    open 'public_key.pem', 'w' do |io| io.write key.public_key.to_pem end
    
    cipher = OpenSSL::Cipher::Cipher.new 'AES-128-CBC'
    pass_phrase = 'Origami rocks'
    
    key_secure = key.export cipher, pass_phrase
    
    open 'private_key.pem', 'w' do |io|
      io.write key_secure
    end
    
    #Create the certificate
    
    name = OpenSSL::X509::Name.parse 'CN=nobody/DC=example'
    
    cert = OpenSSL::X509::Certificate.new
    cert.version = 2
    cert.serial = 0
    cert.not_before = Time.now
    cert.not_after = Time.now + 3600
    
    cert.public_key = key.public_key
    cert.subject = name
    
    
    OUTPUTFILE = "test.pdf"
    
    contents = ContentStream.new.setFilter(:FlateDecode)
    contents.write OUTPUTFILE,
      :x => 350, :y => 750, :rendering => Text::Rendering::STROKE, :size => 30
    
    pdf = PDF.read('Sample.pdf')
    
    
    # Open certificate files
    
    #sigannot = Annotation::Widget::Signature.new
    #sigannot.Rect = Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
    
    #page.add_annot(sigannot)
    
    # Sign the PDF with the specified keys
    pdf.sign(cert, key, 
      :method => 'adbe.pkcs7.sha1',
      #:annotation => sigannot, 
      :location => "Portugal", 
      :contact => "myemail@email.tt", 
      :reason => "Proof of Concept"
    )
    
    # Save the resulting file
    pdf.save(OUTPUTFILE)
    

    版本 2 - 使用现有证书签署 pdf 文档

    require 'openssl'
    
    begin
      require 'origami'
    rescue LoadError
      ORIGAMIDIR = "C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\gems\origami-1.2.4\lib"
      $: << ORIGAMIDIR
      require 'origami'
    end
    include Origami
    
    INPUTFILE = "Sample.pdf"
    @inputfile = String.new(INPUTFILE)
    OUTPUTFILE = @inputfile.insert(INPUTFILE.rindex("."),"_signed")
    CERTFILE = "certificate.pem"
    RSAKEYFILE = "private_key.pem"
    passphrase = "your passphrase"
    
    key4pem=File.read RSAKEYFILE
    
    key = OpenSSL::PKey::RSA.new key4pem, passphrase
    cert = OpenSSL::X509::Certificate.new(File.read CERTFILE)
    
    pdf = PDF.read(INPUTFILE)
    page = pdf.get_page(1)
    
    # Add signature annotation (so it becomes visibles in pdf document)
    
    sigannot = Annotation::Widget::Signature.new
    sigannot.Rect = Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
    
    page.add_annot(sigannot)
    
    # Sign the PDF with the specified keys
    pdf.sign(cert, key, 
      :method => 'adbe.pkcs7.sha1',
      :annotation => sigannot, 
      :location => "Portugal", 
      :contact => "myemail@email.tt", 
      :reason => "Proof of Concept"
    )
    
    # Save the resulting file
    pdf.save(OUTPUTFILE)
    

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    • 原代码是根据@nalply和stackoverflow.com/help/referencing添加的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多