Table: RailsNotes
User: dreamable
Created at: 2021-03-21 22:43:24 UTC
Updated at: 2021-03-22 08:12:47 UTC
Reference:(Table ID 3, Record ID 31)

标题 :
Generate PDF/DOC
笔记 :

What is the best way to generate PDF/HTML/DOCX in Ruby/Rails

PDF:

  1. Prawn: native and more flexible
  2. Wicked-PDF: Wicked depends on wkhtmltopdf and uses systems call
  3. CombinePDF: combine PDF files
  4. pdfcrowd: API for http://pdfcrowd.com

DOC:

  1. Caracal
  2. docx
  3. docx_replace
  4. docx
  5. html2doc

Prawn Notes: guide

  1. Add links:

    • methods
    • 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") } )
      
  2. support Chinese

    • gkai00mp.ttf is not downloaded by bundle install

      • The standard prawn gem is of version 2.4.0, may be dated without all fonts
      • Change to use the latest version from GitHub
       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

      • guide
      • May have some bug, has to set font.
      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
    
Tag: