Last active
June 9, 2020 17:48
-
-
Save tbcooney/045842b735e456343ee4cb96f7fc40d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
OUTPUT_FILE = "#{File.basename(__FILE__, ".rb")}.pdf" | |
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) | |
# Create the PDF contents | |
contents = ContentStream.new.setFilter(:FlateDecode) | |
contents.write OUTPUT_FILE, | |
x: 350, y: 750, rendering: Text::Rendering::STROKE, size: 30 | |
pdf = PDF.new | |
page = Page.new.setContents(contents) | |
pdf.append_page(page) | |
sig_annot = Annotation::Widget::Signature.new | |
sig_annot.Rect = Rectangle[llx: 89.0, lly: 386.0, urx: 190.0, ury: 353.0] | |
page.add_annotation(sig_annot) | |
# Sign the PDF with the specified keys | |
pdf.sign(cert, key, | |
method: 'adbe.pkcs7.detached', | |
annotation: sig_annot, | |
location: "France", | |
contact: "gdelugre@localhost", | |
reason: "Signature sample" | |
) | |
# Save the resulting file | |
pdf.save(OUTPUT_FILE) | |
puts "PDF file saved as #{OUTPUT_FILE}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample Ruby code works with Amazon CloudHSM: