Use our font in pdf
This commit is contained in:
parent
1e1ae1ce3b
commit
34730eba2b
12 changed files with 89 additions and 17 deletions
67
app/models/pdf_documents/base.rb
Normal file
67
app/models/pdf_documents/base.rb
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
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 "<h1><u>#{text}</u></h1>"
|
||||
end
|
||||
|
||||
def heading2(text)
|
||||
@prawn_document.markup "<h2>#{text}</h2>"
|
||||
end
|
||||
|
||||
def heading2(text)
|
||||
@prawn_document.markup "<h3>#{text}</h3>"
|
||||
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
|
||||
21
app/models/pdf_documents/customer_report.rb
Normal file
21
app/models/pdf_documents/customer_report.rb
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
module PdfDocuments
|
||||
class CustomerReport < Base
|
||||
private
|
||||
|
||||
def generate
|
||||
heading1 @params.report.name
|
||||
|
||||
@params.report.elements.each do |element|
|
||||
heading2 element.title
|
||||
text element.path
|
||||
rich_text element.description_html
|
||||
|
||||
element.success_criteria.each do |success_criterion|
|
||||
text success_criterion.title
|
||||
rich_text success_criterion.description_html
|
||||
rich_text success_criterion.comment_html
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,17 +1 @@
|
|||
prawn_document do |pdf|
|
||||
pdf.markup_options = {
|
||||
heading1: { style: :bold, size: 14, margin_bottom: 10, margin_top: 20 }
|
||||
}
|
||||
pdf.text @report.name, size: 22, style: :bold, align: :center
|
||||
@report.elements.each do |element|
|
||||
pdf.text element.title, size: 16, style: :bold
|
||||
pdf.text element.path
|
||||
pdf.markup prepare_rich_text(element.description_html.to_s), inline_format: true
|
||||
|
||||
element.success_criteria.each do |success_criterion|
|
||||
pdf.text success_criterion.title, size: 14, style: :bold
|
||||
pdf.markup prepare_rich_text(success_criterion.description_html.to_s), inline_format: true
|
||||
pdf.markup prepare_rich_text(success_criterion.comment_html.to_s), inline_format: true
|
||||
end
|
||||
end
|
||||
end
|
||||
prawn_document { PdfDocuments::CustomerReport.new(_1, report: @report).create }
|
||||
|
|
|
|||
BIN
vendor/assets/fonts/Lexend-Black.ttf
vendored
Normal file
BIN
vendor/assets/fonts/Lexend-Black.ttf
vendored
Normal file
Binary file not shown.
BIN
vendor/assets/fonts/Lexend-Bold.ttf
vendored
Normal file
BIN
vendor/assets/fonts/Lexend-Bold.ttf
vendored
Normal file
Binary file not shown.
BIN
vendor/assets/fonts/Lexend-ExtraBold.ttf
vendored
Normal file
BIN
vendor/assets/fonts/Lexend-ExtraBold.ttf
vendored
Normal file
Binary file not shown.
BIN
vendor/assets/fonts/Lexend-ExtraLight.ttf
vendored
Normal file
BIN
vendor/assets/fonts/Lexend-ExtraLight.ttf
vendored
Normal file
Binary file not shown.
BIN
vendor/assets/fonts/Lexend-Light.ttf
vendored
Normal file
BIN
vendor/assets/fonts/Lexend-Light.ttf
vendored
Normal file
Binary file not shown.
BIN
vendor/assets/fonts/Lexend-Medium.ttf
vendored
Normal file
BIN
vendor/assets/fonts/Lexend-Medium.ttf
vendored
Normal file
Binary file not shown.
BIN
vendor/assets/fonts/Lexend-Regular.ttf
vendored
Normal file
BIN
vendor/assets/fonts/Lexend-Regular.ttf
vendored
Normal file
Binary file not shown.
BIN
vendor/assets/fonts/Lexend-SemiBold.ttf
vendored
Normal file
BIN
vendor/assets/fonts/Lexend-SemiBold.ttf
vendored
Normal file
Binary file not shown.
BIN
vendor/assets/fonts/Lexend-Thin.ttf
vendored
Normal file
BIN
vendor/assets/fonts/Lexend-Thin.ttf
vendored
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue