to_pdf 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
- = ToPDF
1
+ = to_pdf
2
2
 
3
- Render rails actions as a PDF using {PrinceXML}[http://princexml.com/].
3
+ Render rails actions as a PDF using {PrinceXML}[http://princexml.com/] or Wkhtmltopdf.
4
4
 
5
5
  == Installation
6
6
 
@@ -45,7 +45,7 @@ You can though define CSS at the top of the document (note that "body" rules wil
45
45
 
46
46
  Or link a stylesheet:
47
47
 
48
- = stylesheet_link_tag 'pdf', :media => 'print'
48
+ = stylesheet_link_tag 'pdf', :media => 'all'
49
49
 
50
50
  .job
51
51
  .name
@@ -56,7 +56,7 @@ This will even work with the Rails 3.1 asset pipeline. However, in development y
56
56
  MyProject::Application.configure do
57
57
  ...
58
58
 
59
- config.prince_xml_asset_domain = "http://myproject.dev"
59
+ config.to_pdf_asset_domain = 'http://myproject.dev'
60
60
  end
61
61
 
62
62
  Another configuration option available is the path to the PrinceXML executable. Again, this can optionally be set on a per environment basis:
@@ -70,8 +70,18 @@ Another configuration option available is the path to the PrinceXML executable.
70
70
  == Caveats
71
71
 
72
72
  * Only sends PDFs inline
73
- * Does not use layouts
74
- * Without configuring #prince_xml_asset_domain stylesheets will not load images
73
+ * If you wish to use a layout then it also requires a .pdf format, eg. application.pdf.haml
74
+ * In order for css files to load images then config.prince_xml_asset_domain needs to be defined
75
+
76
+ == Wkhtmltopdf
77
+
78
+ To use Wkhtmltopdf us must add the following additional configuration:
79
+
80
+ MyProject::Application.configure do
81
+ ...
82
+
83
+ config.to_pdf_renderer = :wkhtmltopdf
84
+ end
75
85
 
76
86
  == Maintainers
77
87
 
@@ -98,4 +108,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
98
108
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
99
109
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
100
110
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
101
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
111
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/lib/to_pdf.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  autoload :PrinceXML, 'to_pdf/prince_xml'
2
+ autoload :Wkhtmltopdf, 'to_pdf/wkhtmltopdf'
2
3
 
3
4
  require 'to_pdf/railtie' if defined?(Rails::Railtie)
@@ -1,24 +1,35 @@
1
1
  module ToPDF
2
2
  class Railtie < Rails::Railtie
3
- initializer 'to_pdf.configure_prince_xml' do |app|
4
- if app.config.respond_to? :prince_xml_asset_domain
5
- PrinceXML.asset_domain = app.config.prince_xml_asset_domain
6
- end
3
+ config.to_pdf_renderer = :princexml
7
4
 
5
+ initializer 'to_pdf.configure' do |app|
6
+ if app.config.respond_to? :to_pdf_asset_domain
7
+ PrinceXML.asset_domain = app.config.to_pdf_asset_domain
8
+ Wkhtmltopdf.asset_domain = app.config.to_pdf_asset_domain
9
+ end
10
+
8
11
  if app.config.respond_to? :prince_xml_executable_path
9
12
  PrinceXML.executable_path = app.config.prince_xml_executable_path
10
13
  end
14
+
15
+ if app.config.respond_to? :wkhtmltopdf_executable_path
16
+ Wkhtmltopdf.executable_path = app.to_pdf.wkhtmltopdf_executable_path
17
+ end
11
18
  end
12
19
 
13
20
  initializer 'to_pdf.init_mime_types' do
14
21
  Mime::Type.register 'application/pdf', :pdf
15
22
  end
16
23
 
17
- initializer 'to_pdf.insert_into_action_controller' do
24
+ initializer 'to_pdf.insert_into_action_controller' do |app|
18
25
  ActiveSupport.on_load :action_controller do
19
26
  ActionController::Renderers.add :pdf do |template, options|
20
27
  string = render_to_string template, options
21
- send_data PrinceXML.string_to_pdf(string), :type => :pdf, :disposition => 'inline'
28
+ if app.config.to_pdf_renderer == :wkhtmltopdf
29
+ send_data Wkhtmltopdf.string_to_pdf(string), :type => :pdf, :disposition => 'inline'
30
+ else
31
+ send_data PrinceXML.string_to_pdf(string), :type => :pdf, :disposition => 'inline'
32
+ end
22
33
  end
23
34
 
24
35
  class ActionController::Responder
@@ -1,3 +1,3 @@
1
1
  module ToPDF
2
- VERSION = "0.1.3"
2
+ VERSION = '0.1.4'
3
3
  end
@@ -0,0 +1,48 @@
1
+ class Wkhtmltopdf
2
+ class << self
3
+ attr_writer :executable_path, :asset_domain
4
+
5
+ def asset_domain=(domain)
6
+ @asset_domain = domain
7
+ end
8
+
9
+ def executable_path=(path)
10
+ @executable_path = File.expand_path(path)
11
+ end
12
+
13
+ def detect_executable_path
14
+ IO.popen('which wkhtmltopdf') { |pipe| @executable_path = pipe.gets }
15
+ raise 'Cannot find command `wkhtmltopdf` in $PATH' unless @executable_path
16
+ @executable_path.strip!
17
+ end
18
+
19
+ def string_to_pdf(string)
20
+ detect_executable_path unless @executable_path
21
+
22
+ pdf = IO.popen(wkhtmltopdf_command, 'w+')
23
+ pdf.puts(localise_paths(string))
24
+ pdf.close_write
25
+ result = pdf.gets(nil)
26
+ pdf.close_read
27
+
28
+ result
29
+ end
30
+
31
+ def wkhtmltopdf_command
32
+ "#{@executable_path} --quiet --allow #{Rails.public_path} - -"
33
+ end
34
+
35
+ def localise_paths(string)
36
+ string.gsub!('src="/', "src=\"#{Rails.public_path}/") if defined?(Rails)
37
+
38
+ if @asset_domain
39
+ string.gsub!('link href="/', "link href=\"#{@asset_domain}/")
40
+ elsif defined?(Rails)
41
+ string.gsub!('link href="/', "link href=\"#{Rails.public_path}/")
42
+ string.gsub!('url(/', "url(#{Rails.public_path}/")
43
+ end
44
+
45
+ string
46
+ end
47
+ end
48
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-24 00:00:00.000000000Z
12
+ date: 2011-10-05 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &70349858287660 !ruby/object:Gem::Requirement
16
+ requirement: &70344200396480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70349858287660
24
+ version_requirements: *70344200396480
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70349858287060 !ruby/object:Gem::Requirement
27
+ requirement: &70344200395800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 2.6.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70349858287060
35
+ version_requirements: *70344200395800
36
36
  description: Render rails actions as a PDF using PrinceXML.
37
37
  email: hugh@artpop.com.au
38
38
  executables: []
@@ -46,6 +46,7 @@ files:
46
46
  - lib/to_pdf/prince_xml.rb
47
47
  - lib/to_pdf/railtie.rb
48
48
  - lib/to_pdf/version.rb
49
+ - lib/to_pdf/wkhtmltopdf.rb
49
50
  - to_pdf.gemspec
50
51
  homepage: https://github.com/hughevans/to_pdf
51
52
  licenses: []