从 Rails 3 生成 pdf - 选择什么工具?

发布于 2024-10-01 00:38:21 字数 510 浏览 7 评论 0原文

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

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

I need to be able to render some views as PDFs from a Rails 3 project. I've never before used PDF generation techniques with ruby/rails, so I researched a few popular approaches such as Prawn and PDF::Writer, but all the examples and articles I found so far seem outdated and only applicable for rails 2.x. I haven't yet seen a working Rails3 example; tried myself installing prawn and the prawnto gems and reproducing the example described in this Railscasts episode, but I'm getting error of prawnto method not being recognized. I'm uncertain of whether this was an implementation error or just a sign of incompatibility, but seeing other people share on the web that prawn is no longer working for them in Rails3 I didn't bother tracing the code further.

Has anyone found a working reliable solution for pdf generation in Rails3? Could you possibly share it or point me to external resources and documentation?
Big thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

℉絮湮 2024-10-08 00:38:21

老问题的新答案,以防其他人偶然发现这个:WickedPDF(它使用 wkhtmltopdf 就像 PDFkit 一样)使这变得很简单。

https://github.com/mileszs/wicked_pdf

New answer to an old question, in case others stumble across this: WickedPDF (which uses wkhtmltopdf just like PDFkit) makes this a snap.

https://github.com/mileszs/wicked_pdf

夏日浅笑〃 2024-10-08 00:38:21

Prawn 确实可以与 Rails 3 配合使用。我个人使用它没有任何问题。您必须获取最新版本的 gem 和 Rails 的 prawnto 插件。

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

Prawn does work with Rails 3. I have personally used it with no problems. You do have to get the latest versions of the gem and the prawnto plugin for rails.

PDFkit does have the advantage of using the Webkit rendering engine, so you get to use CSS to define your layouts, and you get matching web pages for free with Safari and Chrome. It has a slightly nicer learning curve than Prawn.

夏日落 2024-10-08 00:38:21

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

Have you seen PDFkit? I'm pretty sure that works with Rails 3, it is a piece of Rack middleware that can convert any HTML page to PDF that matches a route ending in .pdf

善良天后 2024-10-08 00:38:21

关于 prawn,这里有一个 Rails 3 的无缝集成,似乎工作得很好: https://github.com /哎哟/虾轨

About prawn, here is a seamless integration for Rails 3 that seems to work just fine: https://github.com/Whoops/prawn-rails

撑一把青伞 2024-10-08 00:38:21

您可以使用 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

the pdf

XLSX

the xlsx

请注意,XLSX 添加了一个自动过滤器你自动。

You can use the Report gem, which generates PDF but also XLSX and 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

When you call report.pdf.path, a PDF is generating in the tmp directory:

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

You can do it in your controller like:

@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

End result:

PDF

the pdf

XLSX

the xlsx

Note that the XLSX has an autofilter added for you automatically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文