add pdf generation

pull/105/head
Alex Turchyn 2 years ago
parent 170cb1ecea
commit 6a41f82e5b

@ -8,11 +8,11 @@ gem 'audited'
gem 'aws-sdk-s3'
gem 'azure-storage-blob'
gem 'bootsnap', require: false
gem 'combine_pdf'
gem 'devise'
gem 'faraday'
gem 'geoip'
gem 'google-cloud-storage'
gem 'hexapdf'
gem 'image_processing'
gem 'lograge'
gem 'oj'

@ -123,10 +123,8 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
cmdparse (3.0.7)
coderay (1.1.3)
combine_pdf (1.0.23)
matrix
ruby-rc4 (>= 0.1.5)
concurrent-ruby (1.2.2)
connection_pool (2.4.0)
crack (0.4.5)
@ -199,6 +197,7 @@ GEM
websocket-driver (>= 0.6, < 0.8)
ffi (1.15.5)
geoip (1.6.4)
geom2d (0.3.1)
globalid (1.1.0)
activesupport (>= 5.0)
google-apis-core (0.11.0)
@ -236,6 +235,10 @@ GEM
os (>= 0.9, < 2.0)
signet (>= 0.16, < 2.a)
hashdiff (1.0.1)
hexapdf (0.32.2)
cmdparse (~> 3.0, >= 3.0.3)
geom2d (~> 0.3)
openssl (>= 2.2.1)
htmlentities (4.3.4)
httpclient (2.8.3)
i18n (1.13.0)
@ -296,6 +299,7 @@ GEM
nokogiri (1.15.0-arm64-darwin)
racc (~> 1.4)
oj (3.14.3)
openssl (3.1.0)
orm_adapter (0.5.0)
os (1.1.4)
pagy (6.0.4)
@ -412,7 +416,6 @@ GEM
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
ruby-progressbar (1.13.0)
ruby-rc4 (0.1.5)
ruby-vips (2.1.4)
ffi (~> 1.12)
ruby2_keywords (0.0.5)
@ -483,7 +486,6 @@ DEPENDENCIES
bootsnap
bullet
capybara
combine_pdf
cuprite
debug
devise
@ -493,6 +495,7 @@ DEPENDENCIES
faraday
geoip
google-cloud-storage
hexapdf
image_processing
letter_opener_web
lograge

@ -5,13 +5,14 @@ module Api
skip_before_action :authenticate_user!
def create
submission = Submission.find_by!(slug: params[:submission_slug])
submission = Submission.find_by!(slug: params[:submission_slug]) unless current_account
blob = ActiveStorage::Blob.find_signed(params[:blob_signed_id])
attachment = ActiveStorage::Attachment.create!(
blob:,
name: params[:name],
record: submission
record: submission || current_account
)
render json: attachment.as_json(only: %i[uuid], methods: %i[url filename content_type])

@ -0,0 +1,26 @@
# frozen_string_literal: true
class EsignSettingsController < ApplicationController
before_action :load_encrypted_config
def create
attachment = ActiveStorage::Attachment.find_by!(uuid: params[:attachment_uuid])
pdf = HexaPDF::Document.new(io: StringIO.new(attachment.download))
pdf.signatures
end
private
def load_encrypted_config
@encrypted_config =
EncryptedConfig.find_or_initialize_by(account: current_account, key: EncryptedConfig::ESIGN_CERTS_KEY)
end
def storage_configs
params.require(:encrypted_config).permit(value: {}).tap do |e|
e[:value].compact_blank!
end
end
end

@ -17,6 +17,9 @@ class SetupController < ApplicationController
@user = @account.users.new(user_params)
if @user.save
@account.encrypted_configs.create!(key: EncryptedConfig::ESIGN_CERTS_KEY,
value: GenerateCertificate.call)
sign_in(@user)
redirect_to root_path

