dberkom-sinatra-prawn 0.9.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +43 -0
- data/Rakefile +45 -0
- data/VERSION.yml +4 -0
- data/lib/sinatra/prawn.rb +54 -0
- data/test/sinatra_prawn_test.rb +51 -0
- data/test/test_helper.rb +65 -0
- data/test/views/hello.prawn +1 -0
- metadata +74 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 unwwwired.net
|
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,43 @@
|
|
1
|
+
= sinatra-prawn
|
2
|
+
|
3
|
+
sinatra-prawn is an extension for sinatra to enable rendering of pdf files
|
4
|
+
using prawn templates.
|
5
|
+
|
6
|
+
== Installation
|
7
|
+
|
8
|
+
sudo gem install sbfaulkner-sinatra-prawn -s http://gems.github.com
|
9
|
+
|
10
|
+
== Installation from bundler
|
11
|
+
|
12
|
+
add these lines to your project Gemfile
|
13
|
+
|
14
|
+
source "http://gems.github.com"
|
15
|
+
gem "sbfaulkner-sinatra-prawn"
|
16
|
+
|
17
|
+
and run
|
18
|
+
|
19
|
+
bundle install
|
20
|
+
|
21
|
+
|
22
|
+
== Example
|
23
|
+
|
24
|
+
require 'rubygems'
|
25
|
+
require 'sinatra'
|
26
|
+
require 'sinatra/prawn'
|
27
|
+
|
28
|
+
set :prawn, { :page_layout => :landscape }
|
29
|
+
|
30
|
+
get '/' do
|
31
|
+
content_type 'application/pdf'
|
32
|
+
prawn :pdf
|
33
|
+
end
|
34
|
+
|
35
|
+
__END__
|
36
|
+
|
37
|
+
@@ pdf
|
38
|
+
pdf.text "Hello world!!!!!"
|
39
|
+
|
40
|
+
== Legal
|
41
|
+
|
42
|
+
Author:: S. Brent Faulkner <brentf@unwwwired.net>
|
43
|
+
License:: Copyright (c) 2009 unwwwired.net, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |s|
|
6
|
+
s.name = "sinatra-prawn"
|
7
|
+
s.summary = %Q{Sinatra extension to add support for pdf rendering with Prawn templates.}
|
8
|
+
s.email = "brentf@unwwwired.net"
|
9
|
+
s.homepage = "http://github.com/sbfaulkner/sinatra-prawn"
|
10
|
+
s.description = "Sinatra extension to add support for pdf rendering with Prawn templates."
|
11
|
+
s.authors = ["S. Brent Faulkner"]
|
12
|
+
s.add_dependency "prawn"
|
13
|
+
end
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'rake/rdoctask'
|
19
|
+
Rake::RDocTask.new do |rdoc|
|
20
|
+
rdoc.rdoc_dir = 'rdoc'
|
21
|
+
rdoc.title = 'sinatra-prawn'
|
22
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
23
|
+
rdoc.rdoc_files.include('README*')
|
24
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
Rake::TestTask.new(:test) do |t|
|
29
|
+
t.libs << 'lib' << 'test'
|
30
|
+
t.pattern = 'test/**/*_test.rb'
|
31
|
+
t.verbose = false
|
32
|
+
end
|
33
|
+
|
34
|
+
begin
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |t|
|
37
|
+
t.libs << 'test'
|
38
|
+
t.test_files = FileList['test/**/*_test.rb']
|
39
|
+
t.verbose = true
|
40
|
+
end
|
41
|
+
rescue LoadError
|
42
|
+
puts "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
43
|
+
end
|
44
|
+
|
45
|
+
task :default => :test
|
data/VERSION.yml
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
module Prawn
|
5
|
+
module Helpers
|
6
|
+
# Generate pdf file using Prawn.
|
7
|
+
# Takes the name of a template to render as a Symbol and returns a String with the rendered output.
|
8
|
+
#
|
9
|
+
# Options for prawn may be specified in Sinatra using set :prawn, { ... }
|
10
|
+
def prawn(template=nil, options={}, locals = {}, &block)
|
11
|
+
options, template = template, nil if template.is_a?(Hash)
|
12
|
+
template = lambda { block } if template.nil?
|
13
|
+
options[:layout] = false
|
14
|
+
render :prawn, template, options, locals
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.registered(app)
|
19
|
+
app.helpers Prawn::Helpers
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
register Prawn
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
module Tilt
|
28
|
+
class PrawnTemplate < Template
|
29
|
+
def initialize_engine
|
30
|
+
return if defined? ::Prawn::Document
|
31
|
+
require_template_library 'prawn'
|
32
|
+
require_template_library 'prawn/layout'
|
33
|
+
end
|
34
|
+
|
35
|
+
def prepare
|
36
|
+
end
|
37
|
+
|
38
|
+
def evaluate(scope, locals, &block)
|
39
|
+
pdf = ::Prawn::Document.new
|
40
|
+
if data.respond_to?(:to_str)
|
41
|
+
locals[:pdf] = pdf
|
42
|
+
super(scope, locals, &block)
|
43
|
+
elsif data.kind_of?(Proc)
|
44
|
+
data.call(pdf)
|
45
|
+
end
|
46
|
+
pdf.render
|
47
|
+
end
|
48
|
+
|
49
|
+
def precompiled_template(locals)
|
50
|
+
data.to_str
|
51
|
+
end
|
52
|
+
end
|
53
|
+
register 'prawn', PrawnTemplate
|
54
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class SinatraPrawnTest < Test::Unit::TestCase
|
4
|
+
def prawn_app(&block)
|
5
|
+
mock_app {
|
6
|
+
helpers Sinatra::Prawn
|
7
|
+
set :views, File.dirname(__FILE__) + '/views'
|
8
|
+
get '/', &block
|
9
|
+
}
|
10
|
+
get '/'
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_renders_inline_strings
|
14
|
+
prawn_app { prawn 'pdf.text "Hello shrimp!"' }
|
15
|
+
assert last_response.ok?
|
16
|
+
text = PDF::TextInspector.analyze(last_response.body)
|
17
|
+
assert_equal "Hello shrimp!", text.strings.first
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_renders_inline_blocks
|
21
|
+
prawn_app {
|
22
|
+
@name = "Frank & Mary"
|
23
|
+
prawn do |pdf|
|
24
|
+
pdf.text "Hello #{@name}!"
|
25
|
+
end
|
26
|
+
}
|
27
|
+
assert last_response.ok?
|
28
|
+
text = PDF::TextInspector.analyze(last_response.body)
|
29
|
+
assert_equal "Hello Frank & Mary!", text.strings.first
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_renders_prawn_files_in_views_path
|
33
|
+
prawn_app {
|
34
|
+
@name = "World"
|
35
|
+
prawn :hello
|
36
|
+
}
|
37
|
+
assert last_response.ok?
|
38
|
+
text = PDF::TextInspector.analyze(last_response.body)
|
39
|
+
assert_equal "Hello, World!", text.strings.first
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_raises_error_if_template_not_found
|
43
|
+
mock_app {
|
44
|
+
helpers Sinatra::Prawn
|
45
|
+
set :environment, :test
|
46
|
+
set :raise_errors, true
|
47
|
+
get('/') { prawn :no_such_template }
|
48
|
+
}
|
49
|
+
assert_raises(Errno::ENOENT) { get('/') }
|
50
|
+
end
|
51
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'rack/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 Rack::Test::Methods
|
56
|
+
|
57
|
+
attr_reader :app
|
58
|
+
|
59
|
+
# Sets up a Sinatra::Base subclass defined with the block
|
60
|
+
# given. Used in setup or individual spec methods to establish
|
61
|
+
# the application.
|
62
|
+
def mock_app(base=Sinatra::Base, &block)
|
63
|
+
@app = Sinatra.new(base, &block)
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
pdf.text "Hello, #{@name}!"
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dberkom-sinatra-prawn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.2.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- S. Brent Faulkner
|
9
|
+
- Daniel Berkompas
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-04-03 00:00:00.000000000Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: prawn
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
description: Sinatra extension to add support for pdf rendering with Prawn templates.
|
32
|
+
email: brentf@unwwwired.net daniel@clearsightstudio.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files:
|
36
|
+
- LICENSE
|
37
|
+
- README.rdoc
|
38
|
+
files:
|
39
|
+
- LICENSE
|
40
|
+
- README.rdoc
|
41
|
+
- Rakefile
|
42
|
+
- VERSION.yml
|
43
|
+
- lib/sinatra/prawn.rb
|
44
|
+
- test/sinatra_prawn_test.rb
|
45
|
+
- test/test_helper.rb
|
46
|
+
- test/views/hello.prawn
|
47
|
+
homepage: http://github.com/danielberkompas/sinatra-prawn
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --charset=UTF-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.19
|
69
|
+
signing_key:
|
70
|
+
specification_version: 2
|
71
|
+
summary: Sinatra extension to add support for pdf rendering with Prawn templates.
|
72
|
+
test_files:
|
73
|
+
- test/sinatra_prawn_test.rb
|
74
|
+
- test/test_helper.rb
|