sbfaulkner-sinatra-prawn 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ = sinatra-prawn
2
+
3
+ sinatra-prawn is an extension for sinatra to enable rendering of pdf files
4
+ using prawn templates.
5
+
6
+ == Example
7
+
8
+ require 'rubygems'
9
+ require 'sinatra'
10
+ require 'sinatra/prawn'
11
+
12
+ set :prawn, { :page_layout => :landscape }
13
+
14
+ get '/' do
15
+ content_type 'application/pdf'
16
+ prawn :pdf
17
+ end
18
+
19
+ __END__
20
+
21
+ @@ pdf
22
+ pdf.text "Hello world!!!!!"
23
+
24
+ == Legal
25
+
26
+ Author:: S. Brent Faulkner <brentf@unwwwired.net>
27
+ License:: Copyright (c) 2009 unwwwired.net, released under the MIT license
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 9
4
+ :patch: 1
@@ -0,0 +1,31 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module Prawn
5
+ # Generate pdf file using Prawn.
6
+ # Takes the name of a template to render as a Symbol and returns a String with the rendered output.
7
+ #
8
+ # Options for prawn may be specified in Sinatra using set :prawn, { ... }
9
+ def prawn(template=nil, options={}, &block)
10
+ require 'prawn' unless defined? ::Prawn
11
+ options, template = template, nil if template.is_a?(Hash)
12
+ template = lambda { block } if template.nil?
13
+ options[:layout] = false
14
+ options[:options] ||= self.class.prawn if self.class.respond_to? :prawn
15
+ render :prawn, template, options
16
+ end
17
+
18
+ protected
19
+ def render_prawn(template, data, options, &block)
20
+ pdf = ::Prawn::Document.new(options[:options] || {})
21
+ if data.respond_to?(:to_str)
22
+ eval data.to_str, binding, '<PRAWN>', 1
23
+ elsif data.kind_of?(Proc)
24
+ data.call(pdf)
25
+ end
26
+ pdf.render
27
+ end
28
+ end
29
+
30
+ helpers Prawn
31
+ end
@@ -0,0 +1,51 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class SinatraPrawnTest < Test::Unit::TestCase
4
+ include Sinatra::Test
5
+
6
+ def prawn_app(&block)
7
+ mock_app {
8
+ helpers Sinatra::Prawn
9
+ set :views, File.dirname(__FILE__) + '/views'
10
+ get '/', &block
11
+ }
12
+ get '/'
13
+ end
14
+
15
+ def test_renders_inline_strings
16
+ prawn_app { prawn 'pdf.text "Hello shrimp!"' }
17
+ assert ok?
18
+ text = PDF::TextInspector.analyze(body)
19
+ assert_equal "Hello shrimp!", text.strings.first
20
+ end
21
+
22
+ def test_renders_inline_blocks
23
+ prawn_app {
24
+ @name = "Frank & Mary"
25
+ prawn do |pdf|
26
+ pdf.text "Hello #{@name}!"
27
+ end
28
+ }
29
+ assert ok?
30
+ text = PDF::TextInspector.analyze(body)
31
+ assert_equal "Hello Frank & Mary!", text.strings.first
32
+ end
33
+
34
+ def test_renders_prawn_files_in_views_path
35
+ prawn_app {
36
+ @name = "World"
37
+ prawn :hello
38
+ }
39
+ assert ok?
40
+ text = PDF::TextInspector.analyze(body)
41
+ assert_equal "Hello, World!", text.strings.first
42
+ end
43
+
44
+ def test_raises_error_if_template_not_found
45
+ mock_app {
46
+ helpers Sinatra::Prawn
47
+ get('/') { prawn :no_such_template }
48
+ }
49
+ assert_raise(Errno::ENOENT) { get('/') }
50
+ end
51
+ end
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'sinatra/test'
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ require 'sinatra/prawn'
7
+
8
+ require "pdf/reader"
9
+
10
+ module PDF
11
+ class TextInspector
12
+ attr_accessor :font_settings, :size, :strings
13
+
14
+ def initialize
15
+ @font_settings = []
16
+ @fonts = {}
17
+ @strings = []
18
+ end
19
+
20
+ def resource_font(*params)
21
+ @fonts[params[0]] = params[1].basefont
22
+ end
23
+
24
+ def set_text_font_and_size(*params)
25
+ @font_settings << { :name => @fonts[params[0]], :size => params[1] }
26
+ end
27
+
28
+ def show_text(*params)
29
+ @strings << params[0]
30
+ end
31
+
32
+ def show_text_with_positioning(*params)
33
+ # ignore kerning information
34
+ @strings << params[0].reject { |e| Numeric === e }.join
35
+ end
36
+
37
+ def self.analyze(output,*args,&block)
38
+ obs = self.new(*args, &block)
39
+ PDF::Reader.string(output,obs)
40
+ obs
41
+ end
42
+
43
+ def self.analyze_file(filename,*args,&block)
44
+ analyze(File.open(filename, "rb") { |f| f.read },*args,&block)
45
+ end
46
+
47
+ def self.parse(obj)
48
+ PDF::Reader::Parser.new(
49
+ PDF::Reader::Buffer.new(StringIO.new(obj)), nil).parse_token
50
+ end
51
+ end
52
+ end
53
+
54
+ class Test::Unit::TestCase
55
+ include Sinatra::Test
56
+
57
+ # Sets up a Sinatra::Base subclass defined with the block
58
+ # given. Used in setup or individual spec methods to establish
59
+ # the application.
60
+ def mock_app(base=Sinatra::Base, &block)
61
+ @app = Sinatra.new(base, &block)
62
+ end
63
+ end
@@ -0,0 +1 @@
1
+ pdf.text "Hello, #{@name}!"
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sbfaulkner-sinatra-prawn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
+ platform: ruby
6
+ authors:
7
+ - S. Brent Faulkner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: prawn
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Sinatra extension to add support for pdf rendering with Prawn templates.
26
+ email: brentf@unwwwired.net
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README.rdoc
35
+ - VERSION.yml
36
+ - lib/sinatra
37
+ - lib/sinatra/prawn.rb
38
+ - test/sinatra_prawn_test.rb
39
+ - test/test_helper.rb
40
+ - test/views
41
+ - test/views/hello.prawn
42
+ has_rdoc: true
43
+ homepage: http://github.com/sbfaulkner/sinatra-prawn
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --inline-source
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.2.0
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: Sinatra extension to add support for pdf rendering with Prawn templates.
69
+ test_files: []
70
+