Class: Decidim::Initiatives::PdfSignatureExample
- Inherits:
-
Object
- Object
- Decidim::Initiatives::PdfSignatureExample
- Defined in:
- app/services/decidim/initiatives/pdf_signature_example.rb
Overview
Example of service to add a signature to a pdf
Instance Attribute Summary collapse
-
#pdf ⇒ Object
Returns the value of attribute pdf.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ PdfSignatureExample
constructor
Public: Initializes the service.
-
#signed_pdf ⇒ Object
Public: PDF signed using a new certificate generated by the service.
Constructor Details
#initialize(args = {}) ⇒ PdfSignatureExample
Public: Initializes the service. pdf - The pdf document to be signed
14 15 16 |
# File 'app/services/decidim/initiatives/pdf_signature_example.rb', line 14 def initialize(args = {}) @pdf = args.fetch(:pdf) end |
Instance Attribute Details
#pdf ⇒ Object
Returns the value of attribute pdf.
10 11 12 |
# File 'app/services/decidim/initiatives/pdf_signature_example.rb', line 10 def pdf @pdf end |
Instance Method Details
#signed_pdf ⇒ Object
Public: PDF signed using a new certificate generated by the service
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/services/decidim/initiatives/pdf_signature_example.rb', line 19 def signed_pdf signed_file = Tempfile.new("signed_pdf") doc = HexaPDF::Document.new(io: StringIO.new(pdf)) # Prepare the document for embedding of the digital signature data = nil # Used for storing the to-be-signed data signing_mechanism = lambda do |io, byte_range| # Store the to-be-signed data in the local variable data io.pos = byte_range[0] data = io.read(byte_range[1]) io.pos = byte_range[2] data << io.read(byte_range[3]) "" end doc.sign(signed_file.path, signature: (doc), reason: , signature_size: 10_000, external_signing: signing_mechanism) signature = OpenSSL::PKCS7.sign(certificate, key, data, # [HexaPDF.demo_cert.sub_ca, HexaPDF.demo_cert.root_ca], [], OpenSSL::PKCS7::DETACHED | OpenSSL::PKCS7::BINARY).to_der # Embed the signature HexaPDF::DigitalSignature::Signing.(File.open(signed_file.path, "rb+"), signature) File.binread(signed_file.path) ensure signed_file.close signed_file.unlink end |