【问题标题】:Generate pdf from Rails 3 - what tool to choose?从 Rails 3 生成 pdf - 选择什么工具?
【发布时间】:2011-05-06 07:06:56
【问题描述】:

我需要能够将 Rails 3 项目中的一些视图呈现为 PDF。我以前从未使用 ruby​​/rails 的 PDF 生成技术,所以我研究了一些流行的方法,例如 PrawnPDF::Writer,但是所有的例子和文章我发现到目前为止似乎已经过时并且仅适用于 rails 2.x。我还没有看到一个有效的 Rails3 例子;尝试自己安装 prawn 和 prawnto gems 并复制this Railscasts episode 中描述的示例,但我收到了无法识别 prawnto 方法的错误。我不确定这是实现错误还是只是不兼容的迹象,但是看到其他人在网上分享 prawn 在 Rails3 中不再为他们工作,我没有费心去追踪进一步编码。

有没有人找到在 Rails3 中生成 pdf 的可靠解决方案?您能否分享它或将我指向外部资源和文档? 非常感谢!

【问题讨论】:

标签: ruby-on-rails pdf ruby-on-rails-3 pdf-generation prawn


【解决方案1】:

旧问题的新答案,以防其他人偶然发现:WickedPDF(它使用 wkhtmltopdf 就像 PDFkit 一样)让这变得轻而易举。

https://github.com/mileszs/wicked_pdf

【讨论】:

    【解决方案2】:

    Prawn 确实适用于 Rails 3。我个人使用它没有任何问题。您必须获得最新版本的 gem 和用于 rails 的 prawnto 插件。

    PDFkit 确实具有使用 Webkit 渲染引擎的优势,因此您可以使用 CSS 来定义您的布局,并且您可以通过 Safari 和 Chrome 免费获得匹配的网页。它的学习曲线比 Prawn 稍微好一些。

    【讨论】:

    • 是的,Prawnto 现在死了。但是 Prawn 仍然可以与 rails 一起使用。 Ryan bates shows how
    【解决方案3】:

    你见过PDFkit吗?我很确定它适用于 Rails 3,它是一个 Rack 中间件,可以将任何 HTML 页面转换为与以 .pdf 结尾的路由匹配的 PDF

    【讨论】:

      【解决方案4】:

      关于 prawn,这里是 Rails 3 的无缝集成,似乎工作得很好:https://github.com/Whoops/prawn-rails

      【讨论】:

        【解决方案5】:

        您可以使用Report gem,它可以生成 PDF,也可以生成 XLSX 和 CSV。

        # a fake Manufacturer class - you probably have an ActiveRecord model
        Manufacturer = Struct.new(:name, :gsa)
        
        require 'report'
        class ManufacturerReport < Report
          table 'Manufacturers' do # you can have multiple tables, which translate into multiple sheets in XLSX
            head do
              row 'Manufacturer report'
            end
            body do
              rows :manufacturers
              column 'Name', :name
              column 'GSA?', :gsa
            end
          end
          # you would want this so that you can pass in an array
          # attr_reader :manufacturers
          # def initialize(manufacturers)
          #   @manufacturers = manufacturers
          # end
          def manufacturers
            [
              Manufacturer.new('Ford', true),
              Manufacturer.new('Fischer', false),
              Manufacturer.new('Tesla', nil),
            ]
          end
        end
        

        当你调用report.pdf.path时,在tmp目录中生成一个PDF:

        report = ManufacturerReport.new
        puts report.pdf.path #=> /tmp/185051406_Report__Pdf.pdf
        puts report.xlsx.path #=> /tmp/185050541_Report__Xlsx.xlsx
        

        你可以在你的控制器中这样做:

        @manufacturers = Manufacturer.all
        respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @manufacturers }
          format.pdf do
            report = ManufacturerReport.new(@manufacturers) # using the commented-out code
            send_file report.pdf.path, :type => 'application/pdf', :disposition => 'attachment', :filename => 'ManufacturersReport.pdf'
            # tmp files are periodically cleaned up by the operating system, but if you want to be extra clean you can call
            # report.cleanup
            # but this may remove the tmp files before apache/nginx/etc. finishes delivering the file
          end
        end
        

        最终结果:

        PDF

        XLSX

        请注意,XLSX 会自动为您添加一个自动过滤器。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-30
          • 1970-01-01
          • 2015-07-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多