first version

This commit is contained in:
Loic Nageleisen 2013-10-22 17:41:08 +02:00
commit e2c2846b81
11 changed files with 303 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/test/*.pdf

15
.rubocop.yml Normal file
View file

@ -0,0 +1,15 @@
AsciiComments:
Enabled: false
MethodLength:
Max: 30
ParameterLists:
CountKeywordArgs: false
Documentation:
Enabled: false
TrivialAccessors:
ExactNameMatch: true
AllowPredicates: true
AndOr:
Enabled: false
PerlBackrefs:
Enabled: false

3
Gemfile Normal file
View file

@ -0,0 +1,3 @@
source 'https://rubygems.org'
gemspec

77
Gemfile.lock Normal file
View file

@ -0,0 +1,77 @@
PATH
remote: .
specs:
tilt-pdf (0.1.0)
pdfkit (~> 0.5.4)
tilt (~> 1.4.1)
GEM
remote: https://rubygems.org/
specs:
celluloid (0.15.2)
timers (~> 1.1.0)
coderay (1.0.9)
commonjs (0.2.7)
diff-lcs (1.2.4)
ffi (1.9.0)
formatador (0.2.4)
guard (2.1.1)
formatador (>= 0.2.4)
listen (~> 2.1)
lumberjack (~> 1.0)
pry (>= 0.9.12)
thor (>= 0.18.1)
guard-rspec (4.0.3)
guard (>= 2.1.1)
rspec (~> 2.14)
less (2.4.0)
commonjs (~> 0.2.7)
libv8 (3.16.14.3)
listen (2.1.1)
celluloid (>= 0.15.2)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9)
lumberjack (1.0.4)
method_source (0.8.2)
pdfkit (0.5.4)
pry (0.9.12.2)
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.4)
rake (10.1.0)
rb-fsevent (0.9.3)
rb-inotify (0.9.2)
ffi (>= 0.5.0)
ref (1.0.5)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.6)
rspec-expectations (2.14.3)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.4)
slim (2.0.1)
temple (~> 0.6.6)
tilt (>= 1.3.3, < 2.1)
slop (3.4.6)
temple (0.6.7)
therubyracer (0.12.0)
libv8 (~> 3.16.14.0)
ref
thor (0.18.1)
tilt (1.4.1)
timers (1.1.0)
PLATFORMS
ruby
DEPENDENCIES
guard-rspec
less
pry
rake
rspec (~> 2.14)
slim
therubyracer
tilt-pdf!

20
LICENSE Normal file
View file

@ -0,0 +1,20 @@
Copyright (c) 2012 Loic Nageleisen
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

37
README.mdown Normal file
View file

@ -0,0 +1,37 @@
# Tilt::PDF
Integrates PDF generation into a Tilt flow
## Dependencies
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.
## Usage as a Tilt
Add `tilt-pdf` to your Gemfile. Also add any template engine you may optionally want, such as `less` or `slim`.
A `foo` template is currently threefold:
- `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`.
- `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.
- `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.
The three files must currently be stored in the *same* directory.
Rendering is done the usual Tilt way:
```ruby
require 'tilt-pdf'
pdf = Tilt.new('foo.rpdf').render()
```
## Rails integration
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:
```ruby
gem 'tilt-pdf', require: 'tilt/pdf/rails'
```
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.

1
lib/tilt-pdf.rb Normal file
View file

@ -0,0 +1 @@
require 'tilt/pdf'

88
lib/tilt/pdf.rb Normal file
View file

@ -0,0 +1,88 @@
require 'pdfkit'
require 'tilt'
require 'tilt/template'
PDFKit.configure do |config|
config.wkhtmltopdf =
'/Applications/wkhtmltopdf.app/Contents/MacOS/wkhtmltopdf'
config.default_options = {
page_size: 'A4',
orientation: 'portrait',
print_media_type: true,
dpi: 600,
margin_top: 0,
margin_bottom: 0,
margin_left: 0,
margin_right: 0,
header_spacing: 0,
footer_spacing: 0,
disable_smart_shrinking: true,
zoom: 1.0,
}
end
module Tilt
class PDFTemplate < Template
self.default_mime_type = 'application/pdf'
def prepare; end
def evaluate(scope, locals, &block)
html_file = find_html
html = render_html(html_file, scope, locals, &block)
css_files = find_css
render_css(*css_files) do |*css|
kit = PDFKit.new(html, pdfkit_options)
css.each { |f| kit.stylesheets << f }
@output = kit.to_pdf
end
@output
end
private
def pdfkit_options
YAML.load(data) || {}
end
def dirname
eval_file.gsub(/#{basename}$/, '').chomp('/')
end
def find_html
Dir.glob(File.join(dirname, name + '.html*')).first
end
def find_css
Dir.glob(File.join(dirname, name + '.css*'))
end
def render_html(file, scope, locals, &block)
Tilt.new(file).render(scope, locals, &block)
end
def render_css(*files)
tmps = []
files.each do |file|
case file
when /.*\.css$/
yield file
else
tmp = Tempfile.new(File.basename(file))
tmps << tmp
css = Tilt.new(file).render
tmp.write(css)
tmp.close
yield tmp.path
end
end
ensure
tmps.each { |tmp| tmp.close! }
end
end
end
Tilt.register Tilt::PDFTemplate, 'rpdf'

25
lib/tilt/pdf/rails.rb Normal file
View file

@ -0,0 +1,25 @@
require 'tilt-pdf'
require 'action_view/template/handlers'
module ActionView
class Template
module Handlers
class PDFTemplate
class_attribute :default_format
self.default_format = :pdf
def call(template)
"Tilt.new('#{template.identifier}').render(self)"
end
end
end
register_template_handler :rpdf, Handlers::PDFTemplate.new
end
end
module Tilt::PDFTemplate::Rails
class Railtie < ::Rails::Railtie
config.app_generators.template_engine :rpdf
end
end

6
lib/tilt/pdf/version.rb Normal file
View file

@ -0,0 +1,6 @@
module Tilt
module PDF
VERSION = '0.1.0'
end
end

30
tilt-pdf.gemspec Normal file
View file

@ -0,0 +1,30 @@
# -*- encoding: utf-8 -*-
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
require 'tilt/pdf/version'
Gem::Specification.new do |s|
s.name = 'tilt-pdf'
s.version = Tilt::PDF::VERSION
s.authors = ['Loic Nageleisen']
s.email = ['loic.nageleisen@gmail.com']
s.homepage = 'http://github.com/lloeki/tilt-pdf'
s.summary = %q{PDF files via Tilt}
s.description = %q{Integrates PDF generation into a Tilt flow}
s.license = 'MIT'
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n")
.map { |f| File.basename(f) }
s.require_paths = ['lib']
s.add_dependency 'tilt', '~> 1.4.1'
s.add_dependency 'pdfkit', '~> 0.5.4'
s.add_development_dependency 'therubyracer'
s.add_development_dependency 'less'
s.add_development_dependency 'slim'
s.add_development_dependency 'rspec', '~> 2.14'
s.add_development_dependency 'rake'
s.add_development_dependency 'guard-rspec'
s.add_development_dependency 'pry'
end