pdfkit 0.4.6 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +1 -0
- data/Gemfile +2 -8
- data/Gemfile.lock +23 -26
- data/History.md +26 -0
- data/POST_INSTALL +11 -5
- data/README.md +60 -19
- data/Rakefile +1 -22
- data/lib/pdfkit/configuration.rb +10 -8
- data/lib/pdfkit/middleware.rb +62 -44
- data/lib/pdfkit/pdfkit.rb +50 -49
- data/lib/pdfkit/source.rb +2 -6
- data/lib/pdfkit/version.rb +3 -0
- data/lib/pdfkit.rb +1 -2
- data/pdfkit.gemspec +19 -76
- data/spec/middleware_spec.rb +161 -15
- data/spec/pdfkit_spec.rb +97 -39
- data/spec/source_spec.rb +5 -0
- data/spec/spec_helper.rb +4 -7
- metadata +70 -101
- data/VERSION +0 -1
- data/bin/pdfkit +0 -72
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,35 +1,32 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
pdfkit (0.5.2)
|
|
5
|
+
|
|
1
6
|
GEM
|
|
2
7
|
remote: http://rubygems.org/
|
|
3
8
|
specs:
|
|
9
|
+
activesupport (3.0.9)
|
|
4
10
|
diff-lcs (1.1.2)
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
rspec (2.
|
|
17
|
-
rspec-core (= 2.0.0.beta.19)
|
|
18
|
-
rspec-expectations (= 2.0.0.beta.19)
|
|
19
|
-
rspec-mocks (= 2.0.0.beta.19)
|
|
20
|
-
rspec-core (2.0.0.beta.19)
|
|
21
|
-
rspec-expectations (2.0.0.beta.19)
|
|
22
|
-
diff-lcs (>= 1.1.2)
|
|
23
|
-
rspec-mocks (2.0.0.beta.19)
|
|
24
|
-
rubyforge (2.0.4)
|
|
25
|
-
json_pure (>= 1.1.7)
|
|
11
|
+
mocha (0.9.12)
|
|
12
|
+
rack (1.3.0)
|
|
13
|
+
rack-test (0.6.0)
|
|
14
|
+
rack (>= 1.0)
|
|
15
|
+
rspec (2.2.0)
|
|
16
|
+
rspec-core (~> 2.2)
|
|
17
|
+
rspec-expectations (~> 2.2)
|
|
18
|
+
rspec-mocks (~> 2.2)
|
|
19
|
+
rspec-core (2.6.4)
|
|
20
|
+
rspec-expectations (2.6.0)
|
|
21
|
+
diff-lcs (~> 1.1.2)
|
|
22
|
+
rspec-mocks (2.6.0)
|
|
26
23
|
|
|
27
24
|
PLATFORMS
|
|
28
25
|
ruby
|
|
29
26
|
|
|
30
27
|
DEPENDENCIES
|
|
31
|
-
|
|
32
|
-
mocha
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
rspec
|
|
28
|
+
activesupport (>= 3.0.8)
|
|
29
|
+
mocha (>= 0.9.10)
|
|
30
|
+
pdfkit!
|
|
31
|
+
rack-test (>= 0.5.6)
|
|
32
|
+
rspec (~> 2.2.0)
|
data/History.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
2011-07-02
|
|
2
|
+
==================
|
|
3
|
+
* Bump to 0.5.2
|
|
4
|
+
* Fix of dealing with ActiveSupport::SafeBuffer >= 3.0.8.
|
|
5
|
+
* Fix for meta tag options getting dropped in REE 1.8.7.
|
|
6
|
+
* Fix on bundler environment detection.
|
|
7
|
+
|
|
8
|
+
2011-06-17
|
|
9
|
+
==================
|
|
10
|
+
* Bump to 0.5.1
|
|
11
|
+
* Fix for response body coming through as an array.
|
|
12
|
+
* Added root_url configuration for setup where a host my not know its own name.
|
|
13
|
+
* Awareness of Bundler when looking for the wkhtmltopdf executable.
|
|
14
|
+
* Fix for file data getting truncated in Ruby 1.8.6
|
|
15
|
+
* Fix for 0.5.0 release getting stuck rendering all requests as PDFs.
|
|
16
|
+
* More robust meta tag detection.
|
|
17
|
+
|
|
18
|
+
2010-12-27
|
|
19
|
+
==================
|
|
20
|
+
* Bump to 0.5.0
|
|
21
|
+
* Switched to popen - adds support for JRuby and Windows
|
|
22
|
+
* Pulled in support for pdf rendering conditions in middleware via Rémy Coutable
|
|
23
|
+
* Use `which` to try and determine path to wkhtmltopdf
|
|
24
|
+
* Removed wkhtmltopdf auto installer
|
|
25
|
+
* Changed :disable\_smart\_shrinking to false for default options.
|
|
26
|
+
* Added History.md
|
data/POST_INSTALL
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
******************************************************************
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
Global: sudo `which pdfkit` --install-wkhtmltopdf
|
|
5
|
-
or inside RVM folder: export TO=`which pdfkit | sed 's:/pdfkit:/wkhtmltopdf:'` && pdfkit --install-wkhtmltopdf
|
|
6
|
-
(run pdfkit --help to see more options)
|
|
3
|
+
Install wkhtmltopdf:
|
|
7
4
|
|
|
8
|
-
|
|
5
|
+
1. Install by hand (recomended):
|
|
6
|
+
|
|
7
|
+
https://github.com/jdpace/PDFKit/wiki/Installing-WKHTMLTOPDF
|
|
8
|
+
|
|
9
|
+
2. Try using the wkhtmltopdf-binary gem (mac + linux i386)
|
|
10
|
+
|
|
11
|
+
gem install wkhtmltopdf-binary
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
******************************************************************
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# PDFKit
|
|
2
2
|
|
|
3
|
-
Create PDFs using plain old HTML+CSS. Uses [wkhtmltopdf](http://github.com/antialize/wkhtmltopdf) on the
|
|
3
|
+
Create PDFs using plain old HTML+CSS. Uses [wkhtmltopdf](http://github.com/antialize/wkhtmltopdf) on the back-end which renders HTML using Webkit.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -9,24 +9,30 @@ Create PDFs using plain old HTML+CSS. Uses [wkhtmltopdf](http://github.com/antia
|
|
|
9
9
|
gem install pdfkit
|
|
10
10
|
|
|
11
11
|
### wkhtmltopdf
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
|
|
13
|
+
1. Install by hand (recommended):
|
|
14
|
+
|
|
15
|
+
<https://github.com/jdpace/PDFKit/wiki/Installing-WKHTMLTOPDF>
|
|
16
|
+
|
|
17
|
+
2. Try using the wkhtmltopdf-binary gem (mac + linux i386)
|
|
18
|
+
|
|
19
|
+
gem install wkhtmltopdf-binary
|
|
20
|
+
|
|
21
|
+
*Note:* The automated installer has been removed.
|
|
16
22
|
|
|
17
23
|
## Usage
|
|
18
|
-
|
|
24
|
+
|
|
19
25
|
# PDFKit.new takes the HTML and any options for wkhtmltopdf
|
|
20
26
|
# run `wkhtmltopdf --extended-help` for a full list of options
|
|
21
27
|
kit = PDFKit.new(html, :page_size => 'Letter')
|
|
22
28
|
kit.stylesheets << '/path/to/css/file'
|
|
23
|
-
|
|
29
|
+
|
|
24
30
|
# Git an inline PDF
|
|
25
31
|
pdf = kit.to_pdf
|
|
26
|
-
|
|
32
|
+
|
|
27
33
|
# Save the PDF to a file
|
|
28
34
|
file = kit.to_file('/path/to/save/pdf')
|
|
29
|
-
|
|
35
|
+
|
|
30
36
|
# PDFKit.new can optionally accept a URL or a File.
|
|
31
37
|
# Stylesheets can not be added when source is provided as a URL of File.
|
|
32
38
|
kit = PDFKit.new('http://google.com')
|
|
@@ -34,18 +40,19 @@ Create PDFs using plain old HTML+CSS. Uses [wkhtmltopdf](http://github.com/antia
|
|
|
34
40
|
|
|
35
41
|
# Add any kind of option through meta tags
|
|
36
42
|
PDFKit.new('<html><head><meta name="pdfkit-page_size" content="Letter")
|
|
37
|
-
|
|
43
|
+
|
|
38
44
|
## Configuration
|
|
39
45
|
|
|
40
46
|
If you're on Windows or you installed wkhtmltopdf by hand to a location other than /usr/local/bin you will need to tell PDFKit where the binary is. You can configure PDFKit like so:
|
|
41
47
|
|
|
42
48
|
# config/initializers/pdfkit.rb
|
|
43
49
|
PDFKit.configure do |config|
|
|
44
|
-
config.wkhtmltopdf = '/path/to/wkhtmltopdf'
|
|
45
|
-
config.default_options = {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
50
|
+
# config.wkhtmltopdf = '/path/to/wkhtmltopdf'
|
|
51
|
+
# config.default_options = {
|
|
52
|
+
# :page_size => 'Legal',
|
|
53
|
+
# :print_media_type => true
|
|
54
|
+
# }
|
|
55
|
+
# config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server.
|
|
49
56
|
end
|
|
50
57
|
|
|
51
58
|
## Middleware
|
|
@@ -55,27 +62,61 @@ PDFKit comes with a middleware that allows users to get a PDF view of any page o
|
|
|
55
62
|
### Middleware Setup
|
|
56
63
|
|
|
57
64
|
**Non-Rails Rack apps**
|
|
58
|
-
|
|
65
|
+
|
|
59
66
|
# in config.ru
|
|
60
67
|
require 'pdfkit'
|
|
61
68
|
use PDFKit::Middleware
|
|
62
|
-
|
|
69
|
+
|
|
63
70
|
**Rails apps**
|
|
64
71
|
|
|
65
72
|
# in application.rb(Rails3) or environment.rb(Rails2)
|
|
66
73
|
require 'pdfkit'
|
|
67
74
|
config.middleware.use PDFKit::Middleware
|
|
68
|
-
|
|
75
|
+
|
|
69
76
|
**With PDFKit options**
|
|
70
77
|
|
|
71
78
|
# options will be passed to PDFKit.new
|
|
72
79
|
config.middleware.use PDFKit::Middleware, :print_media_type => true
|
|
73
80
|
|
|
81
|
+
**With conditions to limit routes that can be generated in pdf**
|
|
82
|
+
|
|
83
|
+
# conditions can be regexps (either one or an array)
|
|
84
|
+
config.middleware.use PDFKit::Middleware, {}, :only => %r[^/public]
|
|
85
|
+
config.middleware.use PDFKit::Middleware, {}, :only => [%r[^/invoice], %r[^/public]]
|
|
86
|
+
|
|
87
|
+
# conditions can be strings (either one or an array)
|
|
88
|
+
config.middleware.use PDFKit::Middleware, {}, :only => '/public'
|
|
89
|
+
config.middleware.use PDFKit::Middleware, {}, :only => ['/invoice', '/public']
|
|
90
|
+
|
|
91
|
+
## Troubleshooting
|
|
92
|
+
|
|
93
|
+
* **Single thread issue:** In development environments it is common to run a
|
|
94
|
+
single server process. This can cause issues when rendering your pdf
|
|
95
|
+
requires wkhtmltopdf to hit your server again (for images, js, css).
|
|
96
|
+
This is because the resource requests will get blocked by the initial
|
|
97
|
+
request and the initial request will be waiting on the resource
|
|
98
|
+
requests causing a deadlock.
|
|
99
|
+
|
|
100
|
+
This is usually not an issue in a production environment. To get
|
|
101
|
+
around this issue you may want to run a server with multiple workers
|
|
102
|
+
like Passenger or try to embed your resources within your HTML to
|
|
103
|
+
avoid extra HTTP requests.
|
|
104
|
+
|
|
105
|
+
* **Resources aren't included in the PDF:** Images, CSS, or JavaScript
|
|
106
|
+
does not seem to be downloading correctly in the PDF. This is due
|
|
107
|
+
to the fact that wkhtmltopdf does not know where to find those files.
|
|
108
|
+
Make sure you are using absolute paths (start with forward slash) to
|
|
109
|
+
your resources. If you are using PDFKit to generate PDFs from a raw
|
|
110
|
+
HTML source make sure you use complete paths (either file paths or
|
|
111
|
+
urls including the domain). In restrictive server environments the
|
|
112
|
+
root_url configuration may be what you are looking for change your
|
|
113
|
+
asset host.
|
|
114
|
+
|
|
74
115
|
## TODO
|
|
75
116
|
- add amd64 support in --install-wkhtmltopdf
|
|
76
117
|
|
|
77
118
|
## Note on Patches/Pull Requests
|
|
78
|
-
|
|
119
|
+
|
|
79
120
|
* Fork the project.
|
|
80
121
|
* Setup your development environment with: gem install bundler; bundle install
|
|
81
122
|
* Make your feature addition or bug fix.
|
data/Rakefile
CHANGED
|
@@ -1,26 +1,7 @@
|
|
|
1
1
|
require 'rubygems'
|
|
2
2
|
require 'rake'
|
|
3
3
|
require 'bundler'
|
|
4
|
-
Bundler.
|
|
5
|
-
|
|
6
|
-
begin
|
|
7
|
-
require 'jeweler'
|
|
8
|
-
Jeweler::Tasks.new do |gem|
|
|
9
|
-
gem.name = "pdfkit"
|
|
10
|
-
gem.summary = %Q{HTML+CSS -> PDF}
|
|
11
|
-
gem.description = %Q{Uses wkhtmltopdf to create PDFs using HTML}
|
|
12
|
-
gem.email = "jared@codewordstudios.com"
|
|
13
|
-
gem.homepage = "http://github.com/jdpace/PDFKit"
|
|
14
|
-
gem.authors = ["jdpace"]
|
|
15
|
-
gem.add_development_dependency "rspec", "~> 2.0.0.beta.8"
|
|
16
|
-
gem.add_development_dependency "rspec-core", "~> 2.0.0.beta.8"
|
|
17
|
-
gem.add_development_dependency 'mocha'
|
|
18
|
-
gem.post_install_message = File.read('POST_INSTALL')
|
|
19
|
-
end
|
|
20
|
-
Jeweler::GemcutterTasks.new
|
|
21
|
-
rescue LoadError
|
|
22
|
-
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
|
23
|
-
end
|
|
4
|
+
Bundler::GemHelper.install_tasks
|
|
24
5
|
|
|
25
6
|
require 'rspec/core/rake_task'
|
|
26
7
|
RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
@@ -30,8 +11,6 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
|
|
|
30
11
|
spec.rcov = true
|
|
31
12
|
end
|
|
32
13
|
|
|
33
|
-
task :spec => :check_dependencies
|
|
34
|
-
|
|
35
14
|
task :default => :spec
|
|
36
15
|
|
|
37
16
|
require 'rake/rdoctask'
|
data/lib/pdfkit/configuration.rb
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
class PDFKit
|
|
2
2
|
class Configuration
|
|
3
|
-
attr_accessor :meta_tag_prefix, :
|
|
3
|
+
attr_accessor :meta_tag_prefix, :default_options, :root_url
|
|
4
|
+
attr_writer :wkhtmltopdf
|
|
4
5
|
|
|
5
6
|
def initialize
|
|
6
7
|
@meta_tag_prefix = 'pdfkit-'
|
|
7
|
-
@wkhtmltopdf = '/usr/local/bin/wkhtmltopdf'
|
|
8
8
|
@default_options = {
|
|
9
|
-
:disable_smart_shrinking =>
|
|
9
|
+
:disable_smart_shrinking => false,
|
|
10
10
|
:page_size => 'Letter',
|
|
11
11
|
:margin_top => '0.75in',
|
|
12
12
|
:margin_right => '0.75in',
|
|
@@ -15,6 +15,10 @@ class PDFKit
|
|
|
15
15
|
:encoding => "UTF-8"
|
|
16
16
|
}
|
|
17
17
|
end
|
|
18
|
+
|
|
19
|
+
def wkhtmltopdf
|
|
20
|
+
@wkhtmltopdf ||= (defined?(Bundler::GemfileError) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
|
|
21
|
+
end
|
|
18
22
|
end
|
|
19
23
|
|
|
20
24
|
class << self
|
|
@@ -28,14 +32,12 @@ class PDFKit
|
|
|
28
32
|
# PDFKit.configure do |config|
|
|
29
33
|
# config.wkhtmltopdf = '/usr/bin/wkhtmltopdf'
|
|
30
34
|
# end
|
|
31
|
-
|
|
35
|
+
|
|
32
36
|
def self.configuration
|
|
33
37
|
@configuration ||= Configuration.new
|
|
34
38
|
end
|
|
35
|
-
|
|
36
|
-
|
|
39
|
+
|
|
37
40
|
def self.configure
|
|
38
|
-
self.configuration
|
|
39
41
|
yield(configuration)
|
|
40
42
|
end
|
|
41
|
-
end
|
|
43
|
+
end
|
data/lib/pdfkit/middleware.rb
CHANGED
|
@@ -1,61 +1,79 @@
|
|
|
1
1
|
class PDFKit
|
|
2
|
-
|
|
2
|
+
|
|
3
3
|
class Middleware
|
|
4
|
-
|
|
5
|
-
def initialize(app, options = {})
|
|
6
|
-
@app
|
|
7
|
-
@options
|
|
4
|
+
|
|
5
|
+
def initialize(app, options = {}, conditions = {})
|
|
6
|
+
@app = app
|
|
7
|
+
@options = options
|
|
8
|
+
@conditions = conditions
|
|
8
9
|
end
|
|
9
|
-
|
|
10
|
+
|
|
10
11
|
def call(env)
|
|
12
|
+
@request = Rack::Request.new(env)
|
|
11
13
|
@render_pdf = false
|
|
12
|
-
|
|
13
|
-
|
|
14
|
+
|
|
15
|
+
set_request_to_render_as_pdf(env) if render_as_pdf?
|
|
14
16
|
status, headers, response = @app.call(env)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
body =
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
pdf = PDFKit.new(body, @options)
|
|
23
|
-
body = pdf.to_pdf
|
|
24
|
-
|
|
17
|
+
|
|
18
|
+
if rendering_pdf? && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
|
|
19
|
+
body = response.respond_to?(:body) ? response.body : response.join
|
|
20
|
+
body = body.join if body.is_a?(Array)
|
|
21
|
+
body = PDFKit.new(translate_paths(body, env), @options).to_pdf
|
|
22
|
+
response = [body]
|
|
23
|
+
|
|
25
24
|
# Do not cache PDFs
|
|
26
25
|
headers.delete('ETag')
|
|
27
26
|
headers.delete('Cache-Control')
|
|
28
|
-
|
|
29
|
-
headers["Content-Length"]
|
|
30
|
-
headers["Content-Type"]
|
|
31
|
-
|
|
32
|
-
response = [body]
|
|
27
|
+
|
|
28
|
+
headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
|
|
29
|
+
headers["Content-Type"] = "application/pdf"
|
|
33
30
|
end
|
|
34
|
-
|
|
31
|
+
|
|
35
32
|
[status, headers, response]
|
|
36
33
|
end
|
|
37
|
-
|
|
34
|
+
|
|
38
35
|
private
|
|
39
|
-
|
|
40
|
-
# Change relative paths to absolute
|
|
41
|
-
def translate_paths(body, env)
|
|
42
|
-
# Host with protocol
|
|
43
|
-
root = env['rack.url_scheme'] + "://" + env['HTTP_HOST'] + "/"
|
|
44
|
-
|
|
45
|
-
body.gsub(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/,'\1=\2'+root+'\3\2')
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def set_request_to_render_as_pdf(env)
|
|
49
|
-
@render_pdf = true
|
|
50
36
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
37
|
+
# Change relative paths to absolute
|
|
38
|
+
def translate_paths(body, env)
|
|
39
|
+
# Host with protocol
|
|
40
|
+
root = PDFKit.configuration.root_url || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
|
|
41
|
+
|
|
42
|
+
body.gsub(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def rendering_pdf?
|
|
46
|
+
@render_pdf
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def render_as_pdf?
|
|
50
|
+
request_path_is_pdf = @request.path.match(%r{\.pdf$})
|
|
51
|
+
|
|
52
|
+
if request_path_is_pdf && @conditions[:only]
|
|
53
|
+
rules = [@conditions[:only]].flatten
|
|
54
|
+
rules.any? do |pattern|
|
|
55
|
+
if pattern.is_a?(Regexp)
|
|
56
|
+
@request.path =~ pattern
|
|
57
|
+
else
|
|
58
|
+
@request.path[0, pattern.length] == pattern
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
else
|
|
62
|
+
request_path_is_pdf
|
|
58
63
|
end
|
|
59
|
-
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def set_request_to_render_as_pdf(env)
|
|
67
|
+
@render_pdf = true
|
|
68
|
+
path = @request.path.sub(%r{\.pdf$}, '')
|
|
69
|
+
%w[PATH_INFO REQUEST_URI].each { |e| env[e] = path }
|
|
70
|
+
env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
|
|
71
|
+
env["Rack-Middleware-PDFKit"] = "true"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def concat(accepts, type)
|
|
75
|
+
(accepts || '').split(',').unshift(type).compact.join(',')
|
|
76
|
+
end
|
|
77
|
+
|
|
60
78
|
end
|
|
61
79
|
end
|
data/lib/pdfkit/pdfkit.rb
CHANGED
|
@@ -3,45 +3,46 @@ class PDFKit
|
|
|
3
3
|
class NoExecutableError < StandardError
|
|
4
4
|
def initialize
|
|
5
5
|
msg = "No wkhtmltopdf executable found at #{PDFKit.configuration.wkhtmltopdf}\n"
|
|
6
|
-
msg << ">>
|
|
6
|
+
msg << ">> Please install wkhtmltopdf - https://github.com/jdpace/PDFKit/wiki/Installing-WKHTMLTOPDF"
|
|
7
7
|
super(msg)
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
|
-
|
|
10
|
+
|
|
11
11
|
class ImproperSourceError < StandardError
|
|
12
12
|
def initialize(msg)
|
|
13
13
|
super("Improper Source: #{msg}")
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
|
-
|
|
16
|
+
|
|
17
17
|
attr_accessor :source, :stylesheets
|
|
18
18
|
attr_reader :options
|
|
19
|
-
|
|
19
|
+
|
|
20
20
|
def initialize(url_file_or_html, options = {})
|
|
21
21
|
@source = Source.new(url_file_or_html)
|
|
22
|
-
|
|
22
|
+
|
|
23
23
|
@stylesheets = []
|
|
24
24
|
|
|
25
25
|
@options = PDFKit.configuration.default_options.merge(options)
|
|
26
26
|
@options.merge! find_options_in_meta(url_file_or_html) unless source.url?
|
|
27
27
|
@options = normalize_options(@options)
|
|
28
|
-
|
|
28
|
+
|
|
29
29
|
raise NoExecutableError.new unless File.exists?(PDFKit.configuration.wkhtmltopdf)
|
|
30
30
|
end
|
|
31
|
-
|
|
32
|
-
def command
|
|
31
|
+
|
|
32
|
+
def command(path = nil)
|
|
33
33
|
args = [executable]
|
|
34
34
|
args += @options.to_a.flatten.compact
|
|
35
35
|
args << '--quiet'
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
if @source.html?
|
|
38
38
|
args << '-' # Get HTML from stdin
|
|
39
39
|
else
|
|
40
40
|
args << @source.to_s
|
|
41
41
|
end
|
|
42
|
-
|
|
43
|
-
args << '-' #
|
|
44
|
-
|
|
42
|
+
|
|
43
|
+
args << (path || '-') # Write to file or stdout
|
|
44
|
+
|
|
45
|
+
args.map {|arg| %Q{"#{arg.gsub('"', '\"')}"}}
|
|
45
46
|
end
|
|
46
47
|
|
|
47
48
|
def executable
|
|
@@ -53,62 +54,62 @@ class PDFKit
|
|
|
53
54
|
default.split('/').last
|
|
54
55
|
end
|
|
55
56
|
end
|
|
56
|
-
|
|
57
|
-
def to_pdf
|
|
57
|
+
|
|
58
|
+
def to_pdf(path=nil)
|
|
58
59
|
append_stylesheets
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
pdf
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
60
|
+
|
|
61
|
+
args = command(path)
|
|
62
|
+
invoke = args.join(' ')
|
|
63
|
+
|
|
64
|
+
result = IO.popen(invoke, "wb+") do |pdf|
|
|
65
|
+
pdf.puts(@source.to_s) if @source.html?
|
|
66
|
+
pdf.close_write
|
|
67
|
+
pdf.gets(nil)
|
|
68
|
+
end
|
|
69
|
+
result = File.read(path) if path
|
|
70
|
+
|
|
71
|
+
raise "command failed: #{invoke}" if result.to_s.strip.empty?
|
|
68
72
|
return result
|
|
69
73
|
end
|
|
70
|
-
|
|
74
|
+
|
|
71
75
|
def to_file(path)
|
|
72
|
-
|
|
76
|
+
self.to_pdf(path)
|
|
77
|
+
File.new(path)
|
|
73
78
|
end
|
|
74
|
-
|
|
79
|
+
|
|
75
80
|
protected
|
|
76
81
|
|
|
77
|
-
def find_options_in_meta(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
found.merge(name => tag.attributes["content"])
|
|
81
|
-
end
|
|
82
|
-
end
|
|
82
|
+
def find_options_in_meta(content)
|
|
83
|
+
# Read file if content is a File
|
|
84
|
+
content = content.read if content.is_a?(File)
|
|
83
85
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
found = {}
|
|
87
|
+
content.scan(/<meta [^>]*>/) do |meta|
|
|
88
|
+
if meta.match(/name=["']#{PDFKit.configuration.meta_tag_prefix}/)
|
|
89
|
+
name = meta.scan(/name=["']#{PDFKit.configuration.meta_tag_prefix}([^"']*)/)[0][0]
|
|
90
|
+
found[name.to_sym] = meta.scan(/content=["']([^"']*)/)[0][0]
|
|
91
|
+
end
|
|
90
92
|
end
|
|
93
|
+
|
|
91
94
|
found
|
|
92
|
-
rescue # rexml random crash on invalid xml
|
|
93
|
-
[]
|
|
94
95
|
end
|
|
95
|
-
|
|
96
|
+
|
|
96
97
|
def style_tag_for(stylesheet)
|
|
97
98
|
"<style>#{File.read(stylesheet)}</style>"
|
|
98
99
|
end
|
|
99
|
-
|
|
100
|
+
|
|
100
101
|
def append_stylesheets
|
|
101
102
|
raise ImproperSourceError.new('Stylesheets may only be added to an HTML source') if stylesheets.any? && !@source.html?
|
|
102
|
-
|
|
103
|
+
|
|
103
104
|
stylesheets.each do |stylesheet|
|
|
104
105
|
if @source.to_s.match(/<\/head>/)
|
|
105
|
-
@source.to_s.gsub
|
|
106
|
+
@source = Source.new(@source.to_s.gsub(/(<\/head>)/, style_tag_for(stylesheet)+'\1'))
|
|
106
107
|
else
|
|
107
108
|
@source.to_s.insert(0, style_tag_for(stylesheet))
|
|
108
109
|
end
|
|
109
110
|
end
|
|
110
111
|
end
|
|
111
|
-
|
|
112
|
+
|
|
112
113
|
def normalize_options(options)
|
|
113
114
|
normalized_options = {}
|
|
114
115
|
|
|
@@ -119,11 +120,11 @@ class PDFKit
|
|
|
119
120
|
end
|
|
120
121
|
normalized_options
|
|
121
122
|
end
|
|
122
|
-
|
|
123
|
+
|
|
123
124
|
def normalize_arg(arg)
|
|
124
125
|
arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
|
|
125
126
|
end
|
|
126
|
-
|
|
127
|
+
|
|
127
128
|
def normalize_value(value)
|
|
128
129
|
case value
|
|
129
130
|
when TrueClass
|
|
@@ -132,5 +133,5 @@ class PDFKit
|
|
|
132
133
|
value.to_s
|
|
133
134
|
end
|
|
134
135
|
end
|
|
135
|
-
|
|
136
|
-
end
|
|
136
|
+
|
|
137
|
+
end
|
data/lib/pdfkit/source.rb
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
class PDFKit
|
|
2
|
-
|
|
3
2
|
class Source
|
|
4
|
-
|
|
5
3
|
def initialize(url_file_or_html)
|
|
6
4
|
@source = url_file_or_html
|
|
7
5
|
end
|
|
8
6
|
|
|
9
7
|
def url?
|
|
10
|
-
@source.is_a?(String) && @source.match(
|
|
8
|
+
@source.is_a?(String) && @source.match(/\Ahttp/)
|
|
11
9
|
end
|
|
12
10
|
|
|
13
11
|
def file?
|
|
@@ -21,7 +19,5 @@ class PDFKit
|
|
|
21
19
|
def to_s
|
|
22
20
|
file? ? @source.path : @source
|
|
23
21
|
end
|
|
24
|
-
|
|
25
22
|
end
|
|
26
|
-
|
|
27
|
-
end
|
|
23
|
+
end
|