2017-05-31 17:39:15 +02:00
|
|
|
require 'prawn/measurement_extensions'
|
|
|
|
|
2019-03-07 00:51:11 +01:00
|
|
|
#----- A4 page size
|
|
|
|
page_size = 'A4'
|
2022-08-31 11:23:25 +02:00
|
|
|
# page_height = 842
|
2019-03-07 00:51:11 +01:00
|
|
|
page_width = 595
|
|
|
|
|
|
|
|
#----- margins
|
|
|
|
body_width = 400
|
|
|
|
top_margin = 50
|
|
|
|
bottom_margin = 20
|
|
|
|
footer_height = top_margin - bottom_margin
|
|
|
|
|
2022-08-31 11:23:25 +02:00
|
|
|
right_margin = (page_width - body_width) / 2
|
2019-03-07 00:51:11 +01:00
|
|
|
left_margin = right_margin
|
|
|
|
|
|
|
|
#----- size of images
|
|
|
|
max_logo_width = body_width
|
|
|
|
max_logo_height = 50.mm
|
|
|
|
max_signature_size = 50.mm
|
|
|
|
|
2020-09-03 16:01:55 +02:00
|
|
|
def normalize_pdf_text(text)
|
2020-09-08 17:49:17 +02:00
|
|
|
text&.tr("\t", ' ')
|
2020-09-03 16:01:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
title = normalize_pdf_text(@attestation.fetch(:title))
|
|
|
|
body = normalize_pdf_text(@attestation.fetch(:body))
|
|
|
|
footer = normalize_pdf_text(@attestation.fetch(:footer))
|
2019-08-28 14:27:41 +02:00
|
|
|
created_at = @attestation.fetch(:created_at)
|
|
|
|
|
|
|
|
logo = @attestation[:logo]
|
|
|
|
signature = @attestation[:signature]
|
|
|
|
|
2023-01-23 12:03:33 +01:00
|
|
|
def download_file_and_retry(file_or_attached_one, max_attempts = 3)
|
|
|
|
if file_or_attached_one.is_a?(ActiveStorage::Attached::One)
|
|
|
|
file_or_attached_one.download
|
|
|
|
else
|
|
|
|
file_or_attached_one.rewind && file_or_attached_one.read
|
|
|
|
end
|
|
|
|
rescue Fog::OpenStack::Storage::NotFound => e
|
|
|
|
if max_attempts > 0
|
|
|
|
max_attempts = max_attempts - 1
|
|
|
|
sleep 1
|
|
|
|
retry
|
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-07 00:51:11 +01:00
|
|
|
prawn_document(margin: [top_margin, right_margin, bottom_margin, left_margin], page_size: page_size) do |pdf|
|
2022-08-31 11:23:25 +02:00
|
|
|
pdf.font_families.update('marianne' => { normal: Rails.root.join('lib/prawn/fonts/marianne/marianne-regular.ttf') })
|
2020-06-24 08:58:13 +02:00
|
|
|
pdf.font 'marianne'
|
2017-05-31 17:39:15 +02:00
|
|
|
|
|
|
|
grey = '555555'
|
|
|
|
black = '333333'
|
|
|
|
|
2019-03-07 00:51:11 +01:00
|
|
|
body_height = pdf.cursor - footer_height
|
|
|
|
|
|
|
|
pdf.bounding_box([0, pdf.cursor], width: body_width, height: body_height) do
|
2019-08-28 14:27:41 +02:00
|
|
|
if logo.present?
|
2020-03-30 14:48:25 +02:00
|
|
|
logo_content = if logo.is_a?(ActiveStorage::Attached::One)
|
2019-08-28 14:27:41 +02:00
|
|
|
logo.download
|
2019-08-27 17:42:52 +02:00
|
|
|
else
|
2020-03-30 14:48:25 +02:00
|
|
|
logo.rewind && logo.read
|
2019-08-27 17:42:52 +02:00
|
|
|
end
|
2022-08-31 11:23:25 +02:00
|
|
|
pdf.image StringIO.new(logo_content), fit: [max_logo_width, max_logo_height], position: :center
|
2017-05-31 17:39:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
pdf.fill_color grey
|
2020-06-24 10:43:12 +02:00
|
|
|
pdf.pad_top(40) { pdf.text "le #{l(created_at, format: '%e %B %Y')}", size: 9, align: :right, character_spacing: -0.5 }
|
2017-05-31 17:39:15 +02:00
|
|
|
|
|
|
|
pdf.fill_color black
|
2020-06-24 10:43:12 +02:00
|
|
|
pdf.pad_top(40) { pdf.text title, size: 14, character_spacing: -0.2 }
|
2017-05-31 17:39:15 +02:00
|
|
|
|
|
|
|
pdf.fill_color grey
|
2020-06-24 10:43:12 +02:00
|
|
|
pdf.pad_top(30) { pdf.text body, size: 9, character_spacing: -0.2, align: :justify }
|
2017-05-31 17:39:15 +02:00
|
|
|
|
2019-08-28 14:27:41 +02:00
|
|
|
if signature.present?
|
2017-05-31 17:39:15 +02:00
|
|
|
pdf.pad_top(40) do
|
2020-03-30 14:48:25 +02:00
|
|
|
signature_content = if signature.is_a?(ActiveStorage::Attached::One)
|
2019-08-28 14:27:41 +02:00
|
|
|
signature.download
|
2019-08-27 17:42:52 +02:00
|
|
|
else
|
2020-03-30 14:48:25 +02:00
|
|
|
signature.rewind && signature.read
|
2019-08-27 17:42:52 +02:00
|
|
|
end
|
2022-08-31 11:23:25 +02:00
|
|
|
pdf.image StringIO.new(signature_content), fit: [max_signature_size, max_signature_size], position: :right
|
2017-05-31 17:39:15 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
pdf.repeat(:all) do
|
2019-03-07 00:51:11 +01:00
|
|
|
pdf.move_cursor_to footer_height - 10
|
2017-05-31 17:39:15 +02:00
|
|
|
pdf.fill_color grey
|
2020-09-25 15:19:59 +02:00
|
|
|
if footer.present?
|
|
|
|
# We reduce the size of large footer so they can be drawn in the corresponding area.
|
|
|
|
# This is due to a font change, the replacing font is slightly bigger than the previous one
|
|
|
|
footer_font_size = footer.length > 170 ? 7 : 8
|
|
|
|
pdf.text footer, align: :center, size: footer_font_size
|
|
|
|
end
|
2017-05-31 17:39:15 +02:00
|
|
|
end
|
|
|
|
end
|