prawn-rails 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +26 -8
- data/lib/prawn-rails/config.rb +3 -1
- data/lib/prawn-rails/document.rb +8 -0
- data/lib/prawn-rails/engine.rb +2 -0
- data/lib/prawn-rails/extension.rb +43 -0
- data/lib/prawn-rails/rails_helper.rb +18 -0
- data/lib/prawn-rails/renderer.rb +1 -9
- data/lib/prawn-rails/version.rb +1 -1
- data/test/dummy/app/views/pdf/render_pdf.pdf.prawn +34 -32
- data/test/dummy/test/functional/pdf_controller_test.rb +1 -0
- data/test/prawn-rails_test.rb +2 -2
- metadata +19 -4
- data/lib/prawn-rails/prawn_rails_helper.rb +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8087ba0ba0e0bfcbc51bfbc5e05c9847dcf65869
|
4
|
+
data.tar.gz: 067355f7b11a01986b8ce5f5948598bc3e4536ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d62d8ecf3aed1aa63c100b2d6047a18c913d9fa323dbb8c63eb57c56f180c5673ce0bbdac141f8188fd491897dfe31ba8be5f2c6ac542764131477bb1b86e4b5
|
7
|
+
data.tar.gz: 2a0ca3e83f4ab55dfa41487b3fd263c15a77c66679b649ac5b97bb062ba0031f3522f6796cd582082ee782df995dbd03c3a802e799415b942ea9206cd75272ba
|
data/README.markdown
CHANGED
@@ -1,25 +1,35 @@
|
|
1
|
-
# Prawn-Rails
|
1
|
+
# Prawn-Rails [![Gem Version](https://badge.fury.io/rb/prawn-rails.svg)](http://badge.fury.io/rb/prawn-rails)
|
2
2
|
|
3
3
|
## Dependencies
|
4
4
|
|
5
5
|
* prawn > 0.0.12
|
6
|
+
* prawn-table
|
6
7
|
* Rails 3.0x
|
7
8
|
* Ruby > 1.8.7
|
8
9
|
|
9
10
|
## Install
|
10
11
|
1.Add to the Rails Gemfile
|
11
12
|
|
12
|
-
gem 'prawn'
|
13
|
-
gem 'prawn-table'
|
14
13
|
gem 'prawn-rails'
|
15
14
|
|
16
|
-
to the Rails Gemfile
|
17
|
-
|
15
|
+
to the Rails Gemfile
|
16
|
+
`prawn` and `prawn-table` is a dependency so no need to mention it in the projects Gemfile
|
17
|
+
but can mention a specific version if your Gemfile, if you want
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
Create a view with `pdf` as format and `prawn` as handler
|
21
|
-
should look like `example.pdf.prawn`
|
22
|
-
|
21
|
+
so filename should look like `example.pdf.prawn`
|
22
|
+
|
23
|
+
we provide a helper called `prawn_document`
|
24
|
+
it builds a PrawnRails::Document with default options. Can override with `page_size` and `page_layout`
|
25
|
+
example contents of `example.pdf.prawn`
|
26
|
+
|
27
|
+
prawn_document(:page_layout => :landscape) do |pdf|
|
28
|
+
pdf.text "Hello World"
|
29
|
+
end
|
30
|
+
|
31
|
+
No need to call `pdf.render`, it is called by `prawn_document`
|
32
|
+
|
23
33
|
Your available to use all prawn document methods like `pdf.text` `pdf.font_size` and also
|
24
34
|
block like `pdf.font(FONT_NAME,opts) do
|
25
35
|
pdf.XXXX
|
@@ -36,9 +46,17 @@ Removes the html tags from a string
|
|
36
46
|
Add a `prawn-rails.rb` config to your Rails app under `config/initializers` like this
|
37
47
|
|
38
48
|
PrawnRails.config do |config|
|
39
|
-
config.page_layout = :
|
49
|
+
config.page_layout = :portrait
|
50
|
+
config.page_size = "A4"
|
51
|
+
config.skip_page_creation = false
|
40
52
|
end
|
41
53
|
|
54
|
+
by default `page_layout` is portrait and `page_size` is "A4"
|
55
|
+
also `skip_page_creation` is set to false by default, if it is set to true
|
56
|
+
then have to create the first page yourself for eg.
|
57
|
+
|
58
|
+
pdf.start_new_page size: "A4", page_layout: :landscape
|
59
|
+
|
42
60
|
## Examples
|
43
61
|
|
44
62
|
1. **Hello World**
|
data/lib/prawn-rails/config.rb
CHANGED
data/lib/prawn-rails/document.rb
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
require 'prawn'
|
2
2
|
require 'prawn/table'
|
3
|
+
require "prawn-rails/extension"
|
3
4
|
|
4
5
|
module PrawnRails
|
5
6
|
# This derives from Prawn::Document in order to override defaults. Note
|
6
7
|
# that the Prawn::Document behaviour itself shouldn't be changed.
|
7
8
|
class Document < Prawn::Document
|
9
|
+
def initialize(opts = {})
|
10
|
+
default = PrawnRails.config.to_h.merge(opts)
|
11
|
+
super(default)
|
12
|
+
end
|
13
|
+
|
8
14
|
# Typicall text expects a string. But rails views have this interesting
|
9
15
|
# concept that they implicitly call `to_s` on all the variables before
|
10
16
|
# rendering. So, passing an integer to text fails:
|
@@ -18,4 +24,6 @@ module PrawnRails
|
|
18
24
|
super(value.to_s, options)
|
19
25
|
end
|
20
26
|
end
|
27
|
+
|
28
|
+
Document.extensions << Extension
|
21
29
|
end
|
data/lib/prawn-rails/engine.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
require "prawn"
|
2
|
+
require "prawn-rails/rails_helper"
|
2
3
|
require "prawn-rails/renderer"
|
3
4
|
|
4
5
|
module PrawnRails
|
5
6
|
class Engine < Rails::Engine
|
7
|
+
ActionView::Base.send(:include, PrawnRails::RailsHelper)
|
6
8
|
ActionView::Template.register_template_handler(:prawn, PrawnRails::Renderer)
|
7
9
|
|
8
10
|
if !Mime::Type.lookup_by_extension(:pdf)
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module PrawnRails
|
2
|
+
module Extension
|
3
|
+
##
|
4
|
+
# Removes all html tags from the string
|
5
|
+
# @param html[String] string to remove the tags
|
6
|
+
# @return [String] the string with out the tags
|
7
|
+
##
|
8
|
+
def html_strip html
|
9
|
+
return html if html.nil?
|
10
|
+
html
|
11
|
+
text = html.
|
12
|
+
gsub(/( |\n|\s)+/im, ' ').squeeze(' ').strip.
|
13
|
+
gsub(/<([^\s]+)[^>]*(src|href)=\s*(.?)([^>\s]*)\3[^>]*>\4<\/\1>/i,
|
14
|
+
'\4')
|
15
|
+
|
16
|
+
links = []
|
17
|
+
linkregex = /<[^>]*(src|href)=\s*(.?)([^>\s]*)\2[^>]*>\s*/i
|
18
|
+
while linkregex.match(text)
|
19
|
+
links << $~[3]
|
20
|
+
text.sub!(linkregex, "[#{links.size}]")
|
21
|
+
end
|
22
|
+
|
23
|
+
text = CGI.unescapeHTML(
|
24
|
+
text.
|
25
|
+
gsub(/<(script|style)[^>]*>.*<\/\1>/im, '').
|
26
|
+
gsub(/<!--.*-->/m, '').
|
27
|
+
gsub(/<hr(| [^>]*)>/i, "___\n").
|
28
|
+
gsub(/<li(| [^>]*)>/i, "\n* ").
|
29
|
+
gsub(/<blockquote(| [^>]*)>/i, '> ').
|
30
|
+
gsub(/<(br)(| [^>]*)>/i, "\n").
|
31
|
+
gsub(/<(\/h[\d]+|p)(| [^>]*)>/i, "\n\n").
|
32
|
+
gsub(/<[^>]*>/, '')
|
33
|
+
).lstrip.gsub(/\n[ ]+/, "\n") + "\n"
|
34
|
+
|
35
|
+
for i in (0...links.size).to_a
|
36
|
+
text = text + "\n [#{i+1}] <#{CGI.unescapeHTML(links[i])}>" unless
|
37
|
+
links[i].nil?
|
38
|
+
end
|
39
|
+
links = nil
|
40
|
+
text
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'prawn-rails/document'
|
2
|
+
|
3
|
+
module PrawnRails
|
4
|
+
module RailsHelper
|
5
|
+
def prawn_document(options={})
|
6
|
+
options.reverse_merge!(:page_layout => PrawnRails.config.page_layout,
|
7
|
+
:page_size => PrawnRails.config.page_size)
|
8
|
+
|
9
|
+
options.reverse_merge!(:skip_page_creation => true) if PrawnRails.config.skip_page_creation
|
10
|
+
|
11
|
+
pdf = PrawnRails::Document.new(options)
|
12
|
+
|
13
|
+
yield pdf if block_given?
|
14
|
+
|
15
|
+
pdf.render
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/prawn-rails/renderer.rb
CHANGED
@@ -1,17 +1,9 @@
|
|
1
1
|
require "prawn-rails/document"
|
2
|
-
require "prawn-rails/prawn_rails_helper"
|
3
2
|
|
4
3
|
module PrawnRails
|
5
4
|
class Renderer
|
6
5
|
def self.call(template)
|
7
|
-
|
8
|
-
new.call(template)
|
9
|
-
end
|
10
|
-
|
11
|
-
def call(template)
|
12
|
-
"pdf = PrawnRails::Document.new;" +
|
13
|
-
template.source +
|
14
|
-
";self.output_buffer=pdf.render;"
|
6
|
+
template.source.strip
|
15
7
|
end
|
16
8
|
end
|
17
9
|
end
|
data/lib/prawn-rails/version.rb
CHANGED
@@ -1,34 +1,36 @@
|
|
1
|
-
|
2
|
-
pdf.text "
|
3
|
-
pdf.text "
|
4
|
-
pdf.
|
5
|
-
pdf.
|
6
|
-
|
1
|
+
prawn_document do |pdf|
|
2
|
+
pdf.text "Hello There :D"
|
3
|
+
pdf.text "now it is here: #{pdf.cursor}"
|
4
|
+
pdf.text "ITems are #{@items.inspect}"
|
5
|
+
pdf.font("Courier") do
|
6
|
+
pdf.text "Written in Courier because we are inside the block."
|
7
|
+
end
|
7
8
|
|
8
|
-
pdf.text "with html<bold></bold>"
|
9
|
-
pdf.text pdf.html_strip("without html <bold>LD</bold>")
|
10
|
-
pdf.text "Let's see which is the current font_size: #{pdf.font_size.inspect}"
|
11
|
-
pdf.move_down 10
|
12
|
-
pdf.font_size 16
|
13
|
-
pdf.text "Yeah, something bigger!"
|
14
|
-
pdf.move_down 10
|
15
|
-
pdf.font_size(25) { pdf.text "Even bigger!" }
|
16
|
-
pdf.move_down 10
|
17
|
-
pdf.text "Back to 16 again."
|
18
|
-
pdf.move_down 10
|
19
|
-
pdf.text "Single line on 20 using the :size option.", :size => 20
|
20
|
-
pdf.move_down 10
|
21
|
-
pdf.text "Back to 16 once more."
|
22
|
-
pdf.move_down 10
|
23
|
-
pdf.font("Courier", :size => 10) do
|
24
|
-
|
25
|
-
end
|
26
|
-
pdf.font("Helvetica", :size => 12) # back to normal
|
9
|
+
pdf.text "with html<bold></bold>"
|
10
|
+
pdf.text pdf.html_strip("without html <bold>LD</bold>")
|
11
|
+
pdf.text "Let's see which is the current font_size: #{pdf.font_size.inspect}"
|
12
|
+
pdf.move_down 10
|
13
|
+
pdf.font_size 16
|
14
|
+
pdf.text "Yeah, something bigger!"
|
15
|
+
pdf.move_down 10
|
16
|
+
pdf.font_size(25) { pdf.text "Even bigger!" }
|
17
|
+
pdf.move_down 10
|
18
|
+
pdf.text "Back to 16 again."
|
19
|
+
pdf.move_down 10
|
20
|
+
pdf.text "Single line on 20 using the :size option.", :size => 20
|
21
|
+
pdf.move_down 10
|
22
|
+
pdf.text "Back to 16 once more."
|
23
|
+
pdf.move_down 10
|
24
|
+
pdf.font("Courier", :size => 10) do
|
25
|
+
pdf.text "Yeah, using Courier 10 courtesy of the font method."
|
26
|
+
end
|
27
|
+
pdf.font("Helvetica", :size => 12) # back to normal
|
27
28
|
|
28
|
-
pdf.text "Default color is black"
|
29
|
-
pdf.move_down 25
|
30
|
-
pdf.text "Changed to red", :color => "FF0000"
|
31
|
-
pdf.move_down 25
|
32
|
-
pdf.text "CMYK color", :color => [22, 55, 79, 30]
|
33
|
-
pdf.move_down 25
|
34
|
-
pdf.text "Also works with <color rgb='ff0000'>inline</color> formatting", :color => "0000FF", :inline_format => true
|
29
|
+
pdf.text "Default color is black"
|
30
|
+
pdf.move_down 25
|
31
|
+
pdf.text "Changed to red", :color => "FF0000"
|
32
|
+
pdf.move_down 25
|
33
|
+
pdf.text "CMYK color", :color => [22, 55, 79, 30]
|
34
|
+
pdf.move_down 25
|
35
|
+
pdf.text "Also works with <color rgb='ff0000'>inline</color> formatting", :color => "0000FF", :inline_format => true
|
36
|
+
end
|
data/test/prawn-rails_test.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
require "prawn-rails/
|
2
|
+
require "prawn-rails/extension"
|
3
3
|
|
4
4
|
class PrawnRailsTest < ActiveSupport::TestCase
|
5
|
-
include
|
5
|
+
include PrawnRails::Extension
|
6
6
|
test "html_strip" do
|
7
7
|
|
8
8
|
assert_nil(html_strip(nil))
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prawn-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Carlos Ortiz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: prawn
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pdf-reader
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: Prawn Handler for RoR 3.x/4.x projects handles and registers pdf formats
|
70
84
|
email:
|
71
85
|
- chrono.dark@gmail.com
|
@@ -80,7 +94,8 @@ files:
|
|
80
94
|
- lib/prawn-rails/config.rb
|
81
95
|
- lib/prawn-rails/document.rb
|
82
96
|
- lib/prawn-rails/engine.rb
|
83
|
-
- lib/prawn-rails/
|
97
|
+
- lib/prawn-rails/extension.rb
|
98
|
+
- lib/prawn-rails/rails_helper.rb
|
84
99
|
- lib/prawn-rails/renderer.rb
|
85
100
|
- lib/prawn-rails/version.rb
|
86
101
|
- lib/tasks/prawn-rails_tasks.rake
|
@@ -148,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
163
|
version: '0'
|
149
164
|
requirements: []
|
150
165
|
rubyforge_project:
|
151
|
-
rubygems_version: 2.
|
166
|
+
rubygems_version: 2.4.5
|
152
167
|
signing_key:
|
153
168
|
specification_version: 4
|
154
169
|
summary: Prawn Handler for RoR 3.x/4.x projects
|
@@ -1,41 +0,0 @@
|
|
1
|
-
module PrawnRailsHelper
|
2
|
-
##
|
3
|
-
# Removes all html tags from the string
|
4
|
-
# @param html[String] string to remove the tags
|
5
|
-
# @return [String] the string with out the tags
|
6
|
-
##
|
7
|
-
def html_strip html
|
8
|
-
return html if html.nil?
|
9
|
-
html
|
10
|
-
text = html.
|
11
|
-
gsub(/( |\n|\s)+/im, ' ').squeeze(' ').strip.
|
12
|
-
gsub(/<([^\s]+)[^>]*(src|href)=\s*(.?)([^>\s]*)\3[^>]*>\4<\/\1>/i,
|
13
|
-
'\4')
|
14
|
-
|
15
|
-
links = []
|
16
|
-
linkregex = /<[^>]*(src|href)=\s*(.?)([^>\s]*)\2[^>]*>\s*/i
|
17
|
-
while linkregex.match(text)
|
18
|
-
links << $~[3]
|
19
|
-
text.sub!(linkregex, "[#{links.size}]")
|
20
|
-
end
|
21
|
-
|
22
|
-
text = CGI.unescapeHTML(
|
23
|
-
text.
|
24
|
-
gsub(/<(script|style)[^>]*>.*<\/\1>/im, '').
|
25
|
-
gsub(/<!--.*-->/m, '').
|
26
|
-
gsub(/<hr(| [^>]*)>/i, "___\n").
|
27
|
-
gsub(/<li(| [^>]*)>/i, "\n* ").
|
28
|
-
gsub(/<blockquote(| [^>]*)>/i, '> ').
|
29
|
-
gsub(/<(br)(| [^>]*)>/i, "\n").
|
30
|
-
gsub(/<(\/h[\d]+|p)(| [^>]*)>/i, "\n\n").
|
31
|
-
gsub(/<[^>]*>/, '')
|
32
|
-
).lstrip.gsub(/\n[ ]+/, "\n") + "\n"
|
33
|
-
|
34
|
-
for i in (0...links.size).to_a
|
35
|
-
text = text + "\n [#{i+1}] <#{CGI.unescapeHTML(links[i])}>" unless
|
36
|
-
links[i].nil?
|
37
|
-
end
|
38
|
-
links = nil
|
39
|
-
text
|
40
|
-
end
|
41
|
-
end
|