1. install the pdf-writer
gem install pdf-writer
2. require & register in the rails env.
#vi environment.rb
require 'pdf/writer'
Mime::Type.register 'application/pdf', :pdf
3. usage:
# lib/product_drawer.rb
def self.draw(products)
pdf = PDF::Writer.new
products.each do |product|
pdf.text product.name
end
pdf.render
end
# products_controller.rb
def index
@products = Product.find(:all)
respond_to do |format|
format.html
format.pdf do
send_data ProductDrawer.draw(@products),
:filename => 'products.pdf',
:type=> 'application/pdf',
:disposition => 'inline'
end
end
end
# in view
<p><%= link_to 'PDF Format', formatted_products_path(:pdf) %></p>
# in browser
<A href="http://localhost:3000/products.pdf" target=_blank>http://localhost:3000/products.pdf</A>
分享到:
相关推荐
### Rails 4 in Action, 第二版:关键知识点解析 #### 一、Rails 4简介与新特性 **Rails 4 in Action, 第二版** 是一本深入介绍Ruby on Rails框架的专业书籍。该书由Ryan Bigg、Yehuda Katz、Steve Klabnik和...
Run tests when anyone checks code in. How to create tagged data the right way. and many, many more... Owning "Rails Recipes" is like having the best Rails programmers sitting next to you while you ...
The more confident reader might be able to get started in Rails using just this book, extensive online resources, and his wits, but there are other publications that are more introductory in nature ...
We still start with a step-by-step walkthrough of building a real application, and in-depth chapters look at the built-in Rails features. This edition now gives new Ruby and Rails users more ...
### Rails Recipes Final.pdf 知识点解析 #### 标题:Rails Recipes Final.pdf - **核心概念**:本书名为《Rails Recipes》,旨在提供一系列针对Ruby on Rails开发过程中的实用技巧和解决方案。 #### 描述:...
根据提供的文件信息,可以看出这是一份关于 Ruby on Rails(简称 Rails)编程的书籍或资源列表。下面将基于这些信息来生成相关的知识点。 ### 关键词:Rails (Ruby on Rails) **1. Ruby on Rails 平台简介** - *...
Fueled by significant benefits and an impressive portfolio of real-world applications already in production, Rails is destined to continue making significant inroads in coming years. Each new Rails ...
#### Rubyisms in Rails 书中还强调了Ruby语言的一些独特特性和编程习惯,这些“Rubyisms”是Rails开发中不可或缺的一部分,能够帮助开发者更高效地使用Ruby进行编程。 #### Troubleshooting Ruby Processes 本书...
In-Place Form Editing**:介绍了一种让用户可以直接在页面上编辑表单数据的方法,而无需跳转到另一个页面。 - **2. Making Your Own JavaScript Helper**:教会读者如何创建自定义的 JavaScript 辅助函数来简化...
书中的源码部分,读者可以通过下载提供的“Rails 3 in Action.pdf”文件获取,这些源码可以作为实际项目开发的参考和起点。 总之,《Rails 3 in Action》是一本适合初学者和有经验的Rails开发者阅读的书籍,它覆盖...
讨论混入(Mix-in)的设计模式。 #### 2.3 元编程与DSL - **元编程**:Ruby强大的元编程能力允许程序在运行时修改自身的行为。 - **领域特定语言(DSL)**:介绍如何利用Ruby编写特定领域的语言,以提高开发效率。 ...
Rails 2, released in 2008, brings hundreds of improvements, including new support for RESTful applications, new generator options, and so on. And, as importantly, we’ve all learned a lot more about ...