What is the best way to generate PDF/HTML/DOCX in Ruby/Rails
PDF:
DOC:
Prawn Notes: guide
Add links:
inline_format: works
pdf.text "<link href='#{link}'>#{table.name}</link>", align: :center, size: 32, style: :bold, inline_format: true
formatted_text: works, but can't align by center automatically, has to move_down for new text
pdf.formatted_text_box([{text: table.name, link: link, styles: [:bold], size: 32, align: :center }])
link_annotation: has to put position?
pdf.link_annotation([100, 100, 5, 5], :Border => [0,0,1], :A => { :Type => :Action, :S => :URI, :URI => Prawn::LiteralString.new("http://google.com") } )
support Chinese
gkai00mp.ttf is not downloaded by bundle install
prawn
gem is of version 2.4.0, may be dated without all fonts gem 'prawn', :git => 'https://github.com/prawnpdf/prawn.git'
Specify fonts:
pdf.font("#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf") do
pdf.text '测试中文'
pdf.text 'test English'
pdf.text 'test English 和中文'
# NOTE: gkai doesn't support style like italic
end
fallback fonts for both languages
Prawn::Document.generate(file) do |pdf|
kai_path = "#{Prawn::BASEDIR}/data/fonts/gkai00mp.ttf"
kai_spec = { file: kai_path, font: 'Kai' }
#don't use a symbol to define the name of the font!
pdf.font_families['Kai'] = { normal: kai_spec,
bold: kai_spec,
italic: kai_spec,
bold_italic: kai_spec};
pdf.fallback_fonts(["Kai"]);
link = Rails.application.routes.url_helpers.table_records_path(table)
pdf.text "<link href='#{link}'>#{table.name}</link>", align: :center, size: 32, style: :bold, inline_format: true
pdf.text table.user.name, align: :center, size: 16 , style: :italic
head,rows = self.get_head_rows(table,records)
pdf.font("Kai") # Set default font as "Kai". Otherwise, my have error.
rows.each do |row|
pdf.start_new_page
head.zip(row).each do |name,value|
pdf.text "\n"+name.to_s+":"
pdf.text "#{value}" # Not work without using "Kai"
#pdf.text "测试中文English" # works without using "Kai"
end
end
end
Caracal Notes: guide
link & center
docx.h1 do
link(table.name, tlink)
align :center
end