From e2c2846b81653b140dfcadbde36a8fa60e980d13 Mon Sep 17 00:00:00 2001 From: Loic Nageleisen Date: Tue, 22 Oct 2013 17:41:08 +0200 Subject: [PATCH] first version --- .gitignore | 1 + .rubocop.yml | 15 +++++++ Gemfile | 3 ++ Gemfile.lock | 77 ++++++++++++++++++++++++++++++++++++ LICENSE | 20 ++++++++++ README.mdown | 37 +++++++++++++++++ lib/tilt-pdf.rb | 1 + lib/tilt/pdf.rb | 88 +++++++++++++++++++++++++++++++++++++++++ lib/tilt/pdf/rails.rb | 25 ++++++++++++ lib/tilt/pdf/version.rb | 6 +++ tilt-pdf.gemspec | 30 ++++++++++++++ 11 files changed, 303 insertions(+) create mode 100644 .gitignore create mode 100644 .rubocop.yml create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 LICENSE create mode 100644 README.mdown create mode 100644 lib/tilt-pdf.rb create mode 100644 lib/tilt/pdf.rb create mode 100644 lib/tilt/pdf/rails.rb create mode 100644 lib/tilt/pdf/version.rb create mode 100644 tilt-pdf.gemspec diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fcc750b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/test/*.pdf diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..1e9db3b --- /dev/null +++ b/.rubocop.yml @@ -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 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..fa75df1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..1c46f0e --- /dev/null +++ b/Gemfile.lock @@ -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! diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c772868 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.mdown b/README.mdown new file mode 100644 index 0000000..bf73559 --- /dev/null +++ b/README.mdown @@ -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. diff --git a/lib/tilt-pdf.rb b/lib/tilt-pdf.rb new file mode 100644 index 0000000..4f638a3 --- /dev/null +++ b/lib/tilt-pdf.rb @@ -0,0 +1 @@ +require 'tilt/pdf' diff --git a/lib/tilt/pdf.rb b/lib/tilt/pdf.rb new file mode 100644 index 0000000..2352937 --- /dev/null +++ b/lib/tilt/pdf.rb @@ -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' diff --git a/lib/tilt/pdf/rails.rb b/lib/tilt/pdf/rails.rb new file mode 100644 index 0000000..e699286 --- /dev/null +++ b/lib/tilt/pdf/rails.rb @@ -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 diff --git a/lib/tilt/pdf/version.rb b/lib/tilt/pdf/version.rb new file mode 100644 index 0000000..e389ac5 --- /dev/null +++ b/lib/tilt/pdf/version.rb @@ -0,0 +1,6 @@ +module Tilt + module PDF + VERSION = '0.1.0' + + end +end diff --git a/tilt-pdf.gemspec b/tilt-pdf.gemspec new file mode 100644 index 0000000..54f970b --- /dev/null +++ b/tilt-pdf.gemspec @@ -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