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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0d6fbefb65824997d33b8548ae130748091503ae
4
- data.tar.gz: 5a06022952a399839b9d5e791a16ec2285f28e5c
3
+ metadata.gz: 8087ba0ba0e0bfcbc51bfbc5e05c9847dcf65869
4
+ data.tar.gz: 067355f7b11a01986b8ce5f5948598bc3e4536ae
5
5
  SHA512:
6
- metadata.gz: b8dc1640268c8aaad75b44e0dfb5a4ea041decc3ca4e799b9306040645e158a9fea97053a78ed996dd9d23b25610a990eae49df26205da5e934e19094e876d4f
7
- data.tar.gz: b3ffd4b8dd3154d878e80bda3cfcb226722b3b5bcbab332cbc6d7bcd38cca7df8556796d0c26c553f72d6653d13087e0a708f6fa82bdf1c339894d0ed70c4dc6
6
+ metadata.gz: d62d8ecf3aed1aa63c100b2d6047a18c913d9fa323dbb8c63eb57c56f180c5673ce0bbdac141f8188fd491897dfe31ba8be5f2c6ac542764131477bb1b86e4b5
7
+ data.tar.gz: 2a0ca3e83f4ab55dfa41487b3fd263c15a77c66679b649ac5b97bb062ba0031f3522f6796cd582082ee782df995dbd03c3a802e799415b942ea9206cd75272ba
@@ -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 = :landscape
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**
@@ -3,7 +3,9 @@ require 'ostruct'
3
3
  module PrawnRails
4
4
  extend self
5
5
 
6
- @config = OpenStruct.new
6
+ @config = OpenStruct.new(:page_layout => :portrait,
7
+ :page_size => "A4",
8
+ :skip_page_creation => false)
7
9
 
8
10
  def config
9
11
  begin
@@ -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
@@ -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(/(&nbsp;|\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
@@ -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
- ::Prawn::Document.extensions << PrawnRailsHelper
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
@@ -1,3 +1,3 @@
1
1
  module PrawnRails
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,34 +1,36 @@
1
- pdf.text "Hello There :D"
2
- pdf.text "now it is here: #{pdf.cursor}"
3
- pdf.text "ITems are #{@items.inspect}"
4
- pdf.font("Courier") do
5
- pdf.text "Written in Courier because we are inside the block."
6
- end
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
- pdf.text "Yeah, using Courier 10 courtesy of the font method."
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
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'pdf/reader'
2
3
 
3
4
  class PdfControllerTest < ActionController::TestCase
4
5
  test "should get a pdf" do
@@ -1,8 +1,8 @@
1
1
  require 'test_helper'
2
- require "prawn-rails/prawn_rails_helper"
2
+ require "prawn-rails/extension"
3
3
 
4
4
  class PrawnRailsTest < ActiveSupport::TestCase
5
- include PrawnRailsHelper
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.0
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: 2014-10-07 00:00:00.000000000 Z
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/prawn_rails_helper.rb
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.2.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(/(&nbsp;|\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