Files
docuseal/lib/templates/process_document.rb
T

221 lines
6.6 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'
2025-05-24 15:05:24 +03:00
BMP_REGEXP = %r{\Aimage/(?:bmp|x-bmp|x-ms-bmp)\z}
2023-05-21 17:59:36 +03:00
PDF_CONTENT_TYPE = 'application/pdf'
2024-09-13 09:42:56 +03:00
CONCURRENCY = 2
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-10-09 17:48:45 +03:00
def call(attachment, data, extract_fields: false, max_pages: MAX_NUMBER_OF_PAGES_PROCESSED)
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))
2025-03-24 09:44:10 +02:00
fields = Templates::FindAcroFields.call(pdf, attachment, data)
2024-06-22 13:16:22 +03:00
end
2024-10-09 17:48:45 +03:00
generate_pdf_preview_images(attachment, data, pdf, max_pages:)
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
2025-05-31 15:15:25 +03:00
def process(attachment, data, extract_fields: false)
if attachment.content_type == PDF_CONTENT_TYPE && extract_fields && data.size < MAX_FLATTEN_FILE_SIZE
pdf = HexaPDF::Document.new(io: StringIO.new(data))
fields = Templates::FindAcroFields.call(pdf, attachment, data)
end
pdf ||= HexaPDF::Document.new(io: StringIO.new(data))
number_of_pages = pdf.pages.size
attachment.metadata['pdf'] ||= {}
attachment.metadata['pdf']['number_of_pages'] = number_of_pages
attachment.metadata['pdf']['fields'] = fields if fields
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
2025-05-24 15:05:24 +03:00
image =
if BMP_REGEXP.match?(attachment.content_type)
LoadBmp.call(data)
else
Vips::Image.new_from_buffer(data, '')
end
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:,
2024-09-18 13:48:52 +03:00
palette: true, Q: bitdepth == 8 ? Q : 5, 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-10-09 17:48:45 +03:00
def generate_pdf_preview_images(attachment, data, pdf = nil, max_pages: MAX_NUMBER_OF_PAGES_PROCESSED)
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
2024-09-13 09:42:56 +03:00
ApplicationRecord.no_touching do
attachment.save!
end
2023-05-21 17:59:36 +03:00
2024-10-09 17:48:45 +03:00
max_pages_to_process = data.size < GENERATE_PREVIEW_SIZE_LIMIT ? max_pages : 1
2024-06-16 11:27:24 +03:00
2024-10-09 17:48:45 +03:00
generate_document_preview_images(attachment, data, (0..[number_of_pages - 1, max_pages_to_process].min))
end
def generate_document_preview_images(attachment, data, range, concurrency: CONCURRENCY)
2025-05-24 15:05:24 +03:00
doc = Pdfium::Document.open_bytes(data)
2024-10-09 17:48:45 +03:00
pool = Concurrent::FixedThreadPool.new(concurrency)
2024-09-11 16:46:33 +03:00
2024-09-13 09:42:56 +03:00
promises =
2024-10-09 17:48:45 +03:00
range.map do |page_number|
2025-05-24 15:05:24 +03:00
Concurrent::Promise.execute(executor: pool) { build_and_upload_blob(doc, page_number) }
2024-09-13 09:42:56 +03:00
end
2023-05-21 17:59:36 +03:00
2024-09-13 09:42:56 +03:00
Concurrent::Promise.zip(*promises).value!.each do |blob|
2025-03-19 17:57:46 +02:00
next unless blob
2023-10-22 01:54:20 +03:00
ApplicationRecord.no_touching do
ActiveStorage::Attachment.create!(
2024-09-13 09:42:56 +03:00
blob:,
2023-10-22 01:54:20 +03:00
name: ATTACHMENT_NAME,
record: attachment
)
end
end
2025-05-24 15:05:24 +03:00
ensure
doc&.close
pool&.kill
2024-09-13 09:42:56 +03:00
end
2025-05-24 15:05:24 +03:00
def build_and_upload_blob(doc, page_number, format = FORMAT)
doc_page = doc.get_page(page_number)
data, width, height = doc_page.render_to_bitmap(width: MAX_WIDTH)
page = Vips::Image.new_from_memory(data, width, height, 4, :uchar)
page = page.copy(interpretation: :srgb)
2024-09-13 09:42:56 +03:00
2025-05-24 15:05:24 +03:00
data =
if format == FORMAT
2025-05-26 22:26:14 +03:00
bitdepth = 2**page.stats.to_a[1..3].pluck(2).uniq.size
2025-05-24 15:05:24 +03:00
page.write_to_buffer(format, compression: 7, filter: 0, bitdepth:,
palette: true, Q: bitdepth == 8 ? Q : 5, dither: 0)
else
page.write_to_buffer(format, interlace: true, Q: JPEG_Q)
end
2024-09-13 09:42:56 +03:00
blob = ActiveStorage::Blob.new(
2025-05-24 15:05:24 +03:00
filename: "#{page_number}#{format}",
2024-09-13 09:42:56 +03:00
metadata: { analyzed: true, identified: true, width: page.width, height: page.height }
)
2025-05-24 15:05:24 +03:00
blob.upload(StringIO.new(data))
2024-09-13 09:42:56 +03:00
blob
2025-05-24 15:05:24 +03:00
rescue Vips::Error, Pdfium::PdfiumError => e
2025-03-19 17:57:46 +02:00
Rollbar.warning(e) if defined?(Rollbar)
2025-05-30 10:36:08 +03:00
nil
2025-05-24 15:05:24 +03:00
ensure
doc_page&.close
2023-10-22 01:54:20 +03:00
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-11-06 19:27:37 +02:00
pdf.acro_form.each_field do |field|
next if field.field_type != :Ch ||
field[:Opt].blank? ||
%i[combo_box editable_combo_box].exclude?(field.concrete_field_type) ||
!field.field_value.to_s.match?(FindAcroFields::SELECT_PLACEHOLDER_REGEXP)
field[:V] = ''
end
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)
2025-05-24 15:05:24 +03:00
doc = Pdfium::Document.open_file(file_path)
2023-10-22 01:54:20 +03:00
2025-05-24 15:05:24 +03:00
blob = build_and_upload_blob(doc, page_number, '.jpeg')
2023-10-22 01:54:20 +03:00
ApplicationRecord.no_touching do
2023-05-21 17:59:36 +03:00
ActiveStorage::Attachment.create!(
2025-05-24 15:05:24 +03:00
blob: blob,
2023-05-21 17:59:36 +03:00
name: ATTACHMENT_NAME,
record: attachment
)
end
2025-05-24 15:05:24 +03:00
ensure
doc&.close
2023-05-21 17:59:36 +03:00
end
end
end