-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport_generator.rb
146 lines (118 loc) · 4.54 KB
/
report_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require 'sinatra'
require 'letter_opener' unless ENV['RACK_ENV'] == 'production'
require 'pry' unless ENV['RACK_ENV'] == 'production'
require 'pony'
require 'json'
require 'openssl'
require 'origami'
class ReportGenerator < Sinatra::Base
include Origami
use Rack::Auth::Basic, "Restricted Area" do |username, password|
if ENV['RACK_ENV'] == 'production'
@username = ENV["SINATRA_USERNAME"]
@password = ENV["SINATRA_PASSWORD"]
else
@username = "admin"
@password = "admin"
end
username == @username and password == @password
end
post '/kif/?' do
@kif_info = JSON.parse(params["data"])
generate_pdf('reports/kif-report-template.html')
Pony.mail(:to => @kif_info["email"],
:from => '[email protected]',
:subject => 'hi',
:body => "Hello there. It is #{DateTime.now}. Enjoy your KIF report.",
:attachments => {"KIF_report_#{DateTime.now}.pdf" => File.read(@output_file)})
status 200
end
post '/kuf/?' do
@kuf_info = JSON.parse(params["data"])
generate_pdf('reports/kuf-report-template.html')
Pony.mail(:to => @kuf_info["email"],
:from => '[email protected]',
:subject => 'hi',
:body => "Hello there. It is #{DateTime.now}. Enjoy your KUF report.",
:attachments => {"KUF_report_#{DateTime.now}.pdf" => File.read(@output_file)})
status 200
end
post '/ios/?' do
@ios_info = JSON.parse(params["data"])
# generate_pdf('reports/kuf-report-template.html')
# Pony.mail(:to => @kuf_info["email"],
# :from => '[email protected]',
# :subject => 'hi',
# :body => "Hello there. It is #{DateTime.now}. Enjoy your KUF report.",
# :attachments => {"KUF_report_#{DateTime.now}.pdf" => File.read(@file)})
status 200
end
post '/partner_card/?' do
@partner_card = JSON.parse(params["data"])
# generate_pdf('reports/kuf-report-template.html')
# Pony.mail(:to => @kuf_info["email"],
# :from => '[email protected]',
# :subject => 'hi',
# :body => "Hello there. It is #{DateTime.now}. Enjoy your KUF report.",
# :attachments => {"KUF_report_#{DateTime.now}.pdf" => File.read(@file)})
status 200
end
def generate_pdf(pdf_path)
kit = PDFKit.new(erb pdf_path.to_sym)
kit.stylesheets << 'views/reports/css/bootstrap-theme.min.css'
kit.stylesheets << 'views/reports/css/bootstrap.min.css'
kit.stylesheets << 'views/reports/css/style.css'
headers['Content-Type'] = 'application/pdf'
@file_name = "pdf_test#{Time.now.getutc}"
@file = kit.to_file(@file_name)
sign_pdf
end
def sign_pdf
# 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 = if ENV['RACK_ENV'] == 'production'
ENV['PDF_PASSWORD']
else
"admin"
end
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=PDF_signature'
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
@output_file = "pdf_signed_test#{Time.now.getutc}"
contents = ContentStream.new.setFilter(:FlateDecode)
contents.write @output_file,
:x => 350,
:y => 750,
:rendering => Text::Rendering::STROKE,
:size => 30
@pdf = PDF.read(@file)
sigannot = Annotation::Widget::Signature.new
sigannot.Rect = Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
page = @pdf.get_page(1)
page.add_annot(sigannot)
# Sign the PDF with the specified keys
@pdf.sign(cert, key,
:method => 'adbe.pkcs7.sha1',
:annotation => sigannot,
:location => "Serbia",
:contact => "[email protected]",
:reason => "Proof of Concept"
)
# Save the resulting file
@pdf.save(@output_file)
end
end