Skip to content

Commit

Permalink
#341 adding external fonts to prawn with support to unicode and chang…
Browse files Browse the repository at this point in the history
…ing code to use these fonts
  • Loading branch information
Carlos-Eduardo-Cabral-da-Cunha committed Apr 8, 2021
1 parent 1068414 commit f0057ec
Show file tree
Hide file tree
Showing 22 changed files with 16,796 additions and 25 deletions.
32 changes: 16 additions & 16 deletions app/helpers/enrollments_pdf_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def enrollment_student_header(pdf, options={})
enrollment ||= options[:enrollment]
pdf.bounding_box([0, pdf.cursor - 3], :width => 560) do

pdf.font('Courier', :size => 8) do
pdf.font('FreeMono', :size => 8) do
birthdate = enrollment.student.birthdate.nil? ? rescue_blank_text(nil) : I18n.localize(enrollment.student.birthdate, :format => :default)

data_table = [
Expand Down Expand Up @@ -85,7 +85,7 @@ def common_header_part2(pdf, enrollment, hide_stroke)
def grades_report_header(pdf, options={})
enrollment ||= options[:enrollment]
pdf.bounding_box([0, pdf.cursor - 3], :width => 560) do
pdf.font('Courier', :size => 8) do
pdf.font('FreeMono', :size => 8) do
pdf.line_width 0.5

common_header_part1(pdf, enrollment)
Expand Down Expand Up @@ -113,7 +113,7 @@ def grades_report_header(pdf, options={})
def enrollment_header(pdf, options={})
enrollment ||= options[:enrollment]
pdf.bounding_box([0, pdf.cursor - 3], :width => 560) do
pdf.font('Courier', :size => 8) do
pdf.font('FreeMono', :size => 8) do
pdf.line_width 0.5

common_header_part1(pdf, enrollment, [
Expand Down Expand Up @@ -199,7 +199,7 @@ def transcript_table(pdf, options={})

pdf.table(header, :column_widths => [560],
:row_colors => ["E5E5FF"],
:cell_style => {:font => "Helvetica",
:cell_style => {:font => "FreeSans",
:size => 9,
:inline_format => true,
:border_width => 1,
Expand All @@ -226,7 +226,7 @@ def transcript_table(pdf, options={})

pdf.table(header, :column_widths => table_width,
:row_colors => ["E5E5FF"],
:cell_style => {:font => "Helvetica",
:cell_style => {:font => "FreeSans",
:size => 9,
:inline_format => true,
:border_width => 1,
Expand Down Expand Up @@ -287,7 +287,7 @@ def transcript_table(pdf, options={})

pdf.table(table_data, :column_widths => table_width,
:row_colors => ["F2F2FF", "E5E5FF"],
:cell_style => {:font => "Helvetica",
:cell_style => {:font => "FreeSans",
:size => 9,
:inline_format => true,
:border_width => 1,
Expand All @@ -298,7 +298,7 @@ def transcript_table(pdf, options={})
}
) do |table|
table.column(1).align = :left
table.column(1).font = "Helvetica"
table.column(1).font = "FreeSans"
table.column(1).padding = [2, 4]
end
pdf.fill_color "000080"
Expand All @@ -325,7 +325,7 @@ def transcript_table(pdf, options={})
]]
pdf.table(footer, :column_widths => table_width,
:row_colors => ["E5E5FF"],
:cell_style => {:font => "Helvetica",
:cell_style => {:font => "FreeSans",
:size => 9,
:inline_format => true,
:border_width => 1,
Expand Down Expand Up @@ -369,7 +369,7 @@ def grades_report_table(pdf, options={})
pdf.table(header, :column_widths => table_width,
:width => 560,
:row_colors => ["E5E5FF"],
:cell_style => {:font => "Helvetica",
:cell_style => {:font => "FreeSans",
:size => 9,
:inline_format => true,
:border_width => 1,
Expand Down Expand Up @@ -436,7 +436,7 @@ def grades_report_table(pdf, options={})

pdf.table(table_data, :column_widths => table_width,
:row_colors => ["F2F2FF", "E5E5FF"],
:cell_style => {:font => "Helvetica",
:cell_style => {:font => "FreeSans",
:size => 9,
:inline_format => true,
:border_width => 1,
Expand All @@ -449,7 +449,7 @@ def grades_report_table(pdf, options={})
table.row(0).borders = [:left, :right, :top]
table.row(0).border_top_width = 1
table.column(1).align = :left
table.column(1).font = "Helvetica"
table.column(1).font = "FreeSans"
table.column(1).padding = [2, 4]
bold_rows.each do |i|
table.column(1).row(i).align = :right
Expand All @@ -466,7 +466,7 @@ def grades_report_table(pdf, options={})
]
pdf.table(footer, :column_widths => table_width,
:row_colors => ["E5E5FF"],
:cell_style => {:font => "Helvetica",
:cell_style => {:font => "FreeSans",
:size => 9,
:inline_format => true,
:border_width => 1,
Expand Down Expand Up @@ -552,7 +552,7 @@ def thesis_table(curr_pdf, options={})
end

if not data_table_rows.nil?
table = curr_pdf.make_table(data_table_rows, :cell_style => {:borders => [:left, :right, :bottom, :top], :border_bottom_width => 0.5, :border_top_width => 0.5, :width => 560, :padding_top => 5.5, :padding_bottom => 7.5, :inline_format => true, :font => "Courier", :size => 8, :border_color => "000080" })
table = curr_pdf.make_table(data_table_rows, :cell_style => {:borders => [:left, :right, :bottom, :top], :border_bottom_width => 0.5, :border_top_width => 0.5, :width => 560, :padding_top => 5.5, :padding_bottom => 7.5, :inline_format => true, :font => "FreeMono", :size => 8, :border_color => "000080" })
table.row(0).border_top_width = 1.0

if data_table_rows_defense_committee.length == 0
Expand All @@ -565,17 +565,17 @@ def thesis_table(curr_pdf, options={})
if (not data_table_rows_defense_committee.nil?) && (data_table_rows_defense_committee.length > 0)

#title row
table_committee = curr_pdf.make_table(data_table_rows_defense_committee[0,1], :cell_style => {:borders => [:top, :left, :right], :border_top_width => 0.5, :width => 560, :padding_top => 5.5, :padding_bottom => 4.9, :inline_format => true, :font => "Courier", :size => 8, :border_color => "000080" })
table_committee = curr_pdf.make_table(data_table_rows_defense_committee[0,1], :cell_style => {:borders => [:top, :left, :right], :border_top_width => 0.5, :width => 560, :padding_top => 5.5, :padding_bottom => 4.9, :inline_format => true, :font => "FreeMono", :size => 8, :border_color => "000080" })
table_committee.draw

#middle rows (if any)
if data_table_rows_defense_committee.length > 2
table_committee = curr_pdf.make_table(data_table_rows_defense_committee[1,data_table_rows_defense_committee.length - 2], :cell_style => {:borders => [:left, :right], :width => 560, :padding_top => 0.0, :padding_bottom => 1.0, :inline_format => true, :font => "Courier", :size => 8, :border_color => "000080" })
table_committee = curr_pdf.make_table(data_table_rows_defense_committee[1,data_table_rows_defense_committee.length - 2], :cell_style => {:borders => [:left, :right], :width => 560, :padding_top => 0.0, :padding_bottom => 1.0, :inline_format => true, :font => "FreeMono", :size => 8, :border_color => "000080" })
table_committee.draw
end

#last row
table_committee = curr_pdf.make_table(data_table_rows_defense_committee[data_table_rows_defense_committee.length - 1,data_table_rows_defense_committee.length], :cell_style => {:borders => [:left, :right, :bottom], :border_bottom_width => 1.0 , :width => 560, :padding_top => 0.0, :padding_bottom => 8.5, :inline_format => true, :font => "Courier", :size => 8, :border_color => "000080" })
table_committee = curr_pdf.make_table(data_table_rows_defense_committee[data_table_rows_defense_committee.length - 1,data_table_rows_defense_committee.length], :cell_style => {:borders => [:left, :right, :bottom], :border_bottom_width => 1.0 , :width => 560, :padding_top => 0.0, :padding_bottom => 8.5, :inline_format => true, :font => "FreeMono", :size => 8, :border_color => "000080" })
table_committee.draw
end

Expand Down
44 changes: 35 additions & 9 deletions app/helpers/pdf_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def signature_footer(pdf, options={})
last_box_width2 = 335

last_box_y = pdf.bounds.bottom - FOOTER_TOP_MARGIN
pdf.font('Courier', :size => 8) do
pdf.font('FreeMono', :size => 8) do
pdf.bounding_box([0, last_box_y], :width => last_box_width1, :height => last_box_height) do
pdf.stroke_bounds
current_x = x
Expand All @@ -88,7 +88,7 @@ def signature_footer(pdf, options={})
end
end

pdf.font('Courier', :size => 6) do
pdf.font('FreeMono', :size => 6) do
pdf.bounding_box([last_box_width1, last_box_y], :width => last_box_width2, :height => last_box_height) do
pdf.stroke_bounds
current_x = x
Expand All @@ -111,7 +111,7 @@ def signature_footer(pdf, options={})
end
end

pdf.font('Courier', :size => 8) do
pdf.font('FreeMono', :size => 8) do
pdf.bounding_box([last_box_width1 + last_box_width2, last_box_y], :width => pdf.bounds.right - last_box_width1 - last_box_width2 - diff_width, :height => last_box_height) do
pdf.stroke_bounds
current_x = x
Expand All @@ -133,7 +133,7 @@ def datetime_footer(pdf, options={})
last_box_width2 = 335

last_box_y = pdf.bounds.bottom #pdf.bounds.bottom + last_box_height# pdf.cursor - 15
pdf.font('Courier', :size => 8) do
pdf.font('FreeMono', :size => 8) do
pdf.bounding_box([0, last_box_y], :width => last_box_width1, :height => last_box_height) do
current_x = x
pdf.move_down last_box_height/2
Expand Down Expand Up @@ -177,6 +177,32 @@ def new_document(name, title, options = {}, &block)
:bottom_margin => (pdf_config.signature_footer ? 96 + FOOTER_TOP_MARGIN : 1.cm),
:filename => name
}.merge(options)) do |pdf|

freefont_directory = "#{Rails.root}/vendor/assets/fonts/gnu-freefont/"

pdf.font_families.update("FreeMono" => {
:normal => freefont_directory + "FreeMono.ttf",
:bold => freefont_directory + "FreeMonoBold.ttf",
:italic => freefont_directory + "FreeMonoOblique.ttf",
:bold_italic => freefont_directory + "FreeMonoBoldOblique.ttf"
})

pdf.font_families.update("FreeSans" => {
:normal => freefont_directory + "FreeSans.ttf",
:bold => freefont_directory + "FreeSansBold.ttf",
:italic => freefont_directory + "FreeSansOblique.ttf",
:bold_italic => freefont_directory + "FreeSansBoldOblique.ttf"
})

pdf.font_families.update("FreeSerif" => {
:normal => freefont_directory + "FreeSerif.ttf",
:bold => freefont_directory + "FreeSerifBold.ttf",
:italic => freefont_directory + "FreeSerifOblique.ttf",
:bold_italic => freefont_directory + "FreeSerifBoldOblique.ttf"
})

pdf.fallback_fonts(["FreeSans"])

pdf.fill_color "000080"
pdf.stroke_color '000080'
header(pdf, title, pdf_config)
Expand All @@ -193,7 +219,7 @@ def new_document(name, title, options = {}, &block)
pdf.create_stamp("watermark") do
pdf.rotate(60, :origin => [0, 0]) do
pdf.fill_color "993333"
pdf.font('Courier', :size => 22) do
pdf.font('FreeMono', :size => 22) do
pdf.draw_text I18n.t('pdf_content.professor_watermark'), :at => [0, 0]
end
pdf.fill_color "000000"
Expand All @@ -212,7 +238,7 @@ def pdf_list_with_title(pdf, title, data, options={}, &block)

pdf_table_with_title(pdf, [width], title, [], data, options) do |table|
table.column(0).align = :left
table.column(0).font = "Courier"
table.column(0).font = "FreeMono"
#table.column(0).size = 10
table.column(0).padding = [2, 5]
yield table unless block.nil?
Expand All @@ -232,7 +258,7 @@ def pdf_table_with_title(pdf, widths, title, header, data, options={}, &block)
pdf.table(title, :column_widths => [widths.sum],
:width => widths.sum,
:row_colors => ["E5E5FF"],
:cell_style => {:font => "Helvetica",
:cell_style => {:font => "FreeSans",
:size => 9,
:inline_format => true,
:border_width => 1,
Expand All @@ -255,7 +281,7 @@ def simple_pdf_table(pdf, widths, header, data, options={}, &block)
pdf.table(header, :column_widths => widths,
:width => widths.sum,
:row_colors => ["E5E5FF"],
:cell_style => {:font => "Helvetica",
:cell_style => {:font => "FreeSans",
:size => 9,
:inline_format => true,
:border_width => 1,
Expand All @@ -274,7 +300,7 @@ def simple_pdf_table(pdf, widths, header, data, options={}, &block)
pdf.table(data, :column_widths => widths,
:width => widths.sum,
:row_colors => ["F2F2FF", "E5E5FF"],
:cell_style => {:font => "Helvetica",
:cell_style => {:font => "FreeSans",
:size => 9,
:inline_format => true,
:border_width => 1,
Expand Down
Loading

0 comments on commit f0057ec

Please sign in to comment.