a11yist/app/models/pdf_documents/base.rb
david 02e13871bb
Some checks failed
/ Run tests (push) Successful in 2m3s
/ Run system tests (push) Failing after 2m22s
/ Build, push and deploy image (push) Successful in 1m38s
Try fix faulty images
2024-11-23 22:06:50 +01:00

114 lines
3 KiB
Ruby

# frozen_string_literal: true
module PdfDocuments
class Base
attr_reader :params, :font
def initialize(prawn_document, **params)
@prawn_document = prawn_document
@prawn_document.markup_options = markup_options
# @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'
# })
@font = "Helvetica"
@prawn_document.font @font, size: 12
@params = OpenStruct.new(params)
end
def create
generate
@prawn_document
end
private
def generate
raise NotImplementedError
end
def heading1(text)
@prawn_document.markup "<h1>#{text}</h1>"
end
def heading2(text)
@prawn_document.markup "<h2>#{text}</h2>"
end
def heading3(text)
@prawn_document.markup "<h3>#{text}</h3>"
end
def heading4(text)
@prawn_document.markup "<h4>#{text}</h4>"
end
def text(text)
@prawn_document.text text, markup_options[:text]
end
def rich_text(text)
@prawn_document.markup prepare_rich_text(text)
end
def hr
@prawn_document.markup "<hr>"
end
def markup_options
{
text: { size: 12, margin_bottom: 5 },
heading1: { style: :bold, size: 26, margin_bottom: 10, margin_top: 0 },
heading2: { style: :bold, size: 17, margin_bottom: 10, margin_top: 15 },
heading3: { style: :bold, size: 13, margin_bottom: 10, margin_top: 15 },
heading4: { style: :bold, size: 12, margin_bottom: 10, margin_top: 10 },
heading5: { style: :bold, size: 12, margin_bottom: 10, margin_top: 10 },
heading6: { style: :thin, size: 12, margin_bottom: 10, margin_top: 5 }
}
end
def logo
@prawn_document.image "app/assets/images/logo-apfelschule.png", width: 150
@prawn_document.move_down 30
end
def prepare_rich_text(rich_text)
{ h1: "h5" }.each do |tag, replacement|
rich_text = rich_text.to_s.gsub("<#{tag}", "<#{replacement}")
rich_text = rich_text.to_s.gsub("</#{tag}>", "</#{replacement}>")
end
rich_text
end
def font(...)
@prawn_document.font(...)
end
def formatted_text(...)
@prawn_document.formatted_text(...)
end
def move_down(...)
@prawn_document.move_down(...)
end
def safe_display(value, &block)
return if value.blank?
yield(value)
end
def bold(text)
@prawn_document.font(@font, style: :bold) do
@prawn_document.text text
end
end
def image(attachable, **args)
@prawn_document.image(ActiveStorage::Blob.service.path_for(attachable.key), **args) rescue StandardError
end
end
end