wisepdf 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/Gemfile +10 -0
  2. data/Gemfile.lock +102 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +243 -0
  5. data/Rakefile +43 -0
  6. data/VERSION +1 -0
  7. data/generators/pdf/pdf_generator.rb +7 -0
  8. data/generators/pdf/templates/configure_pdf.rb +6 -0
  9. data/lib/generators/pdf_generator.rb +9 -0
  10. data/lib/wisepdf/configuration.rb +47 -0
  11. data/lib/wisepdf/errors.rb +13 -0
  12. data/lib/wisepdf/helper.rb +59 -0
  13. data/lib/wisepdf/rails/engine.rb +10 -0
  14. data/lib/wisepdf/rails/legacy.rb +5 -0
  15. data/lib/wisepdf/rails/railtie.rb +10 -0
  16. data/lib/wisepdf/rails.rb +17 -0
  17. data/lib/wisepdf/render.rb +92 -0
  18. data/lib/wisepdf/tempfile.rb +11 -0
  19. data/lib/wisepdf/writer.rb +177 -0
  20. data/lib/wisepdf.rb +10 -0
  21. data/pdf.gemspec +66 -0
  22. data/test/application_controller_test.rb +27 -0
  23. data/test/dummy/README.rdoc +261 -0
  24. data/test/dummy/Rakefile +7 -0
  25. data/test/dummy/app/assets/javascripts/application.js +15 -0
  26. data/test/dummy/app/assets/javascripts/wisepdf.js +1 -0
  27. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  28. data/test/dummy/app/assets/stylesheets/wisepdf.css +1 -0
  29. data/test/dummy/app/controllers/application_controller.rb +3 -0
  30. data/test/dummy/app/helpers/application_helper.rb +2 -0
  31. data/test/dummy/app/mailers/.gitkeep +0 -0
  32. data/test/dummy/app/models/.gitkeep +0 -0
  33. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  34. data/test/dummy/config/application.rb +57 -0
  35. data/test/dummy/config/boot.rb +10 -0
  36. data/test/dummy/config/database.yml +3 -0
  37. data/test/dummy/config/environment.rb +5 -0
  38. data/test/dummy/config/environments/development.rb +37 -0
  39. data/test/dummy/config/environments/production.rb +67 -0
  40. data/test/dummy/config/environments/test.rb +37 -0
  41. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/test/dummy/config/initializers/inflections.rb +15 -0
  43. data/test/dummy/config/initializers/mime_types.rb +5 -0
  44. data/test/dummy/config/initializers/secret_token.rb +7 -0
  45. data/test/dummy/config/initializers/session_store.rb +8 -0
  46. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  47. data/test/dummy/config/locales/en.yml +5 -0
  48. data/test/dummy/config/routes.rb +58 -0
  49. data/test/dummy/config.ru +4 -0
  50. data/test/dummy/lib/assets/.gitkeep +0 -0
  51. data/test/dummy/log/.gitkeep +0 -0
  52. data/test/dummy/log/test.log +274 -0
  53. data/test/dummy/public/404.html +26 -0
  54. data/test/dummy/public/422.html +26 -0
  55. data/test/dummy/public/500.html +25 -0
  56. data/test/dummy/public/favicon.ico +0 -0
  57. data/test/dummy/public/javascripts/wisepdf.js +1 -0
  58. data/test/dummy/public/stylesheets/wisepdf.css +1 -0
  59. data/test/dummy/script/rails +6 -0
  60. data/test/dummy/tmp/cache/assets/CA9/590/sprockets%2F260d19b0714b39b217abfe83309458b7 +0 -0
  61. data/test/dummy/tmp/cache/assets/D13/4A0/sprockets%2Fc857f4fea90e731182fa7000ea6833e9 +0 -0
  62. data/test/dummy/tmp/cache/assets/D1C/0F0/sprockets%2F13dc05c787589dd73a669e0ad23d54e8 +0 -0
  63. data/test/dummy/tmp/cache/assets/D6E/B20/sprockets%2F2669d77f5dd55e82ba092accac21871a +0 -0
  64. data/test/dummy/tmp/cache/assets/D93/BA0/sprockets%2Fe162e2a148480db4edf41c7ca8a527cb +0 -0
  65. data/test/dummy/tmp/cache/assets/E1B/1A0/sprockets%2Fbdc3a3ccd7d2f02dddd41712ed4c8e31 +0 -0
  66. data/test/helper.rb +23 -0
  67. data/test/helper_assets_test.rb +25 -0
  68. data/test/helper_legacy_test.rb +25 -0
  69. data/test/writer_test.rb +204 -0
  70. data/wisepdf.gemspec +120 -0
  71. metadata +177 -0
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'rails', '>=3.0.0'
4
+
5
+ group :development, :test do
6
+ gem 'sqlite3-ruby'
7
+ gem "shoulda", ">= 0"
8
+ gem "bundler", "~> 1.0.0"
9
+ gem "jeweler", "~> 1.6.4"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,102 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionmailer (3.2.2)
5
+ actionpack (= 3.2.2)
6
+ mail (~> 2.4.0)
7
+ actionpack (3.2.2)
8
+ activemodel (= 3.2.2)
9
+ activesupport (= 3.2.2)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ journey (~> 1.0.1)
13
+ rack (~> 1.4.0)
14
+ rack-cache (~> 1.1)
15
+ rack-test (~> 0.6.1)
16
+ sprockets (~> 2.1.2)
17
+ activemodel (3.2.2)
18
+ activesupport (= 3.2.2)
19
+ builder (~> 3.0.0)
20
+ activerecord (3.2.2)
21
+ activemodel (= 3.2.2)
22
+ activesupport (= 3.2.2)
23
+ arel (~> 3.0.2)
24
+ tzinfo (~> 0.3.29)
25
+ activeresource (3.2.2)
26
+ activemodel (= 3.2.2)
27
+ activesupport (= 3.2.2)
28
+ activesupport (3.2.2)
29
+ i18n (~> 0.6)
30
+ multi_json (~> 1.0)
31
+ arel (3.0.2)
32
+ builder (3.0.0)
33
+ erubis (2.7.0)
34
+ git (1.2.5)
35
+ hike (1.2.1)
36
+ i18n (0.6.0)
37
+ jeweler (1.6.4)
38
+ bundler (~> 1.0)
39
+ git (>= 1.2.5)
40
+ rake
41
+ journey (1.0.3)
42
+ json (1.6.5)
43
+ mail (2.4.3)
44
+ i18n (>= 0.4.0)
45
+ mime-types (~> 1.16)
46
+ treetop (~> 1.4.8)
47
+ mime-types (1.17.2)
48
+ multi_json (1.1.0)
49
+ polyglot (0.3.3)
50
+ rack (1.4.1)
51
+ rack-cache (1.2)
52
+ rack (>= 0.4)
53
+ rack-ssl (1.3.2)
54
+ rack
55
+ rack-test (0.6.1)
56
+ rack (>= 1.0)
57
+ rails (3.2.2)
58
+ actionmailer (= 3.2.2)
59
+ actionpack (= 3.2.2)
60
+ activerecord (= 3.2.2)
61
+ activeresource (= 3.2.2)
62
+ activesupport (= 3.2.2)
63
+ bundler (~> 1.0)
64
+ railties (= 3.2.2)
65
+ railties (3.2.2)
66
+ actionpack (= 3.2.2)
67
+ activesupport (= 3.2.2)
68
+ rack-ssl (~> 1.3.2)
69
+ rake (>= 0.8.7)
70
+ rdoc (~> 3.4)
71
+ thor (~> 0.14.6)
72
+ rake (0.9.2.2)
73
+ rdoc (3.12)
74
+ json (~> 1.4)
75
+ shoulda (3.0.1)
76
+ shoulda-context (~> 1.0.0)
77
+ shoulda-matchers (~> 1.0.0)
78
+ shoulda-context (1.0.0)
79
+ shoulda-matchers (1.0.0)
80
+ sprockets (2.1.2)
81
+ hike (~> 1.2)
82
+ rack (~> 1.0)
83
+ tilt (~> 1.1, != 1.3.0)
84
+ sqlite3 (1.3.5)
85
+ sqlite3-ruby (1.3.3)
86
+ sqlite3 (>= 1.3.3)
87
+ thor (0.14.6)
88
+ tilt (1.3.3)
89
+ treetop (1.4.10)
90
+ polyglot
91
+ polyglot (>= 0.3.1)
92
+ tzinfo (0.3.32)
93
+
94
+ PLATFORMS
95
+ ruby
96
+
97
+ DEPENDENCIES
98
+ bundler (~> 1.0.0)
99
+ jeweler (~> 1.6.4)
100
+ rails (>= 3.0.0)
101
+ shoulda
102
+ sqlite3-ruby
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Miles Z. Sterret
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.md ADDED
@@ -0,0 +1,243 @@
1
+ # wisepdf
2
+
3
+ Wkhtmltopdf wrapper done right.
4
+
5
+ **Wisepdf** uses the shell utility [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/) to serve a PDF file to a user from HTML. In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, then let PDF take care of the hard stuff.
6
+
7
+ **Wisepdf** is inspired by [Wicked PDF](https://github.com/mileszs/wicked_pdf) and [PDFKit](https://github.com/jdpace/PDFKit). PDF is optimized to use with Rails 3.1 (3.2), Ruby 1.9.2 and wkhtmltopdf 0.10.0 (and above).
8
+
9
+ ## Installation
10
+
11
+ First, be sure to install [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/).
12
+ Note that versions before 0.9.0 [have problems](http://code.google.com/p/wkhtmltopdf/issues/detail?id=82&q=vodnik) on some machines with reading/writing to streams.
13
+ This plugin relies on streams to communicate with wkhtmltopdf.
14
+
15
+ More information about [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/) could be found [here](http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html).
16
+
17
+ Add this to your Gemfile:
18
+
19
+ gem 'wisepdf'
20
+
21
+ then do:
22
+
23
+ bundle
24
+
25
+ ## How does it work?
26
+
27
+ ### Basic Usage
28
+
29
+ class ThingsController < ApplicationController
30
+ def show
31
+ respond_to do |format|
32
+ format.html
33
+ format.pdf do
34
+ render :pdf => "file_name"
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ ### Advanced Usage with all available options
41
+
42
+ class ThingsController < ApplicationController
43
+ def show
44
+ respond_to do |format|
45
+ format.html
46
+ format.pdf do
47
+ render :pdf => 'file_name',
48
+ :template => 'things/show.pdf.erb',
49
+ :layout => 'pdf.html', # use 'pdf.html' for a pdf.html.erb file
50
+ :show_as_html => params[:debug].present?, # allow debuging based on url param
51
+ :orientation => 'Landscape', # default Portrait
52
+ :page_size => 'A4, Letter, ...', # default A4
53
+ :save_to_file => Rails.root.join('pdfs', "#{filename}.pdf"),
54
+ :save_only => false, # depends on :save_to_file being set first
55
+ :proxy => 'TEXT',
56
+ :basic_auth => false # when true username & password are automatically sent from session
57
+ :username => 'TEXT',
58
+ :password => 'TEXT',
59
+ :cover => 'URL',
60
+ :dpi => 'dpi',
61
+ :encoding => 'TEXT',
62
+ :user_style_sheet => 'URL',
63
+ :cookie => ['_session_id SESSION_ID'], # could be an array or a single string in a 'name value' format
64
+ :post => ['query QUERY_PARAM'], # could be an array or a single string in a 'name value' format
65
+ :redirect_delay => NUMBER,
66
+ :zoom => FLOAT,
67
+ :page_offset => NUMBER,
68
+ :book => true,
69
+ :default_header => true,
70
+ :disable_javascript => false,
71
+ :greyscale => true,
72
+ :lowquality => true,
73
+ :enable_plugins => true,
74
+ :disable_internal_links => true,
75
+ :disable_external_links => true,
76
+ :print_media_type => true,
77
+ :disable_smart_shrinking => true,
78
+ :use_xserver => true,
79
+ :no_background => true,
80
+ :margin => {:top => SIZE, # default 10 (mm)
81
+ :bottom => SIZE,
82
+ :left => SIZE,
83
+ :right => SIZE},
84
+ :header => {:html => { :template => 'users/header.pdf.erb', # use :template OR :url
85
+ :layout => 'pdf_plain.html', # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout
86
+ :url => 'www.example.com',
87
+ :locals => { :foo => @bar }},
88
+ :center => 'TEXT',
89
+ :font_name => 'NAME',
90
+ :font_size => SIZE,
91
+ :left => 'TEXT',
92
+ :right => 'TEXT',
93
+ :spacing => REAL,
94
+ :line => true},
95
+ :footer => {:html => { :template => 'shared/footer.pdf.erb', # use :template OR :url
96
+ :layout => 'pdf_plain.html', # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout
97
+ :url => 'www.example.com',
98
+ :locals => { :foo => @bar }},
99
+ :center => 'TEXT',
100
+ :font_name => 'NAME',
101
+ :font_size => SIZE,
102
+ :left => 'TEXT',
103
+ :right => 'TEXT',
104
+ :spacing => REAL,
105
+ :line => true},
106
+ :outline => {:outline => true,
107
+ :outline_depth => LEVEL}
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ By default, it will render without a layout (:layout => false) and the template for the current controller and action.
114
+
115
+ ### Super Advanced Usage ###
116
+
117
+ If you need to just create a pdf and not display it:
118
+
119
+ # create a pdf from a string
120
+ pdf = Wisepdf::Writer.new.to_pdf('<h1>Hello There!</h1>')
121
+
122
+ # or from your controller, using views & templates and all other options as normal
123
+ pdf = render_to_string :pdf => "some_file_name"
124
+
125
+ # then save to a file
126
+ save_path = Rails.root.join('pdfs','filename.pdf')
127
+ File.open(save_path, 'wb') do |file|
128
+ file << pdf
129
+ end
130
+
131
+ If you need to display utf encoded characters, add this to your pdf views or layouts:
132
+
133
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
134
+
135
+ ### Styles
136
+
137
+ You must define absolute paths to CSS files, images, and javascripts; the best option is to use the *wisepdf_stylesheet_tag*, *wisepdf_image_tag*, and *wisepdf_javascript_tag* helpers.
138
+
139
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
140
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
141
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
142
+ <head>
143
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
144
+ <%= wisepdf_stylesheet_tag "pdf" -%>
145
+ <%= wisepdf_javascript_tag "number_pages" %>
146
+ </head>
147
+ <body onload='number_pages'>
148
+ <div id="header">
149
+ <%= wisepdf_image_tag 'mysite.jpg' %>
150
+ </div>
151
+ <div id="content">
152
+ <%= yield %>
153
+ </div>
154
+ </body>
155
+ </html>
156
+
157
+ ### Page Numbering
158
+
159
+ A bit of javascript can help you number your pages. Create a template or header/footer file with this:
160
+
161
+ <html>
162
+ <head>
163
+ <script>
164
+ function number_pages() {
165
+ var vars={};
166
+ var x=document.location.search.substring(1).split('&');
167
+ for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
168
+ var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
169
+ for(var i in x) {
170
+ var y = document.getElementsByClassName(x[i]);
171
+ for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
172
+ }
173
+ }
174
+ </script>
175
+ </head>
176
+ <body onload="number_pages()">
177
+ Page <span class="page"></span> of <span class="topage"></span>
178
+ </body>
179
+ </html>
180
+
181
+ Anything with a class listed in "var x" above will be auto-filled at render time.
182
+
183
+ If you do not have explicit page breaks (and therefore do not have any "page" class), you can also use wkhtmltopdf's built in page number generation by setting one of the headers to "[page]":
184
+
185
+ render :pdf => 'filename', :header => { :right => '[page] of [topage]' }
186
+
187
+ ### Configuration
188
+
189
+ You can put your default configuration, applied to all pdf's at "configure_wisepdf.rb" initializer.
190
+
191
+ Wisepdf::Configuration.configure do |c|
192
+ c.wkhtmltopdf = '/path/to/wkhtmltopdf'
193
+ c.options = {
194
+ :layout => "layout.html",
195
+ :use_xserver => true,
196
+ :footer => {
197
+ :right => "#{Date.today.year}",
198
+ :font_size => 8,
199
+ :spacing => 8
200
+ },
201
+ :margin => {
202
+ :bottom => 15
203
+ }
204
+ }
205
+
206
+ end
207
+
208
+
209
+ ### Debugging
210
+
211
+ You can use a debug param on the URL that shows you the content of the pdf in plain html to design it faster.
212
+
213
+ First of all you must configure the render parameter `:show_as_html => params[:debug]` and then just use it like normally but adding `debug=1` as a param:
214
+
215
+ http://localhost:3001/CONTROLLER/X.pdf?debug=1
216
+
217
+ However, the wisepdf_* helpers will use file:// paths for assets when using :show_as_html, and your browser's cross-domain safety feature will kick in, and not render them. To get around this, you can load your assets like so in your templates:
218
+
219
+ <%= params[:debug].present? ? image_tag('foo') : wisepdf_image_tag('foo') %>
220
+
221
+ ## Production?
222
+
223
+ **wisepdf** is used at:
224
+
225
+ * [www.sdelki.ru](http://www.sdelki.ru)
226
+ * [www.lienlog.com](http://www.lienlog.com)
227
+
228
+ Know other projects? Then contact me and I will add them to the list.
229
+
230
+ ## Contributing to wisepdf
231
+
232
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
233
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
234
+ * Fork the project
235
+ * Start a feature/bugfix branch
236
+ * Commit and push until you are happy with your contribution
237
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
238
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
239
+
240
+ ## Copyright
241
+
242
+ Copyright (c) 2012 Igor Alexandrov. See LICENSE.txt for
243
+ further details.
data/Rakefile ADDED
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ gem.name = "wisepdf"
17
+ gem.homepage = "http://github.com/igor-alexandrov/wisepdf"
18
+ gem.license = "MIT"
19
+ gem.summary = "wkhtmltopdf for Rails done right"
20
+ gem.description = %Q{wisepdf uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML. In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, and let pdf take care of the hard stuff.}
21
+ gem.email = "igor.alexandrov@gmail.com"
22
+ gem.authors = [ "Igor Alexandrov" ]
23
+ end
24
+ Jeweler::RubygemsDotOrgTasks.new
25
+
26
+ require 'rake/testtask'
27
+ Rake::TestTask.new(:test) do |test|
28
+ test.libs << 'lib' << 'test'
29
+ test.pattern = 'test/**/*_test.rb'
30
+ test.verbose = true
31
+ end
32
+
33
+ task :default => :test
34
+
35
+ require 'rdoc/task'
36
+ Rake::RDocTask.new do |rdoc|
37
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
38
+
39
+ rdoc.rdoc_dir = 'rdoc'
40
+ rdoc.title = "wisepdf #{version}"
41
+ rdoc.rdoc_files.include('README*')
42
+ rdoc.rdoc_files.include('lib/**/*.rb')
43
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,7 @@
1
+ class PdfGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.file "configure_pdf.rb", "config/initializers/configure_pdf.rb"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ Pdf::Configuration.configuration do |config|
2
+ config.wkhtmltopdf = '/path/to/wkhtmltopdf'
3
+ config.options = {
4
+ #:layout => "pdf.html"
5
+ }
6
+ end
@@ -0,0 +1,9 @@
1
+ if defined?(Rails) && Rails::VERSION::MAJOR > 2
2
+ # Rails3 generator invoked with 'rails generate pdf'
3
+ class PdfGenerator < Rails::Generators::Base
4
+ source_root(File.expand_path(File.dirname(__FILE__) + "/../../generators/pdf/templates"))
5
+ def copy_initializer
6
+ copy_file 'configure_pdf.rb', 'config/initializers/configure_pdf.rb'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,47 @@
1
+ require 'singleton'
2
+
3
+ module Wisepdf
4
+ class Configuration
5
+ include Singleton
6
+ cattr_accessor :options
7
+ cattr_accessor :wkhtmltopdf
8
+
9
+ class << self
10
+ def wkhtmltopdf
11
+ return @@wkhtmltopdf if @@wkhtmltopdf.present?
12
+
13
+ if @@wkhtmltopdf.nil? && !self.windows?
14
+ @@wkhtmltopdf = (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
15
+ end
16
+ return @@wkhtmltopdf
17
+ end
18
+
19
+ def configure
20
+ yield self
21
+ end
22
+
23
+ def reset!
24
+ @@options = {
25
+ :layout => "pdf.html",
26
+ :use_xserver => false
27
+ }
28
+ @@wkhtmltopdf = nil
29
+ end
30
+
31
+ def development?
32
+ (defined?(::Rails) && ::Rails.env == 'development') ||
33
+ (defined?(RAILS_ENV) && RAILS_ENV == 'development')
34
+ end
35
+
36
+ def windows?
37
+ RbConfig::CONFIG['target_os'] == 'mingw32'
38
+ end
39
+ end
40
+
41
+ self.reset!
42
+
43
+ def method_missing(method)
44
+ self.class.send(method)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,13 @@
1
+ module Wisepdf
2
+ class NoExecutableError < StandardError
3
+ def initialize(path=nil)
4
+ msg = "No wkhtmltopdf found or it is not executable\n"
5
+ msg += "Path: '#{path}'\n" unless path.nil?
6
+ msg += "Please install wkhtmltopdf - http://code.google.com/p/wkhtmltopdf"
7
+ super(msg)
8
+ end
9
+ end
10
+
11
+ class WriteError < StandardError
12
+ end
13
+ end
@@ -0,0 +1,59 @@
1
+ module Wisepdf
2
+ module Helper
3
+ module Legacy
4
+ def wisepdf_stylesheet_tag(*sources)
5
+ stylesheets_dir = ::Rails.root.join('public','stylesheets')
6
+
7
+ sources.collect { |source|
8
+ filename = ( source =~ /.css\z/ ? source : source + '.css' )
9
+ "<style type='text/css'>
10
+ #{File.read(stylesheets_dir.join(filename))}
11
+ </style>"
12
+ }.join("\n").html_safe
13
+ end
14
+
15
+ def wisepdf_image_tag(img, options={})
16
+ image_tag "file:///#{::Rails.root.join('public', 'images', img).pathname.to_s}", options
17
+ end
18
+
19
+ def wisepdf_javascript_tag(*sources)
20
+ javascripts_dir = ::Rails.root.join('public','javascripts')
21
+
22
+ sources.collect { |source|
23
+ filename = ( source =~ /.js\z/ ? source : source + '.js' )
24
+ "<script type='text/javascript'>
25
+ //<![CDATA[
26
+ #{File.read(javascripts_dir.join(filename))}
27
+ //]]>
28
+ </script>"
29
+ }.join("\n").html_safe
30
+ end
31
+ end
32
+
33
+ module Assets
34
+ def wisepdf_stylesheet_tag(*sources)
35
+ sources.collect { |source|
36
+ filename = ( source =~ /.css\z/ ? source : source + '.css' )
37
+ "<style type='text/css'>
38
+ #{::Rails.application.assets.find_asset(filename)}
39
+ </style>"
40
+ }.join("\n").html_safe
41
+ end
42
+
43
+ def wisepdf_image_tag(img, options={})
44
+ image_tag "file:///#{::Rails.application.assets.find_asset(img).pathname.to_s}", options
45
+ end
46
+
47
+ def wisepdf_javascript_tag(*sources)
48
+ sources.collect { |source|
49
+ filename = ( source =~ /.js\z/ ? source : source + '.js' )
50
+ "<script type='text/javascript'>
51
+ //<![CDATA[
52
+ #{::Rails.application.assets.find_asset(filename)}
53
+ //]]>
54
+ </script>"
55
+ }.join("\n").html_safe
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,10 @@
1
+ module Wisepdf
2
+ module Rails
3
+ class Engine < ::Rails::Engine
4
+ initializer "wicked_pdf.register" do
5
+ ActionController::Base.send :include, Render
6
+ ActionView::Base.send :include, Helper::Assets
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ ActionController::Base.send :include, Render
2
+
3
+ unless ActionView::Base.instance_methods.include? "wicked_pdf_stylesheet_link_tag"
4
+ ActionView::Base.send :include, Helper::Legacy
5
+ end
@@ -0,0 +1,10 @@
1
+ module Wisepdf
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+ initializer "wicked_pdf.register" do
5
+ ActionController::Base.send :include, Render
6
+ ActionView::Base.send :include, Helper::Legacy
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ module Wisepdf
2
+ module Rails
3
+ if ::Rails::VERSION::MAJOR == 2
4
+ require 'wisepdf/rails/legacy'
5
+ elsif ::Rails::VERSION::MAJOR > 2
6
+ if ::Rails::VERSION::MINOR < 1
7
+ require 'wisepdf/rails/railtie'
8
+ else
9
+ require 'wisepdf/rails/engine'
10
+ end
11
+ end
12
+
13
+ if Mime::Type.lookup_by_extension(:pdf).nil?
14
+ Mime::Type.register('application/pdf', :pdf)
15
+ end
16
+ end
17
+ end