@ -0,0 +1,27 @@
# frozen_string_literal: true
class SubmissionsDebugController < ApplicationController
layout 'flow'
skip_before_action :authenticate_user!
def index
@submission = Submission.preload({ attachments_attachments: :blob },
flow: { documents_attachments: :blob })
.find_by(slug: params[:submission_slug])
respond_to do |f|
f.html do
render 'submit_flow/show'
end
f.pdf do
Submissions::GenerateResultAttachments.call(@submission)
send_data ActiveStorage::Attachment.where(name: :documents).last.download,
filename: 'debug.pdf',
disposition: 'inline',
type: 'application/pdf'
end
end
end
end

@ -5,12 +5,14 @@ import { createApp, reactive } from 'vue'
import ToggleVisible from './elements/toggle_visible'
import DisableHidden from './elements/disable_hidden'
import TurboModal from './elements/turbo_modal'
import FileDropzone from './elements/file_dropzone'
import FlowBuilder from './flow_builder/builder'
window.customElements.define('toggle-visible', ToggleVisible)
window.customElements.define('disable-hidden', DisableHidden)
window.customElements.define('turbo-modal', TurboModal)
window.customElements.define('file-dropzone', FileDropzone)
window.customElements.define('flow-builder', class extends HTMLElement {
connectedCallback () {

@ -0,0 +1,82 @@
import { DirectUpload } from '@rails/activestorage'
import { actionable } from '@github/catalyst/lib/actionable'
import { target, targetable } from '@github/catalyst/lib/targetable'
export default actionable(targetable(class extends HTMLElement {
static [target.static] = [
'loading',
'input',
'valueField'
]
connectedCallback () {
this.addEventListener('drop', this.onDrop)
this.addEventListener('dragover', (e) => e.preventDefault())
}
onDrop (e) {
e.preventDefault()
this.uploadFiles(e.dataTransfer.files)
}
onSelectFiles (e) {
e.preventDefault()
this.uploadFiles(this.input.files).then(() => {
this.input.value = ''
})
}
async uploadFiles (files) {
console.log( files )
const blobs = await Promise.all(
Array.from(files).map(async (file) => {
const upload = new DirectUpload(
file,
'/direct_uploads',
this.input
)
return new Promise((resolve, reject) => {
upload.create((error, blob) => {
if (error) {
console.error(error)
return reject(error)
} else {
return resolve(blob)
}
})
}).catch((error) => {
console.error(error)
})
})
)
await Promise.all(
blobs.map((blob) => {
return fetch('/api/attachments', {
method: 'POST',
body: JSON.stringify({
name: this.dataset.name,
blob_signed_id: blob.signed_id,
submission_slug: this.dataset.submissionSlug
}),
headers: { 'Content-Type': 'application/json' }
}).then(resp => resp.json()).then((data) => {
return data
})
})).then((result) => {
result.forEach((attachment) => {
if (this.valueField) {
this.valueField.value = attachment.uuid
}
this.dispatchEvent(new CustomEvent('upload', { detail: attachment }))
})
})
}
}))

@ -91,7 +91,7 @@ export default {
onDrop (e) {
this.$emit('drop-field', {
x: e.layerX / this.$refs.mask.clientWidth,
y: e.layerY / this.$refs.mask.clientHeight,
y: e.layerY / this.$refs.mask.clientHeight - (this.$refs.mask.clientWidth / 30 / this.$refs.mask.clientWidth) / 2,
w: this.$refs.mask.clientWidth / 5 / this.$refs.mask.clientWidth,
h: this.$refs.mask.clientWidth / 30 / this.$refs.mask.clientWidth,
page: this.number

@ -1,14 +1,16 @@
<template>
<div
class="flex cursor-pointer bg-red-100 absolute"
class="flex cursor-pointer bg-red-100 bg-opacity-60 absolute"
:style="computedStyle"
>
<img
v-if="field.type === 'image' && image"
class="object-contain"
:src="image.url"
>
<img
v-else-if="field.type === 'signature' && signature"
class="object-contain"
:src="signature.url"
>
<div v-else-if="field.type === 'attachment'">

@ -83,7 +83,7 @@ export default {
body: JSON.stringify({
submission_slug: this.submissionSlug,
blob_signed_id: data.signed_id,
name: 'signatures'
name: 'attachments'
}),
headers: { 'Content-Type': 'application/json' }
}).then((resp) => resp.json()).then((attachment) => {

@ -23,6 +23,7 @@
class EncryptedConfig < ApplicationRecord
FILES_STORAGE_KEY = 'active_storage'
EMAIL_SMTP_KEY = 'action_mailer_smtp'
ESIGN_CERTS_KEY = 'esign_certs'
belongs_to :account

@ -40,8 +40,6 @@ class Submission < ApplicationRecord
has_many_attached :documents
has_many_attached :attachments
has_many_attached :images
has_many_attached :signatures
scope :active, -> { where(deleted_at: nil) }
end

@ -3,6 +3,7 @@ Hellp
<%= link_to 'Create Flow', new_flow_path, data: { turbo_frame: :modal } %>
<%= link_to 'Storage settings', settings_storage_index_path %>
<%= link_to 'Email settings', settings_email_index_path %>
<%= link_to 'eSign', settings_esign_index_path %>
<%= link_to 'Users', settings_users_path %>
</div>
<div>

@ -0,0 +1,10 @@
<%= form_for '', url: settings_esign_index_path, method: :post do |f| %>
<file-dropzone data-name="verify_attachments">
<label for="file">
<input id="attachment_uuid" name="attachment_uuid" class="hidden" data-target="file-dropzone.valueField" type="text">
<input id="file" class="hidden" data-action="change:file-dropzone#onSelectFiles" data-target="file-dropzone.input" type="file">
LCick to upload
</label>
</file-dropzone>
<%= f.button button_title %>
<% end %>

@ -48,11 +48,13 @@ Rails.application.routes.draw do
resources :submissions, only: %i[], param: 'slug' do
resources :download, only: %i[index], controller: 'submissions_download'
resources :debug, only: %i[index], controller: 'submissions_debug'
end
scope '/settings', as: :settings do
resources :storage, only: %i[index create], controller: 'storage_settings'
resources :email, only: %i[index create], controller: 'email_settings'
resources :esign, only: %i[index create], controller: 'esign_settings'
resources :users, only: %i[index]
end
end

@ -0,0 +1,90 @@
# frozen_string_literal: true
module GenerateCertificate
NAME = 'DocuSeal'
SIZE = 2**11
module_function
def call(name = NAME)
root_cert, root_key = generate_root_ca(name)
sub_cert, sub_key = generate_sub_ca(name, root_cert, root_key)
cert, key = generate_certificate(name, sub_cert, sub_key)
{
cert: cert.to_pem,
key: key.to_pem,
root_ca: root_cert.to_pem,
root_key: root_key.to_pem,
sub_ca: sub_cert.to_pem,
sub_key: sub_key.to_pem
}
end
def generate_root_ca(name)
key = OpenSSL::PKey::RSA.new(SIZE)
cert = OpenSSL::X509::Certificate.new
cert.subject = OpenSSL::X509::Name.parse("/C=AT/O=#{name}/CN=#{name} Root CA")
cert.issuer = cert.subject
cert.not_before = Time.current
cert.not_after = 100.years.from_now
cert.public_key = key.public_key
cert.serial = OpenSSL::BN.rand(160)
ef = OpenSSL::X509::ExtensionFactory.new
ef.subject_certificate = cert
ef.issuer_certificate = cert
cert.add_extension(ef.create_extension('basicConstraints', 'CA:TRUE', true))
cert.add_extension(ef.create_extension('keyUsage', 'Certificate Sign, CRL Sign', true))
cert.add_extension(ef.create_extension('subjectKeyIdentifier', 'hash', false))
cert.sign(key, OpenSSL::Digest.new('SHA256'))
[cert, key]
end
def generate_sub_ca(name, root_ca_cert, root_ca_key)
key = OpenSSL::PKey::RSA.new(SIZE)
cert = OpenSSL::X509::Certificate.new
cert.subject = OpenSSL::X509::Name.parse("/C=AT/O=#{name}/CN=#{name} Sub-CA")
cert.issuer = root_ca_cert.subject
cert.not_before = Time.current
cert.not_after = 100.years.from_now
cert.public_key = key.public_key
cert.serial = OpenSSL::BN.rand(160)
ef = OpenSSL::X509::ExtensionFactory.new
ef.subject_certificate = cert
ef.issuer_certificate = root_ca_cert
cert.add_extension(ef.create_extension('basicConstraints', 'CA:TRUE', true))
cert.add_extension(ef.create_extension('keyUsage', 'Certificate Sign, CRL Sign', true))
cert.add_extension(ef.create_extension('subjectKeyIdentifier', 'hash', false))
cert.sign(root_ca_key, OpenSSL::Digest.new('SHA256'))
[cert, key]
end
def generate_certificate(name, ca_cert, ca_key)
key = OpenSSL::PKey::RSA.new(SIZE)
cert = OpenSSL::X509::Certificate.new
cert.subject = OpenSSL::X509::Name.parse("/C=AT/O=#{name}/CN=#{name} Certificate")
cert.issuer = ca_cert.subject
cert.not_before = Time.current
cert.not_after = 100.years.from_now
cert.public_key = key.public_key
cert.serial = OpenSSL::BN.rand(160)
ef = OpenSSL::X509::ExtensionFactory.new
ef.subject_certificate = cert
ef.issuer_certificate = ca_cert
cert.add_extension(ef.create_extension('basicConstraints', 'CA:FALSE', true))
cert.add_extension(ef.create_extension('keyUsage', 'Digital Signature', true))
cert.add_extension(ef.create_extension('subjectKeyIdentifier', 'hash', false))
cert.sign(ca_key, OpenSSL::Digest.new('SHA256'))
[cert, key]
end
end

@ -0,0 +1,15 @@
# frozen_string_literal: true
module PdfIcons
PATH = Rails.root.join('lib/pdf_icons')
module_function
def check_io
@check_io ||= StringIO.new(PATH.join('check.png').read)
end
def paperclip_io
@paperclip_io ||= StringIO.new(PATH.join('paperclip.png').read)
end
end

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

@ -2,53 +2,124 @@
module Submissions
module GenerateResultAttachments
FONT_SIZE = 12
FONT_NAME = 'Helvetica'
module_function
# rubocop:disable Metrics
def call(submission)
cert = submission.flow.account.encrypted_configs
.find_by(key: EncryptedConfig::ESIGN_CERTS_KEY).value
zip_file = Tempfile.new
zip_stream = Zip::ZipOutputStream.open(zip_file)
submission.flow.schema.map do |item|
document = submission.flow.documents.find { |e| e.uuid == item['attachment_uuid'] }
field_area_index = Flows.build_field_areas_index(submission.flow)
pdfs_index =
submission.flow.documents.to_h do |attachment|
[attachment.uuid, HexaPDF::Document.new(io: StringIO.new(attachment.download))]
end
submission.flow.fields.each do |field|
field.fetch('areas', []).each do |area|
pdf = pdfs_index[area['attachment_uuid']]
page = pdf.pages[area['page']]
width = page.box.width
height = page.box.height
value = submission.values[field['uuid']]
canvas = page.canvas(type: :overlay)
case field['type']
when 'image', 'signature'
attachment = submission.attachments.find { |a| a.uuid == value }
io = StringIO.new(attachment.download)
Vips::Image.new_from_buffer(io.read, '')
document.open do |tempfile|
pdf = CombinePDF.load(tempfile.path)
scale = [(area['w'] * width) / attachment.metadata['width'],
(area['h'] * height) / attachment.metadata['height']].min
pdf.pages.each_with_index do |page, index|
blocks = field_area_index.dig(document.uuid, index)
canvas.image(io, at: [area['x'] * width,
height - (area['y'] * height) -
(((attachment.metadata['height'] * scale) + (area['h'] * height)) / 2)],
width: attachment.metadata['width'] * scale,
height: attachment.metadata['height'] * scale)
when 'attachment'
Array.wrap(value).each_with_index do |uuid, index|
attachment = submission.attachments.find { |a| a.uuid == uuid }
next if blocks.blank?
canvas.image(PdfIcons.paperclip_io,
at: [area['x'] * width,
height - ((area['y'] * height) + (1.2 * FONT_SIZE) - (FONT_SIZE * index))],
width: FONT_SIZE, height: FONT_SIZE)
blocks.each do |block|
area, field = block.values_at(:area, :field)
page.textbox(submission.values[field['uuid']],
x: area['x'] * page.page_size[2],
y: page.page_size[3] - (area['y'] * page.page_size[3]),
width: area['w'] * page.page_size[2],
height: area['h'] * page.page_size[3])
canvas.font(FONT_NAME, size: FONT_SIZE)
canvas.text(attachment.filename.to_s,
at: [(area['x'] * width) + FONT_SIZE,
height - ((area['y'] * height) + FONT_SIZE - (FONT_SIZE * index))])
page[:Annots] ||= []
page[:Annots] << pdf.add({
Type: :Annot, Subtype: :Link,
Rect: [
area['x'] * width,
height - (area['y'] * height),
(area['x'] * width) + (area['w'] * width),
height - (area['y'] * height) - FONT_SIZE
],
A: { Type: :Action, S: :URI, URI: attachment.url }
})
end
when 'checkbox'
Array.wrap(value).each_with_index do |value, index|
canvas.image(PdfIcons.check_io,
at: [area['x'] * width,
height - ((area['y'] * height) + (1.2 * FONT_SIZE) - (FONT_SIZE * index))],
width: FONT_SIZE, height: FONT_SIZE)
canvas.font(FONT_NAME, size: FONT_SIZE)
canvas.text(value,
at: [(area['x'] * width) + FONT_SIZE,
height - ((area['y'] * height) + FONT_SIZE - (FONT_SIZE * index))])
end
when 'date'
canvas.font(FONT_NAME, size: FONT_SIZE)
canvas.text(I18n.l(Date.parse(value)), at: [area['x'] * width, height - ((area['y'] * height) + FONT_SIZE)])
else
canvas.font(FONT_NAME, size: FONT_SIZE)
canvas.text(value.to_s, at: [area['x'] * width, height - ((area['y'] * height) + FONT_SIZE)])
end
end
end
string = pdf.to_pdf
io = StringIO.new(string)
submission.flow.schema.map do |item|
document = submission.flow.documents.find { |a| a.uuid == item['attachment_uuid'] }
zip_stream.put_next_entry("#{item['name']}.pdf")
zip_stream.write(string)
io = StringIO.new
ActiveStorage::Attachment.create!(
blob: ActiveStorage::Blob.create_and_upload!(
io:, filename: "#{item['name']}.pdf"
),
name: 'documents',
record: submission
)
end
zip_stream.put_next_entry("#{item['name']}.pdf")
zip_stream.write(io.string)
pdf = pdfs_index[item['attachment_uuid']]
pdf.sign(io, reason: "Signed by #{submission.email}",
doc_mdp_permissions: :no_changes,
certificate: OpenSSL::X509::Certificate.new(cert['cert']),
key: OpenSSL::PKey::RSA.new(cert['key']),
certificate_chain: [OpenSSL::X509::Certificate.new(cert['sub_ca']),
OpenSSL::X509::Certificate.new(cert['root_ca'])])
submission.documents.attach(io: StringIO.new(io.string), filename: document.filename)
end
zip_stream.close
submission.archive.attach(io: zip_file, filename: 'submission.zip')
submission.archive.attach(io: zip_file, filename: "#{submission.flow.name}.zip")
end
# rubocop:enable Metrics
end
end

Loading…
Cancel
Save