Generate PDF from Rails

前端 未结 10 1146
醉酒成梦
醉酒成梦 2020-12-02 06:36

The Ruby On Rails Wiki lists a couple of libraries that facilitate PDF generation in Rails. I need to print out address labels (in letter f

相关标签:
10条回答
  • 2020-12-02 07:08

    I've used flying saucer for pdf generation from html. It's a java library but you can use the Ruby-Java Bridge gem to access it in your rails app. It's css 2.1 compliant and has a few additions from css3 to allow some extra control over paging. I'd recommend it as it doesn't require you to put 'pdf code' in your html, you can use the same views and partials to display to the browser as you do to generate pdfs.

    Flying Saucer: https://github.com/flyingsaucerproject/flyingsaucer

    Ruby Java Bridge: http://rjb.rubyforge.org/

    I use this module code to generate the pdfs

    require 'rubygems'
    require 'rjb'
    
    module Html2Pdf
    
      def self.included(controller)
          controller.send :helper_method, :create_pdf
      end
    
      def create_pdf(options = {})
          itext = "#{RAILS_ROOT}/lib/html2pdf/jars/iText-2.0.8.jar"
          core_renderer = "#{RAILS_ROOT}/lib/html2pdf/jars/core-renderer.jar"
          xerces = "#{RAILS_ROOT}/lib/html2pdf/jars/xml-apis-xerces-2.9.1.jar" 
          joinchar = (RUBY_PLATFORM.include? 'mswin') ? ';' : ':'
              classpath = [itext, core_renderer, xerces].join(joinchar)
          Rjb::load(classpath, jvmargs=['-Djava.awt.headless=true'])
          if options[:htmlstring].nil?
          options[:layout] ||= false
              options[:template] ||= File.join(controller_path,action_name+".pdf.erb")
              html_string = render_to_string(:template => options[:template], :layout => options[:layout])
        else
              html_string = options[:htmlstring]
        end
        # Make all paths relative, on disk paths...
        html_string.gsub!(".com:/",".com/") # strip out bad attachment_fu URLs
        html_string.gsub!( /src=["']+([^:]+?)["']/i ) { |m| "src=\"file:///#{RAILS_ROOT}/public/" + $1 + '"' } # re-route absolute paths
        html_string.gsub!( /url\(["']+([^:]+?)["']/i ) { |m| "url\(\"file:///#{RAILS_ROOT}/public/" + $1 + '"' } # re-route absolute paths
        # Remove asset ids on images with a regex // tbh i can't remember what this line is for but i'm sure it did something awesome
        html_string.gsub!( /src=["'](\S+\?\d*)["']/i ) { |m| 'src="' + $1.split('?').first + '"' } 
        filename = "#{RAILS_ROOT}/public/pdfs/"+options[:filename]+".pdf"
        fileOutputStream = Rjb::import('java.io.FileOutputStream')
          iTextRenderer = Rjb::import('org.xhtmlrenderer.pdf.ITextRenderer')
          renderer = iTextRenderer.new
          renderer.setDocumentFromString(html_string)
          os = fileOutputStream.new(filename)
          renderer.layout()
          renderer.createPDF(os)
          os.close()
      end
    
    end
    

    Calling it with code like this:

    def generate_pdf  
      htmlsrc = render_to_string(:partial => 'invoice', :layout => false)
      rnd = Time.now.to_s(:datentime).gsub!(/[\/ \.:]/,'')
      filename = "docstore/tmp_#{rnd}"
      create_pdf(:htmlstring => htmlsrc, :filename => filename)
      contents = open("#{RAILS_ROOT}/public/pdfs/#{filename}.pdf", "rb") { |io| io.read }
      File.delete("#{RAILS_ROOT}/public/pdfs/#{filename}.pdf")
      respond_to do | wants |
        wants.html { render :text => contents, :content_type => 'application/pdf' }
      end    
    end
    
    0 讨论(0)
  • 2020-12-02 07:12

    Prawn is the way to go. Now with prawn-labels that is really easy to do.

    Check out the project's README here:

    https://github.com/jordanbyron/prawn-labels#readme

    This is a super simple example being used in a Rails controller. Don't forget to add gem 'prawn-labels' to your Gemfile.

    names = %w{Jordan Kelly Greg Bob}
    
    labels = Prawn::Labels.render(names, :type => "Avery5160") do |pdf, name|
      pdf.text name
    end
    
    send_data labels, :filename => "names.pdf", :type => "application/pdf"
    
    0 讨论(0)
  • 2020-12-02 07:14

    There's also RTeX. That works well if you're willing to translate to LaTeX first. LaTeX is a very good way to store marked-up documents. It just depends on how static each document is. If most of the document is dynamic, you might do better with Prawn or PDF::Writer. If most of it is static, with just a couple of text-replacements for each, LaTeX might be a better choice.

    0 讨论(0)
  • 2020-12-02 07:19

    There is also PDFKit. It's quite interesting too.

    0 讨论(0)
  • 2020-12-02 07:23

    The best I've seen so far is Prawn:

    • Prawn http://prawn.majesticseacreature.com/
    • http://advent2008.hackruby.com/past/2008/12/17/awn_and_the_future_of_ruby_pdf_generation/
    • http://railstips.org/2008/10/14/how-to-generate-pdfs-in-rails-with-prawn
    • Prawn Rails plugin: http://www.cracklabs.com/prawnto
    0 讨论(0)
  • 2020-12-02 07:24

    Prawn with Prawnto for sure. The DSL is a real treat, as is the simplicity of being able to treat PDF as any other format in a respond_to format block:

    respond_to do |format|
    format.pdf { render :layout => false }
    

    There's a tutorial video on Prawn here:

    0 讨论(0)
提交回复
热议问题