How to change the font in shrimp

What I'm trying to do is generate a PDF file using shrimp, having some language-specific characters.

And as a result, I get the following error:

raise Prawn::Errors::IncompatibleStringEncoding, "Your document includes text that not compatible with the Windows-1252 character set.\n" \ "If you need full UTF-8 support, use TTF fonts instead of PDF built-in fonts\n." 

So I tried to change the font by doing the following:

 # app/models/prawn/change_font_decorator.rb Prawn::Document.generate("output.pdf") do font_families.update("Arial" => { :normal => Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf"), :italic => Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf"), :bold => Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf"), :bold_italic => Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf") }) font "Arial" end 

However, I get the same error when trying to create a PDF file.

Any ideas on how to solve this?

+8
source share
3 answers

The shrimp guide is a great reference and includes sections on using fonts. The External Fonts section is especially important for your problem.

Here is a basic example that should work, although it does not support bold and italics:

 Prawn::Document.generate("output.pdf") do font Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf") text "Euro €" end 

To also use bold and italic, it is best to register a font family that does not conflict with one of the standard PDF fonts:

 Prawn::Document.generate("output.pdf") do font_families.update("OpenSans" => { :normal => Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf"), :italic => Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf"), :bold => Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf"), :bold_italic => Rails.root.join("app/assets/fonts/OpenSans-Regular.ttf") }) font "OpenSans" text "Euro €" end 
+22
source

If you create your PDF using initialization, you can simply update the font families in the initialize method, and then set the desired font.

 class InvoicePdf < Prawn::Document def initialize() super() self.font_families.update("DejaVuSans" => {:normal => "#{Rails.root}/public/DejaVuSans.ttf"}) font "DejaVuSans" business_logo invoice_items footer end def business_logo ##stuff here end end 
+3
source

I actually went into the gems folder and in Prawn / font.rb.

I found the following function:

 def font_families @font_families ||= Hash.new { |h,k| h[k] = {} }.merge!( { "Courier" => { :bold => "Courier-Bold", :italic => "Courier-Oblique", :bold_italic => "Courier-BoldOblique", :normal => "Courier" }, "Times-Roman" => { :bold => "Times-Bold", :italic => "Times-Italic", :bold_italic => "Times-BoldItalic", :normal => "Times-Roman" }, "Helvetica" => { :bold => "Helvetica-Bold", :italic => "Helvetica-Oblique", :bold_italic => "Helvetica-BoldOblique", :normal => "Helvetica" } }) end 

I edited it to enable Arial:

 def font_families @font_families ||= Hash.new { |h,k| h[k] = {} }.merge!( { "Courier" => { :bold => "Courier-Bold", :italic => "Courier-Oblique", :bold_italic => "Courier-BoldOblique", :normal => "Courier" }, "Times-Roman" => { :bold => "Times-Bold", :italic => "Times-Italic", :bold_italic => "Times-BoldItalic", :normal => "Times-Roman" }, "Helvetica" => { :bold => "Helvetica-Bold", :italic => "Helvetica-Oblique", :bold_italic => "Helvetica-BoldOblique", :normal => "Helvetica" }, "Arial" => { :normal => "public/fonts/arial.ttf", :italic => "public/fonts/ariali.ttf.ttf", :bold => "public/fonts/arialbd.ttf", :bold_italic => "public/fonts/arialbi.ttf"} }) end 

and viola! it worked!

-1
source

Source: https://habr.com/ru/post/1259892/


All Articles