add pdf generation

This commit is contained in:
Alex Turchyn
2023-05-28 23:20:48 +03:00
parent 170cb1ecea
commit 6a41f82e5b
21 changed files with 383 additions and 49 deletions
@@ -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
+3
View File
@@ -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
+2
View File
@@ -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 () {
+82
View File
@@ -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 }))
})
})
}
}))
+1 -1
View File
@@ -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
+3 -1
View File
@@ -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'">
+1 -1
View File
@@ -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) => {
+1
View File
@@ -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
-2
View File
@@ -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
+1
View File
@@ -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>
+10
View File
@@ -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 %>