2024-09-05 22:54:38 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-07-21 02:57:13 +02:00
|
|
|
module PdfDocuments
|
|
|
|
|
class Base
|
2024-11-23 21:11:01 +01:00
|
|
|
attr_reader :params, :font
|
2024-07-22 01:29:09 +02:00
|
|
|
|
2024-07-21 02:57:13 +02:00
|
|
|
def initialize(prawn_document, **params)
|
|
|
|
|
@prawn_document = prawn_document
|
|
|
|
|
@prawn_document.markup_options = markup_options
|
2024-07-22 01:29:09 +02:00
|
|
|
# @prawn_document.font_families.update('Lexend' => {
|
|
|
|
|
# normal: 'vendor/assets/fonts/Lexend-Light.ttf',
|
|
|
|
|
# bold: 'vendor/assets/fonts/Lexend-Bold.ttf',
|
|
|
|
|
# exta_bold: 'vendor/assets/fonts/Lexend-ExtraBold.ttf',
|
|
|
|
|
# italic: 'vendor/assets/fonts/Lexend-Regular.ttf'
|
|
|
|
|
# })
|
2024-11-23 21:11:01 +01:00
|
|
|
@font = "Helvetica"
|
|
|
|
|
@prawn_document.font @font, size: 12
|
2024-07-21 02:57:13 +02:00
|
|
|
@params = OpenStruct.new(params)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
generate
|
|
|
|
|
@prawn_document
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def generate
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def heading1(text)
|
2024-07-22 01:29:09 +02:00
|
|
|
@prawn_document.markup "<h1>#{text}</h1>"
|
2024-07-21 02:57:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def heading2(text)
|
|
|
|
|
@prawn_document.markup "<h2>#{text}</h2>"
|
|
|
|
|
end
|
|
|
|
|
|
2024-07-22 01:29:09 +02:00
|
|
|
def heading3(text)
|
2024-07-21 02:57:13 +02:00
|
|
|
@prawn_document.markup "<h3>#{text}</h3>"
|
|
|
|
|
end
|
|
|
|
|
|
2024-11-23 21:11:01 +01:00
|
|
|
def heading4(text)
|
|
|
|
|
@prawn_document.markup "<h4>#{text}</h4>"
|
|
|
|
|
end
|
|
|
|
|
|
2024-07-21 02:57:13 +02:00
|
|
|
def text(text)
|
|
|
|
|
@prawn_document.text text, markup_options[:text]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def rich_text(text)
|
|
|
|
|
@prawn_document.markup prepare_rich_text(text)
|
|
|
|
|
end
|
|
|
|
|
|
2024-07-22 01:29:09 +02:00
|
|
|
def hr
|
2024-09-05 22:54:38 +02:00
|
|
|
@prawn_document.markup "<hr>"
|
2024-07-22 01:29:09 +02:00
|
|
|
end
|
|
|
|
|
|
2024-07-21 02:57:13 +02:00
|
|
|
def markup_options
|
|
|
|
|
{
|
2024-07-22 01:29:09 +02:00
|
|
|
text: { size: 12, margin_bottom: 5 },
|
2024-11-23 23:12:06 +01:00
|
|
|
heading1: { style: :bold, size: 26, margin_bottom: 5, margin_top: 0 },
|
|
|
|
|
heading2: { style: :bold, size: 17, margin_bottom: 5, margin_top: 5 },
|
|
|
|
|
heading3: { style: :bold, size: 13, margin_bottom: 5, margin_top: 5 },
|
|
|
|
|
heading4: { style: :bold, size: 12, margin_bottom: 5, margin_top: 5 },
|
|
|
|
|
heading5: { style: :bold, size: 12, margin_bottom: 5, margin_top: 5 },
|
|
|
|
|
heading6: { style: :thin, size: 12, margin_bottom: 5, margin_top: 5 }
|
2024-07-21 02:57:13 +02:00
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
|
2024-07-22 01:29:09 +02:00
|
|
|
def logo
|
2024-09-05 22:54:38 +02:00
|
|
|
@prawn_document.image "app/assets/images/logo-apfelschule.png", width: 150
|
2024-07-22 01:29:09 +02:00
|
|
|
end
|
|
|
|
|
|
2024-07-21 02:57:13 +02:00
|
|
|
def prepare_rich_text(rich_text)
|
2024-11-23 21:11:01 +01:00
|
|
|
{ h1: "h5" }.each do |tag, replacement|
|
2024-07-21 02:57:13 +02:00
|
|
|
rich_text = rich_text.to_s.gsub("<#{tag}", "<#{replacement}")
|
|
|
|
|
rich_text = rich_text.to_s.gsub("</#{tag}>", "</#{replacement}>")
|
|
|
|
|
end
|
|
|
|
|
|
2024-11-23 23:12:06 +01:00
|
|
|
rich_text.sub(/(<br>)+$/, "")
|
2024-07-21 02:57:13 +02:00
|
|
|
end
|
2024-07-22 01:29:09 +02:00
|
|
|
|
|
|
|
|
def font(...)
|
|
|
|
|
@prawn_document.font(...)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def formatted_text(...)
|
|
|
|
|
@prawn_document.formatted_text(...)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def move_down(...)
|
|
|
|
|
@prawn_document.move_down(...)
|
|
|
|
|
end
|
2024-11-23 21:11:01 +01:00
|
|
|
|
|
|
|
|
def safe_display(value, &block)
|
|
|
|
|
return if value.blank?
|
|
|
|
|
|
2024-11-23 23:19:06 +01:00
|
|
|
begin
|
2024-11-23 23:26:21 +01:00
|
|
|
yield(value)
|
|
|
|
|
rescue StandardError
|
2024-11-23 23:19:06 +01:00
|
|
|
nil
|
|
|
|
|
end
|
2024-11-23 21:11:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def bold(text)
|
|
|
|
|
@prawn_document.font(@font, style: :bold) do
|
|
|
|
|
@prawn_document.text text
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-11-23 22:05:01 +01:00
|
|
|
|
|
|
|
|
def image(attachable, **args)
|
2024-11-24 00:58:36 +01:00
|
|
|
begin
|
|
|
|
|
@prawn_document.image(ActiveStorage::Blob.service.path_for(attachable.key), **args) && raise("david")
|
|
|
|
|
rescue StandardError
|
|
|
|
|
false
|
|
|
|
|
end
|
2024-11-23 23:12:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def without_page_break(&block)
|
|
|
|
|
return yield
|
|
|
|
|
return @prawn_document.bounding_box([ 0, @prawn_document.cursor ], width: 300) do
|
|
|
|
|
yield
|
|
|
|
|
# @prawn_document.stroke_bounds if Rails.env.development?
|
|
|
|
|
# print_coordinates
|
|
|
|
|
end
|
|
|
|
|
@prawn_document.span(520) do
|
|
|
|
|
yield
|
|
|
|
|
# @prawn_document.stroke_bounds
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def print_coordinates
|
|
|
|
|
@prawn_document.text("top: #{@prawn_document.bounds.top}")
|
|
|
|
|
@prawn_document.text("bottom: #{@prawn_document.bounds.bottom}")
|
|
|
|
|
@prawn_document.text("left: #{@prawn_document.bounds.left}")
|
|
|
|
|
@prawn_document.text("right: #{@prawn_document.bounds.right}")
|
|
|
|
|
@prawn_document.move_down(10)
|
|
|
|
|
@prawn_document.text("absolute top: #{Float(@prawn_document.bounds.absolute_top).round(2)}")
|
|
|
|
|
@prawn_document.text("absolute bottom: #{Float(@prawn_document.bounds.absolute_bottom).round(2)}")
|
|
|
|
|
@prawn_document.text("absolute left: #{Float(@prawn_document.bounds.absolute_left).round(2)}")
|
|
|
|
|
@prawn_document.text("absolute right: #{Float(@prawn_document.bounds.absolute_right).round(2)}")
|
|
|
|
|
end
|
|
|
|
|
|
2024-11-24 00:58:36 +01:00
|
|
|
def new_page(required_space: 0)
|
|
|
|
|
@prawn_document.start_new_page if @prawn_document.cursor <= required_space + 10
|
2024-11-23 22:05:01 +01:00
|
|
|
end
|
2024-07-21 02:57:13 +02:00
|
|
|
end
|
|
|
|
|
end
|