Files
docuseal/lib/templates/process_document.rb
T

158 lines
4.9 KiB
Ruby
Raw Normal View History

2023-05-21 17:59:36 +03:00
# frozen_string_literal: true
2023-05-31 23:19:20 +03:00
module Templates
2023-05-21 17:59:36 +03:00
module ProcessDocument
DPI = 200
2024-09-11 16:46:33 +03:00
FORMAT = '.png'
2023-05-21 17:59:36 +03:00
ATTACHMENT_NAME = 'preview_images'
PDF_CONTENT_TYPE = 'application/pdf'
2024-09-11 16:46:33 +03:00
Q = 95
JPEG_Q = ENV.fetch('PAGE_QUALITY', '35').to_i
2023-05-21 17:59:36 +03:00
MAX_WIDTH = 1400
2023-10-24 23:26:05 +03:00
MAX_NUMBER_OF_PAGES_PROCESSED = 15
2024-06-22 13:16:22 +03:00
MAX_FLATTEN_FILE_SIZE = 20.megabytes
2024-06-16 11:27:24 +03:00
GENERATE_PREVIEW_SIZE_LIMIT = 50.megabytes
2023-05-21 17:59:36 +03:00
module_function
2024-09-11 16:46:33 +03:00
def call(attachment, data, extract_fields: false)
2023-05-21 17:59:36 +03:00
if attachment.content_type == PDF_CONTENT_TYPE
2024-06-22 13:16:22 +03:00
if extract_fields && data.size < MAX_FLATTEN_FILE_SIZE
pdf = HexaPDF::Document.new(io: StringIO.new(data))
fields = Templates::FindAcroFields.call(pdf, attachment)
end
2024-09-11 16:46:33 +03:00
generate_pdf_preview_images(attachment, data, pdf)
2024-06-22 13:16:22 +03:00
attachment.metadata['pdf']['fields'] = fields if fields
2023-06-25 11:35:23 +03:00
elsif attachment.image?
2024-09-11 16:46:33 +03:00
generate_preview_image(attachment, data)
2023-05-21 17:59:36 +03:00
end
attachment
end
2024-09-11 16:46:33 +03:00
def generate_preview_image(attachment, data)
2023-05-21 17:59:36 +03:00
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all
2023-09-01 01:27:13 +03:00
image = Vips::Image.new_from_buffer(data, '')
2023-07-07 00:54:32 +03:00
image = image.autorot.resize(MAX_WIDTH / image.width.to_f)
2023-05-21 17:59:36 +03:00
2024-09-11 16:46:33 +03:00
bitdepth = 2**image.stats.to_a[1..3].pluck(2).uniq.size
io = StringIO.new(image.write_to_buffer(FORMAT, compression: 7, filter: 0, bitdepth:,
palette: true, Q: Q, dither: 0))
2023-05-21 17:59:36 +03:00
ActiveStorage::Attachment.create!(
blob: ActiveStorage::Blob.create_and_upload!(
io:, filename: "0#{FORMAT}",
metadata: { analyzed: true, identified: true, width: image.width, height: image.height }
),
name: ATTACHMENT_NAME,
record: attachment
)
end
2024-09-11 16:46:33 +03:00
def generate_pdf_preview_images(attachment, data, pdf = nil)
2023-05-21 17:59:36 +03:00
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all
2024-05-17 12:40:35 +03:00
2024-06-22 13:16:22 +03:00
pdf ||= HexaPDF::Document.new(io: StringIO.new(data))
2024-05-17 12:40:35 +03:00
number_of_pages = pdf.pages.size
2024-05-27 00:39:36 +03:00
data = maybe_flatten_form(data, pdf)
2023-10-22 01:54:20 +03:00
2024-04-18 22:25:04 +03:00
attachment.metadata['pdf'] ||= {}
attachment.metadata['pdf']['number_of_pages'] = number_of_pages
2023-10-22 01:54:20 +03:00
attachment.save!
2023-05-21 17:59:36 +03:00
2024-06-16 11:27:24 +03:00
max_pages_to_process = data.size < GENERATE_PREVIEW_SIZE_LIMIT ? MAX_NUMBER_OF_PAGES_PROCESSED : 1
(0..[number_of_pages - 1, max_pages_to_process].min).each do |page_number|
2023-09-01 01:27:13 +03:00
page = Vips::Image.new_from_buffer(data, '', dpi: DPI, page: page_number)
2023-05-21 17:59:36 +03:00
page = page.resize(MAX_WIDTH / page.width.to_f)
2024-09-11 16:46:33 +03:00
bitdepth = 2**page.stats.to_a[1..3].pluck(2).uniq.size
io = StringIO.new(page.write_to_buffer(FORMAT, compression: 7, filter: 0, bitdepth:,
palette: true, Q: Q, dither: 0))
2023-05-21 17:59:36 +03:00
2023-10-22 01:54:20 +03:00
ApplicationRecord.no_touching do
ActiveStorage::Attachment.create!(
blob: ActiveStorage::Blob.create_and_upload!(
io:, filename: "#{page_number}#{FORMAT}",
metadata: { analyzed: true, identified: true, width: page.width, height: page.height }
),
name: ATTACHMENT_NAME,
record: attachment
)
end
end
end
2024-05-27 00:39:36 +03:00
def maybe_flatten_form(data, pdf)
2024-05-17 12:40:35 +03:00
return data if data.size > MAX_FLATTEN_FILE_SIZE
return data if pdf.acro_form.blank?
io = StringIO.new
2024-05-27 00:39:36 +03:00
pdf.acro_form.create_appearances(force: true) if pdf.acro_form[:NeedAppearances]
pdf.acro_form.flatten
2024-05-17 12:40:35 +03:00
pdf.write(io, incremental: false, validate: false)
io.string
2024-09-03 18:30:37 +03:00
rescue StandardError
2024-05-17 12:40:35 +03:00
raise if Rails.env.development?
data
end
2024-06-22 13:16:22 +03:00
def normalize_attachment_fields(template, attachments = template.documents)
attachments.flat_map do |a|
pdf_fields = a.metadata['pdf'].delete('fields').to_a if a.metadata['pdf'].present?
next [] if pdf_fields.blank?
pdf_fields.each { |f| f['submitter_uuid'] = template.submitters.first['uuid'] }
pdf_fields
end
end
2024-09-11 16:46:33 +03:00
def generate_pdf_preview_from_file(attachment, file_path, page_number)
2023-10-23 22:48:52 +03:00
io = StringIO.new
command = [
2024-09-11 16:46:33 +03:00
'pdftocairo', '-jpeg', '-jpegopt', "progressive=y,quality=#{JPEG_Q},optimize=y",
2023-10-23 22:48:52 +03:00
'-scale-to-x', MAX_WIDTH, '-scale-to-y', '-1',
'-r', DPI, '-f', page_number + 1, '-l', page_number + 1,
'-singlefile', Shellwords.escape(file_path), '-'
].join(' ')
Open3.popen3(command) do |_, stdout, _, _|
io.write(stdout.read)
io.rewind
end
page = Vips::Image.new_from_buffer(io.read, '')
2023-10-22 01:54:20 +03:00
2023-10-23 22:48:52 +03:00
io.rewind
2023-10-22 01:54:20 +03:00
ApplicationRecord.no_touching do
2023-05-21 17:59:36 +03:00
ActiveStorage::Attachment.create!(
blob: ActiveStorage::Blob.create_and_upload!(
2024-09-11 16:46:33 +03:00
io:, filename: "#{page_number}.jpg",
2023-05-21 17:59:36 +03:00
metadata: { analyzed: true, identified: true, width: page.width, height: page.height }
),
name: ATTACHMENT_NAME,
record: attachment
)
end
end
end
end