module PdfDocuments
class Base
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',
italic: 'vendor/assets/fonts/Lexend-Regular.ttf'
})
@prawn_document.font 'Lexend'
@params = OpenStruct.new(params)
end
def create
generate
@prawn_document
end
private
def generate
raise NotImplementedError
end
def heading1(text)
@prawn_document.markup "
#{text}
"
end
def heading2(text)
@prawn_document.markup "#{text}
"
end
def heading2(text)
@prawn_document.markup "#{text}
"
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 markup_options
{
text: { size: 10, margin_bottom: 10 },
heading1: { style: :bold, size: 24, margin_bottom: 10, margin_top: 20 },
heading2: { style: :bold, size: 18, margin_bottom: 10, margin_top: 15 },
heading3: { style: :bold, size: 16, margin_bottom: 10, margin_top: 10 },
heading4: { style: :bold, size: 14, margin_bottom: 10, margin_top: 5 },
heading5: { style: :bold, size: 14, margin_bottom: 10, margin_top: 5 },
heading6: { style: :thin, size: 14, margin_bottom: 10, margin_top: 5 }
}
end
def prepare_rich_text(rich_text)
{ h1: 'h4' }.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
end
end