prawn_rails 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +81 -0
  3. data/Rakefile +56 -0
  4. data/init.rb +1 -0
  5. data/lib/prawn_rails.rb +33 -0
  6. data/rails/init.rb +2 -0
  7. metadata +103 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Walton Hoops
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,81 @@
1
+ = Prawn Rails
2
+
3
+ Prawn Rails provides a simple way of creating PDF views in Rails 3 using the prawn library.
4
+
5
+ To use Prawn Rails simply add the line
6
+
7
+ gem 'prawn_rails'
8
+
9
+ to your Gemfile and then run
10
+
11
+ bundle install
12
+
13
+ That's it! You can now create views named
14
+
15
+ [action].pdf.prawn
16
+
17
+ which will be used whenever the user requests a page with a 'pdf' extension
18
+
19
+ == Usage
20
+
21
+ prawn_rails is designed to provide only a very thin wrapper around Prawn itself. A prawn_rails view should consist of only a call to the function prawn_document and a block. This will create an instance of Prawn::Document and yield it to the block.
22
+ For a simple pdf view try:
23
+
24
+ views/.../simple.pdf.prawn
25
+
26
+ prawn_document() do |pdf|
27
+ pdf.text "Hello World"
28
+ end
29
+
30
+ This will create a simple PDF with only the text Hello World.
31
+
32
+ Like normal Rails views, instance variables assigned in the controller are made available in the view. For example:
33
+
34
+ home_controller.rb
35
+
36
+ class HomeController < ApplicationController
37
+ def index
38
+ @people=['Jane','John','Jack']
39
+ end
40
+ end
41
+
42
+ views/.../index.pdf.prawn
43
+
44
+ prawn_document(:page_layout => :landscape) do |pdf|
45
+ @people.each {|person| pdf.text person}
46
+ end
47
+
48
+ This will produce a pdf with Jane, John, and Jack all written on seperate lines.
49
+
50
+ Notice we passed a hash into prawn_document. Any parameters placed in this hash will be passed to the constructor of Prawn::Document, with one exception. The renderer option will be removed before creating the document and can be used to override the class to be used for rendering with a subclass of Prawn::Document like so:
51
+
52
+ views/.../override.pdf.prawn
53
+
54
+ prawn_document({:renderer => ApplicationHelper::Foo})
55
+
56
+ So for the view above you could have an application helper file that looks like:
57
+
58
+ application_helper.rb
59
+
60
+ module ApplicationHelper
61
+ class Foo < Prawn::Document
62
+ def initialize
63
+ super
64
+ text "Foo"
65
+ text "Bar"
66
+ end
67
+ end
68
+ end
69
+
70
+ This would generate a canned report with just the lines Foo and Bar.
71
+
72
+ == Gotchas
73
+
74
+ The one major gotcha at this point is that layouts do not work. Do not attempt to make an app/views/layouts/application.pdf.prawn. All your pdf views will quit. This is something I hope to fix in a later release. In the meantime I recommend using custom classes like the one above to achieve a similair effect.
75
+
76
+ == Examples
77
+
78
+ For examples see: http://github.com/Volundr/prawn_rails_demo
79
+
80
+
81
+ Copyright (c) 2010 Walton Hoops, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'rake/gempackagetask'
5
+ require 'rubygems'
6
+
7
+ PKG_FILES = FileList[
8
+ '[a-zA-Z]*',
9
+ 'lib/**/*',
10
+ 'rails/**/*',
11
+ 'test/**/*'
12
+ ]
13
+
14
+ spec = Gem::Specification.new do |s|
15
+ s.name = "prawn_rails"
16
+ s.version = "0.0.3"
17
+ s.author = "Walton Hoops"
18
+ s.email = "me@waltonhoops.com"
19
+ s.homepage = "http://github.com/Volundr/prawn-rails"
20
+ s.add_dependency('rails', '>=3.0.0')
21
+ s.add_dependency('prawn', '>=0.8.4')
22
+ s.platform = Gem::Platform::RUBY
23
+ s.summary = "Integrates Prawn into Rails in a natural way"
24
+ s.description=<<-EOF
25
+ The prawn_rails gem provides a Prawn based view engine for creating PDFs with rails.
26
+ EOF
27
+ s.files = PKG_FILES.to_a
28
+ s.require_path = "lib"
29
+ s.has_rdoc = false
30
+ s.extra_rdoc_files = ["README.rdoc"]
31
+ end
32
+
33
+ desc 'Default: create gem'
34
+ task :default => :gem
35
+
36
+ desc 'Turn this plugin into a gem.'
37
+ Rake::GemPackageTask.new(spec) do |pkg|
38
+ pkg.gem_spec = spec
39
+ end
40
+
41
+ desc 'Test the prawn_rails plugin.'
42
+ Rake::TestTask.new(:test) do |t|
43
+ t.libs << 'lib'
44
+ t.libs << 'test'
45
+ t.pattern = 'test/**/*_test.rb'
46
+ t.verbose = true
47
+ end
48
+
49
+ desc 'Generate documentation for the prawn_rails plugin.'
50
+ Rake::RDocTask.new(:rdoc) do |rdoc|
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = 'PrawnRails'
53
+ rdoc.options << '--line-numbers' << '--inline-source'
54
+ rdoc.rdoc_files.include('README')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'prawn_rails'
@@ -0,0 +1,33 @@
1
+ require 'prawn'
2
+ require 'prawn/layout'
3
+ require 'prawn/security'
4
+
5
+ module Prawn
6
+ module Rails
7
+
8
+ module PrawnHelper
9
+
10
+ def prawn_document(opts={})
11
+ pdf = (opts.delete(:renderer) || Prawn::Document).new(opts)
12
+ yield pdf if block_given?
13
+
14
+ pdf
15
+ end
16
+
17
+ end
18
+
19
+ class TemplateHandler < ActionView::Template::Handler
20
+ self.default_format = :pdf
21
+
22
+ def self.call(template)
23
+ "#{template.source.strip}.render"
24
+ end
25
+
26
+ end
27
+
28
+ end
29
+ end
30
+
31
+ Mime::Type.register_alias "application/pdf", :pdf
32
+ ActionView::Template.register_template_handler(:prawn, Prawn::Rails::TemplateHandler)
33
+ ActionView::Base.send(:include, Prawn::Rails::PrawnHelper)
data/rails/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'prawn_rails'
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prawn_rails
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ version: 0.0.3
11
+ platform: ruby
12
+ authors:
13
+ - Walton Hoops
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-23 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: prawn
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 55
46
+ segments:
47
+ - 0
48
+ - 8
49
+ - 4
50
+ version: 0.8.4
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ description: " The prawn_rails gem provides a Prawn based view engine for creating PDFs with rails.\n"
54
+ email: me@waltonhoops.com
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - README.rdoc
61
+ files:
62
+ - README.rdoc
63
+ - init.rb
64
+ - Rakefile
65
+ - MIT-LICENSE
66
+ - lib/prawn_rails.rb
67
+ - rails/init.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/Volundr/prawn-rails
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 3
92
+ segments:
93
+ - 0
94
+ version: "0"
95
+ requirements: []
96
+
97
+ rubyforge_project:
98
+ rubygems_version: 1.3.7
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Integrates Prawn into Rails in a natural way
102
+ test_files: []
103
+