tilt-pdf 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 417c8016b155b872083883cfe1ad4d2398cdf4d5
4
+ data.tar.gz: 1f89a38c892af318dc4fe74ae494904c3a69390f
5
+ SHA512:
6
+ metadata.gz: d38f611ba62114a0a0ea785f76a67db742e8b8daad5aad12f1de531f06829a0c137e6f6ed076442e484e9bcb1a735cb81a3be3104ccfe72dc19a4605ba8c0c90
7
+ data.tar.gz: da7cb5aa90eaace59ca76f6ae17117639cf869b782707c5b377191aaf14fd3cb84e2f4a26dbef5c6eaf871723a1880e36860cfcd413252ba828af0cddb91acf8
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ /test/*.pdf
data/.rubocop.yml ADDED
@@ -0,0 +1,15 @@
1
+ AsciiComments:
2
+ Enabled: false
3
+ MethodLength:
4
+ Max: 30
5
+ ParameterLists:
6
+ CountKeywordArgs: false
7
+ Documentation:
8
+ Enabled: false
9
+ TrivialAccessors:
10
+ ExactNameMatch: true
11
+ AllowPredicates: true
12
+ AndOr:
13
+ Enabled: false
14
+ PerlBackrefs:
15
+ Enabled: false
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tilt-pdf (0.1.0)
5
+ pdfkit (~> 0.5.4)
6
+ tilt (~> 1.4.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ celluloid (0.15.2)
12
+ timers (~> 1.1.0)
13
+ coderay (1.0.9)
14
+ commonjs (0.2.7)
15
+ diff-lcs (1.2.4)
16
+ ffi (1.9.0)
17
+ formatador (0.2.4)
18
+ guard (2.1.1)
19
+ formatador (>= 0.2.4)
20
+ listen (~> 2.1)
21
+ lumberjack (~> 1.0)
22
+ pry (>= 0.9.12)
23
+ thor (>= 0.18.1)
24
+ guard-rspec (4.0.3)
25
+ guard (>= 2.1.1)
26
+ rspec (~> 2.14)
27
+ less (2.4.0)
28
+ commonjs (~> 0.2.7)
29
+ libv8 (3.16.14.3)
30
+ listen (2.1.1)
31
+ celluloid (>= 0.15.2)
32
+ rb-fsevent (>= 0.9.3)
33
+ rb-inotify (>= 0.9)
34
+ lumberjack (1.0.4)
35
+ method_source (0.8.2)
36
+ pdfkit (0.5.4)
37
+ pry (0.9.12.2)
38
+ coderay (~> 1.0.5)
39
+ method_source (~> 0.8)
40
+ slop (~> 3.4)
41
+ rake (10.1.0)
42
+ rb-fsevent (0.9.3)
43
+ rb-inotify (0.9.2)
44
+ ffi (>= 0.5.0)
45
+ ref (1.0.5)
46
+ rspec (2.14.1)
47
+ rspec-core (~> 2.14.0)
48
+ rspec-expectations (~> 2.14.0)
49
+ rspec-mocks (~> 2.14.0)
50
+ rspec-core (2.14.6)
51
+ rspec-expectations (2.14.3)
52
+ diff-lcs (>= 1.1.3, < 2.0)
53
+ rspec-mocks (2.14.4)
54
+ slim (2.0.1)
55
+ temple (~> 0.6.6)
56
+ tilt (>= 1.3.3, < 2.1)
57
+ slop (3.4.6)
58
+ temple (0.6.7)
59
+ therubyracer (0.12.0)
60
+ libv8 (~> 3.16.14.0)
61
+ ref
62
+ thor (0.18.1)
63
+ tilt (1.4.1)
64
+ timers (1.1.0)
65
+
66
+ PLATFORMS
67
+ ruby
68
+
69
+ DEPENDENCIES
70
+ guard-rspec
71
+ less
72
+ pry
73
+ rake
74
+ rspec (~> 2.14)
75
+ slim
76
+ therubyracer
77
+ tilt-pdf!
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Loic Nageleisen
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.mdown ADDED
@@ -0,0 +1,37 @@
1
+ # Tilt::PDF
2
+
3
+ Integrates PDF generation into a Tilt flow
4
+
5
+ ## Dependencies
6
+
7
+ This gem depends on PDFKit, which in turn requires `wkhtmltopdf`. It is recommended to use the statically compiled version of the latter, as it is built against a patched QT that supports more features.
8
+
9
+ ## Usage as a Tilt template
10
+
11
+ Add `tilt-pdf` to your Gemfile. Also add any template engine you may optionally want, such as `less` or `slim`.
12
+
13
+ A `foo` template is currently threefold:
14
+
15
+ - `foo.rpdf`: this file is a YAML file containing options pertaining to the PDF generation process, such as page size, orientation and metadata. Options are apssed as is to PDFKit, and subsequently to `wkhtmltopdf`.
16
+ - `foo.html`: this document can be written in any template language you need (such as ERB or Slim), and the Tilt template resolution system via extension chaining will apply. Tilt will pass the render block to be yielded to this document.
17
+ - `foo.css`: this stylesheet can be written in any template language you need (such as Sass or Less), and the Tilt template resolution system via extension chaining will apply. Tilt will *not* pass the block to be yielded to this template.
18
+
19
+ The three files must currently be stored in the *same* directory.
20
+
21
+ Rendering is done the usual Tilt way:
22
+
23
+ ```ruby
24
+ require 'tilt-pdf'
25
+
26
+ pdf = Tilt.new('foo.rpdf').render()
27
+ ```
28
+
29
+ ## Rails and ActionView integration
30
+
31
+ Require `tilt/pdf/rails` if you want to set up and register `tilt-pdf` as an ActionView template handler. You can do it in an initializer, or straight from the Gemfile:
32
+
33
+ ```ruby
34
+ gem 'tilt-pdf', require: 'tilt/pdf/rails'
35
+ ```
36
+
37
+ Put your three template files *together* in the relevant `app/views/foo` view directory. Work is in progress to enable better integration with Rails file layout.
@@ -0,0 +1,25 @@
1
+ require 'tilt-pdf'
2
+ require 'action_view/template/handlers'
3
+
4
+ module ActionView
5
+ class Template
6
+ module Handlers
7
+ class PDFTemplate
8
+ class_attribute :default_format
9
+ self.default_format = :pdf
10
+
11
+ def call(template)
12
+ "Tilt.new('#{template.identifier}').render(self)"
13
+ end
14
+ end
15
+ end
16
+
17
+ register_template_handler :rpdf, Handlers::PDFTemplate.new
18
+ end
19
+ end
20
+
21
+ module Tilt::PDFTemplate::Rails
22
+ class Railtie < ::Rails::Railtie
23
+ config.app_generators.template_engine :rpdf
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ module Tilt
2
+ module PDF
3
+ VERSION = '0.1.0'
4
+
5
+ end
6
+ end
data/lib/tilt/pdf.rb ADDED
@@ -0,0 +1,88 @@
1
+ require 'pdfkit'
2
+ require 'tilt'
3
+ require 'tilt/template'
4
+
5
+ PDFKit.configure do |config|
6
+ config.wkhtmltopdf =
7
+ '/Applications/wkhtmltopdf.app/Contents/MacOS/wkhtmltopdf'
8
+ config.default_options = {
9
+ page_size: 'A4',
10
+ orientation: 'portrait',
11
+ print_media_type: true,
12
+ dpi: 600,
13
+ margin_top: 0,
14
+ margin_bottom: 0,
15
+ margin_left: 0,
16
+ margin_right: 0,
17
+ header_spacing: 0,
18
+ footer_spacing: 0,
19
+ disable_smart_shrinking: true,
20
+ zoom: 1.0,
21
+ }
22
+ end
23
+
24
+ module Tilt
25
+ class PDFTemplate < Template
26
+ self.default_mime_type = 'application/pdf'
27
+
28
+ def prepare; end
29
+
30
+ def evaluate(scope, locals, &block)
31
+ html_file = find_html
32
+ html = render_html(html_file, scope, locals, &block)
33
+
34
+ css_files = find_css
35
+ render_css(*css_files) do |*css|
36
+ kit = PDFKit.new(html, pdfkit_options)
37
+ css.each { |f| kit.stylesheets << f }
38
+ @output = kit.to_pdf
39
+ end
40
+
41
+ @output
42
+ end
43
+
44
+ private
45
+
46
+ def pdfkit_options
47
+ YAML.load(data) || {}
48
+ end
49
+
50
+ def dirname
51
+ eval_file.gsub(/#{basename}$/, '').chomp('/')
52
+ end
53
+
54
+ def find_html
55
+ Dir.glob(File.join(dirname, name + '.html*')).first
56
+ end
57
+
58
+ def find_css
59
+ Dir.glob(File.join(dirname, name + '.css*'))
60
+ end
61
+
62
+ def render_html(file, scope, locals, &block)
63
+ Tilt.new(file).render(scope, locals, &block)
64
+ end
65
+
66
+ def render_css(*files)
67
+ tmps = []
68
+
69
+ files.each do |file|
70
+ case file
71
+ when /.*\.css$/
72
+ yield file
73
+ else
74
+ tmp = Tempfile.new(File.basename(file))
75
+ tmps << tmp
76
+ css = Tilt.new(file).render
77
+ tmp.write(css)
78
+ tmp.close
79
+ yield tmp.path
80
+ end
81
+ end
82
+ ensure
83
+ tmps.each { |tmp| tmp.close! }
84
+ end
85
+ end
86
+ end
87
+
88
+ Tilt.register Tilt::PDFTemplate, 'rpdf'
data/lib/tilt-pdf.rb ADDED
@@ -0,0 +1 @@
1
+ require 'tilt/pdf'
data/tilt-pdf.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
3
+ require 'tilt/pdf/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'tilt-pdf'
7
+ s.version = Tilt::PDF::VERSION
8
+ s.authors = ['Loic Nageleisen']
9
+ s.email = ['loic.nageleisen@gmail.com']
10
+ s.homepage = 'http://github.com/lloeki/tilt-pdf'
11
+ s.summary = %q{PDF files via Tilt}
12
+ s.description = %q{Integrates PDF generation into a Tilt flow}
13
+ s.license = 'MIT'
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n")
17
+ .map { |f| File.basename(f) }
18
+ s.require_paths = ['lib']
19
+
20
+ s.add_dependency 'tilt', '~> 1.4.1'
21
+ s.add_dependency 'pdfkit', '~> 0.5.4'
22
+
23
+ s.add_development_dependency 'therubyracer'
24
+ s.add_development_dependency 'less'
25
+ s.add_development_dependency 'slim'
26
+ s.add_development_dependency 'rspec', '~> 2.14'
27
+ s.add_development_dependency 'rake'
28
+ s.add_development_dependency 'guard-rspec'
29
+ s.add_development_dependency 'pry'
30
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tilt-pdf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Loic Nageleisen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tilt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: pdfkit
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.5.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.5.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: therubyracer
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: less
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: slim
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'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '2.14'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '2.14'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Integrates PDF generation into a Tilt flow
140
+ email:
141
+ - loic.nageleisen@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - .gitignore
147
+ - .rubocop.yml
148
+ - Gemfile
149
+ - Gemfile.lock
150
+ - LICENSE
151
+ - README.mdown
152
+ - lib/tilt-pdf.rb
153
+ - lib/tilt/pdf.rb
154
+ - lib/tilt/pdf/rails.rb
155
+ - lib/tilt/pdf/version.rb
156
+ - tilt-pdf.gemspec
157
+ homepage: http://github.com/lloeki/tilt-pdf
158
+ licenses:
159
+ - MIT
160
+ metadata: {}
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - '>='
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - '>='
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubyforge_project:
177
+ rubygems_version: 2.0.3
178
+ signing_key:
179
+ specification_version: 4
180
+ summary: PDF files via Tilt
181
+ test_files: []
182
+ has_rdoc: