ezprint 0.2.2 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Jason Stewart
1
+ Copyright (c) 2011 Jason Stewart
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README CHANGED
@@ -56,6 +56,27 @@ layout: false
56
56
  template: the template for the current controller/action
57
57
  stylesheets: none
58
58
 
59
+
60
+ Using another PDF processor
61
+ ===========================
62
+
63
+ While ezprint has been designed with PDFKit in mind, other PDF processors
64
+ such as princexml can be used. A princexml processor is included and can be used
65
+ by setting Ezprint.processor = :prince in an initializer.
66
+
67
+ It's also easy to create your own processor:
68
+
69
+ module Ezprint
70
+ module Processors
71
+ class MyProcessor < Base
72
+ def self.process(html_string, options = {})
73
+ # Code to return a PDF string from an html string here.
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+
59
80
  Credits
60
81
  =======
61
82
 
@@ -54,6 +54,25 @@ The defaults for the render options are as follows:
54
54
  template: the template for the current controller/action
55
55
  stylesheets: none
56
56
 
57
+
58
+ == Using another PDF processor
59
+
60
+ While ezprint has been designed with PDFKit in mind, other PDF processors
61
+ such as princexml can be used. A princexml processor is included and can be used
62
+ by setting Ezprint.processor = :prince in an initializer.
63
+
64
+ It's also easy to create your own processor:
65
+
66
+ module Ezprint
67
+ module Processors
68
+ class MyProcessor < Base
69
+ def self.process(html_string, options = {})
70
+ # Code to return a PDF string from an html string here.
71
+ end
72
+ end
73
+ end
74
+ end
75
+
57
76
  == Credits
58
77
 
59
78
  * Michael Bleigh for writing the awesome princely plugin, which most of the code is reworked from.
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ begin
10
10
  gem.email = "jstewart@fusionary.com"
11
11
  gem.homepage = "http://github.com/jstewart/ezprint"
12
12
  gem.authors = ["Jason Stewart"]
13
- gem.add_dependency 'pdfkit', '>=0.4.1'
13
+ gem.add_dependency 'pdfkit', '~> 0.5.0'
14
14
  end
15
15
 
16
16
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ezprint}
8
- s.version = "0.2.2"
8
+ s.version = "0.3.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jason Stewart"]
12
- s.date = %q{2011-01-13}
12
+ s.date = %q{2011-05-16}
13
13
  s.description = %q{A Rails wrapper for the PDFkit library. Meant to be a drop in replacement for princely.}
14
14
  s.email = %q{jstewart@fusionary.com}
15
15
  s.extra_rdoc_files = [
@@ -27,24 +27,26 @@ Gem::Specification.new do |s|
27
27
  "init.rb",
28
28
  "lib/ezprint.rb",
29
29
  "lib/ezprint/pdf_helper.rb",
30
+ "lib/ezprint/processors/base.rb",
31
+ "lib/ezprint/processors/pdfkit.rb",
32
+ "lib/ezprint/processors/prince.rb",
30
33
  "lib/ezprint/railtie.rb"
31
34
  ]
32
35
  s.homepage = %q{http://github.com/jstewart/ezprint}
33
36
  s.require_paths = ["lib"]
34
- s.rubygems_version = %q{1.3.7}
37
+ s.rubygems_version = %q{1.6.1}
35
38
  s.summary = %q{A Rails wrapper for the PDFkit library. Meant to be a drop in replacement for princely.}
36
39
 
37
40
  if s.respond_to? :specification_version then
38
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
39
41
  s.specification_version = 3
40
42
 
41
43
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
- s.add_runtime_dependency(%q<pdfkit>, [">= 0.4.1"])
44
+ s.add_runtime_dependency(%q<pdfkit>, ["~> 0.5.0"])
43
45
  else
44
- s.add_dependency(%q<pdfkit>, [">= 0.4.1"])
46
+ s.add_dependency(%q<pdfkit>, ["~> 0.5.0"])
45
47
  end
46
48
  else
47
- s.add_dependency(%q<pdfkit>, [">= 0.4.1"])
49
+ s.add_dependency(%q<pdfkit>, ["~> 0.5.0"])
48
50
  end
49
51
  end
50
52
 
@@ -1,9 +1,26 @@
1
- require 'ezprint/pdf_helper'
1
+ require 'active_support/inflector'
2
+
3
+ module Ezprint
4
+ autoload :PdfHelper, 'ezprint/pdf_helper'
5
+
6
+ module Processors
7
+ autoload :Base, 'ezprint/processors/base'
8
+ autoload :Pdfkit, 'ezprint/processors/pdfkit'
9
+ autoload :Prince, 'ezprint/processors/prince'
10
+ end
11
+
12
+ @@processor = :pdfkit
13
+ mattr_accessor :processor
14
+
15
+ def self.get_processor
16
+ ::Ezprint::Processors.const_get(Ezprint.processor.to_s.classify)
17
+ end
18
+ end
2
19
 
3
20
  if defined?(::Rails::Railtie)
4
21
  require 'ezprint/railtie'
5
22
  else
6
23
  # Rails 2.x
7
24
  Mime::Type.register 'application/pdf', :pdf
8
- ActionController::Base.send(:include, EzPrint::PdfHelper)
25
+ ActionController::Base.send(:include, Ezprint::PdfHelper)
9
26
  end
@@ -1,13 +1,11 @@
1
- module EzPrint
1
+ module Ezprint
2
2
  module PdfHelper
3
- require 'pdfkit'
4
-
5
3
  def self.included(base)
6
4
  base.class_eval do
7
5
  alias_method_chain :render, :ezprint
8
6
  end
9
7
  end
10
-
8
+
11
9
  def render_with_ezprint(options = nil, *args, &block)
12
10
  if options.is_a?(Symbol) or options.nil? or options[:pdf].nil?
13
11
  render_without_ezprint(options, *args, &block)
@@ -20,18 +18,14 @@ module EzPrint
20
18
  private
21
19
 
22
20
  def make_pdf(options = {})
23
- stylesheets = options.delete(:stylesheets) || []
24
- layout = options.delete(:layout) || false
25
- template = options.delete(:template) || File.join(controller_path,action_name)
21
+ template = options.delete(:template) || File.join(controller_path,action_name)
22
+ layout = options.delete(:layout) || false
23
+ stylesheets = options[:stylesheets] || []
24
+ options[:stylesheets] = stylesheets.map { |s| stylesheet_file_path(s) }
26
25
 
27
- # Stop Rails from appending timestamps to assets.
28
- ENV["RAILS_ASSET_ID"] = ''
26
+ ENV["RAILS_ASSET_ID"] = '' # Stop Rails from appending timestamps to assets
29
27
  html_string = render_to_string(:template => template, :layout => layout)
30
-
31
- kit = PDFKit.new(process_html_string(html_string), options)
32
- kit.stylesheets = stylesheets.collect{ |style| stylesheet_file_path(style) }
33
-
34
- kit.to_pdf
28
+ Ezprint.get_processor.send(:process, html_string, options)
35
29
  end
36
30
 
37
31
  def make_and_send_pdf(pdf_name, options = {})
@@ -53,15 +47,13 @@ module EzPrint
53
47
 
54
48
  def stylesheet_file_path(stylesheet)
55
49
  stylesheet = stylesheet.to_s.gsub(".css","")
56
- File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR,"#{stylesheet}.css")
57
- end
58
50
 
59
- def process_html_string(html)
60
- # reroute absolute paths
61
- html.gsub!("src=\"/", "src=\"#{RAILS_ROOT}/public/")
62
- html.gsub!("href=\"/", "src=\"#{RAILS_ROOT}/public/")
63
- html.gsub!("url(/", "url(#{RAILS_ROOT}/public/")
64
- html
51
+ stylesheets_dir = if defined? ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR
52
+ ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR
53
+ else
54
+ config.stylesheets_dir
55
+ end
56
+ File.join(stylesheets_dir, "#{stylesheet}.css")
65
57
  end
66
58
  end
67
59
  end
