`
magixyu
  • 浏览: 79431 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

PDF in Rails

    博客分类:
  • ROR
阅读更多
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, Second Edition.pdf

    ### Rails 4 in Action, 第二版:关键知识点解析 #### 一、Rails 4简介与新特性 **Rails 4 in Action, 第二版** 是一本深入介绍Ruby on Rails框架的专业书籍。该书由Ryan Bigg、Yehuda Katz、Steve Klabnik和...

    Rails Recipes英文版(清晰文字pdf+源码)

    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 Rails 5 Way-Leanpub(2017).pdf

    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 ...

    Agile Web Development with Rails 4

    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 Final.pdf - **核心概念**:本书名为《Rails Recipes》,旨在提供一系列针对Ruby on Rails开发过程中的实用技巧和解决方案。 #### 描述:...

    rails22cn.pdf

    根据提供的文件信息,可以看出这是一份关于 Ruby on Rails(简称 Rails)编程的书籍或资源列表。下面将基于这些信息来生成相关的知识点。 ### 关键词:Rails (Ruby on Rails) **1. Ruby on Rails 平台简介** - *...

    Advanced Rails Recipes(英语清晰文字pdf+源码)

    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 ...

    the rails way

    #### Rubyisms in Rails 书中还强调了Ruby语言的一些独特特性和编程习惯,这些“Rubyisms”是Rails开发中不可或缺的一部分,能够帮助开发者更高效地使用Ruby进行编程。 #### Troubleshooting Ruby Processes 本书...

    Rails Recipes (2006) .pdf

    In-Place Form Editing**:介绍了一种让用户可以直接在页面上编辑表单数据的方法,而无需跳转到另一个页面。 - **2. Making Your Own JavaScript Helper**:教会读者如何创建自定义的 JavaScript 辅助函数来简化...

    新书\"Rails 3 in Action\"介绍与下载

    书中的源码部分,读者可以通过下载提供的“Rails 3 in Action.pdf”文件获取,这些源码可以作为实际项目开发的参考和起点。 总之,《Rails 3 in Action》是一本适合初学者和有经验的Rails开发者阅读的书籍,它覆盖...

    Ruby_for_Rails.pdf

    讨论混入(Mix-in)的设计模式。 #### 2.3 元编程与DSL - **元编程**:Ruby强大的元编程能力允许程序在运行时修改自身的行为。 - **领域特定语言(DSL)**:介绍如何利用Ruby编写特定领域的语言,以提高开发效率。 ...

    Agile Web Development with Rails, Third Edition 2009 (PDF + 代码)

    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 ...

Global site tag (gtag.js) - Google Analytics