prawnpdf / prawn

Fast, Nimble PDF Writer for Ruby
https://prawnpdf.org
Other
4.67k stars 688 forks source link

no support for ttf fonts in Prawn-tables? #1054

Open mail2sanand opened 6 years ago

mail2sanand commented 6 years ago

Hi I am trying to display some up(HTML codes: ↑), down(HTML codes: ↓) and other special characters in Prawn PDF.

Some articles had suggested using UTF-8 codes for these special characters. So, started using the ttf fonts instead of the default afm fonts.

I was able to display them with normal text, by using ttf fonts. but when it to tables, it is not working.

Here is what I had done .

Registering of a new font family

  1. font_families.update( "open-sans-ttf" => { :normal => "#{Rails.root.join("app/assets/header/fonts/OpenSans-Regular.ttf")}" })

Using this font in text is working fine.

  1. font("open-sans-ttf"){ text "\u2191"}

Using the new ttf font in table

  1. patient_cmc_details = ([ [{:content => " CC : \u2191 ", :border_width => 0}]])

Options Tried :

  1. table patient_cmc_details, :cell_style => {:font => "open-sans-ttf", :inline_format => true,:height => 20, :size => 10}

  2. table patient_cmc_details, :cell_style => {:font => "#{Rails.root.join("app/assets/header/fonts/OpenSans-Regular.ttf")}", :inline_format => true,:height => 20, :size => 10}

  3. patient_cmc_details = ([ [{:content => " CC : # \u2191 ",:font => "#{Rails.root.join("app/assets/header/fonts/OpenSans-Regular.ttf")}", :border_width => 0}]])

Error Reported

  1. Your document includes text that's not compatible with the Windows-1252 character set. If you need full UTF-8 support, use TTF fonts instead of PDF's built-in fonts.

I happen to google for some help but not helpful. Can someone suggest a work-around

Anand

CyborgMaster commented 6 years ago

This does appear to be a bug in the main prawn gem. I had custom fonts working in tables in prawn (2.1.0) but once I upgraded to prawn (2.2.2), I started to get the same crash:

Your document includes text that's not compatible with the Windows-1252 character set. If you need full UTF-8 support, use TTF fonts instead of PDF's built-in fonts.

I came up with the following hacky workaround that appears to be ok for now.

old (working on 2.1.0):

attentions = [["\uF003"]]
pdf.make_table attentions, cell_style: {
  font: 'Font Awesome',
}

new (working on 2.2.2):

attentions = [["<font name='Font Awesome'>\uF003</font>"]]
pdf.make_table attentions, cell_style: {
  font: 'Font Awesome',
  inline_format: true,
}