@@ -0,0 +1,9 @@
1
+ module Ezprint
2
+ module Processors
3
+ class Base
4
+ def self.process(html_string, options = {})
5
+ raise RuntimeError.new "Process is not implemented"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ require 'pdfkit'
2
+
3
+ module Ezprint
4
+ module Processors
5
+ class Pdfkit < Base
6
+ def self.process(html_string, options = {})
7
+ stylesheets = options.delete(:stylesheets)
8
+ kit = PDFKit.new(self.process_html(html_string), options)
9
+ kit.stylesheets = stylesheets
10
+ kit.to_pdf
11
+ end
12
+
13
+ def self.process_html(html)
14
+ # reroute absolute paths
15
+ html.gsub!("src=\"/", "src=\"#{RAILS_ROOT}/public/")
16
+ html.gsub!("href=\"/", "src=\"#{RAILS_ROOT}/public/")
17
+ html.gsub!("url(/", "url(#{RAILS_ROOT}/public/")
18
+ html
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,35 @@
1
+ module Ezprint
2
+ module Processors
3
+ class Prince < Base
4
+ def self.process(html_string, options = {})
5
+ pdf = IO.popen(self.cmd(options), "w+")
6
+ pdf.puts(self.process_html(html_string))
7
+ pdf.close_write
8
+ result = pdf.gets(nil)
9
+ pdf.close_read
10
+ result
11
+ end
12
+
13
+ def self.cmd(options)
14
+ stylesheets = options.delete(:stylesheets)
15
+ prince_cmd = `which prince`.chomp
16
+
17
+ if prince_cmd.length == 0
18
+ raise RuntimeError.new "Cannot locate prince binary. Please check your path"
19
+ end
20
+
21
+ prince_cmd << " --input=html --server "
22
+ stylesheets.each { |s| prince_cmd << " -s #{s} " }
23
+ prince_cmd << " --silent - -o -"
24
+ end
25
+
26
+ def self.process_html(html)
27
+ # reroute absolute paths
28
+ html.gsub!("src=\"/", "src=\"#{RAILS_ROOT}/public/")
29
+ html.gsub!("href=\"/", "src=\"#{RAILS_ROOT}/public/")
30
+ html.gsub!("url(/", "url(#{RAILS_ROOT}/public/")
31
+ html
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,8 +1,7 @@
1
1
  require 'action_controller/base'
2
2
  require 'action_dispatch/http/mime_types'
3
- require 'ezprint/pdf_helper'
4
3
 
5
- module EzPrint
4
+ module Ezprint
6
5
  class Railtie < Rails::Railtie
7
6
 
8
7
  initializer :init_mime_types do
@@ -11,7 +10,7 @@ module EzPrint
11
10
 
12
11
  initializer :insert_into_action_controller do
13
12
  ActiveSupport.on_load :action_controller do
14
- ActionController::Base.send(:include, EzPrint::PdfHelper)
13
+ ActionController::Base.send(:include, Ezprint::PdfHelper)
15
14
  end
16
15
  end
17
16
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ezprint
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease: false
4
+ hash: 17
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 2
10
- version: 0.2.2
8
+ - 3
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Stewart
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-01-13 00:00:00 -05:00
18
+ date: 2011-05-16 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -24,14 +24,14 @@ dependencies:
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
- - - ">="
27
+ - - ~>
28
28
  - !ruby/object:Gem::Version
29
- hash: 13
29
+ hash: 11
30
30
  segments:
31
31
  - 0
32
- - 4
33
- - 1
34
- version: 0.4.1
32
+ - 5
33
+ - 0
34
+ version: 0.5.0
35
35
  type: :runtime
36
36
  version_requirements: *id001
37
37
  description: A Rails wrapper for the PDFkit library. Meant to be a drop in replacement for princely.
@@ -54,6 +54,9 @@ files:
54
54
  - init.rb
55
55
  - lib/ezprint.rb
56
56
  - lib/ezprint/pdf_helper.rb
57
+ - lib/ezprint/processors/base.rb
58
+ - lib/ezprint/processors/pdfkit.rb
59
+ - lib/ezprint/processors/prince.rb
57
60
  - lib/ezprint/railtie.rb
58
61
  has_rdoc: true
59
62
  homepage: http://github.com/jstewart/ezprint
@@ -85,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
88
  requirements: []
86
89
 
87
90
  rubyforge_project:
88
- rubygems_version: 1.3.7
91
+ rubygems_version: 1.6.1
89
92
  signing_key:
90
93
  specification_version: 3
91
94
  summary: A Rails wrapper for the PDFkit library. Meant to be a drop in replacement for princely.