Files
docuseal/lib/templates/process_document.rb
T

105 lines
3.2 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
FORMAT = '.jpg'
ATTACHMENT_NAME = 'preview_images'
PDF_CONTENT_TYPE = 'application/pdf'
Q = 35
MAX_WIDTH = 1400
2023-10-24 23:26:05 +03:00
MAX_NUMBER_OF_PAGES_PROCESSED = 15
2023-05-21 17:59:36 +03:00
module_function
2023-09-01 01:27:13 +03:00
def call(attachment, data)
2023-05-21 17:59:36 +03:00
if attachment.content_type == PDF_CONTENT_TYPE
2023-09-01 01:27:13 +03:00
generate_pdf_preview_images(attachment, data)
2023-06-25 11:35:23 +03:00
elsif attachment.image?
2023-09-01 01:27:13 +03:00
generate_preview_image(attachment, data)
2023-05-21 17:59:36 +03:00
end
attachment
end
2023-09-01 01:27:13 +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
2023-06-09 01:43:43 +03:00
io = StringIO.new(image.write_to_buffer(FORMAT, Q: Q, interlace: true))
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
2023-09-01 01:27:13 +03:00
def generate_pdf_preview_images(attachment, data)
2023-05-21 17:59:36 +03:00
ActiveStorage::Attachment.where(name: ATTACHMENT_NAME, record: attachment).destroy_all
2023-10-22 01:54:20 +03:00
number_of_pages = HexaPDF::Document.new(io: StringIO.new(data)).pages.size
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
2023-10-22 01:54:20 +03:00
(0..[number_of_pages - 1, MAX_NUMBER_OF_PAGES_PROCESSED].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)
2023-06-09 01:43:43 +03:00
io = StringIO.new(page.write_to_buffer(FORMAT, Q: Q, interlace: true))
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
def generate_pdf_preview_from_file(attachment, file_path, page_number)
2023-10-23 22:48:52 +03:00
io = StringIO.new
command = [
'pdftocairo', '-jpeg', '-jpegopt', "progressive=y,quality=#{Q},optimize=y",
'-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!(
io:, filename: "#{page_number}#{FORMAT}",
metadata: { analyzed: true, identified: true, width: page.width, height: page.height }
),
name: ATTACHMENT_NAME,
record: attachment
)
end
end
end
end