hssc_pdfkit 0.5.4
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/.document +5 -0
- data/.gitignore +23 -0
- data/.rspec +1 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +36 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +40 -0
- data/LICENSE +20 -0
- data/POST_INSTALL +14 -0
- data/README.md +146 -0
- data/Rakefile +24 -0
- data/lib/pdfkit/configuration.rb +43 -0
- data/lib/pdfkit/middleware.rb +94 -0
- data/lib/pdfkit/pdfkit.rb +143 -0
- data/lib/pdfkit/source.rb +23 -0
- data/lib/pdfkit/version.rb +3 -0
- data/lib/pdfkit.rb +4 -0
- data/pdfkit.gemspec +30 -0
- data/spec/fixtures/example.css +1 -0
- data/spec/fixtures/example.html +5 -0
- data/spec/middleware_spec.rb +314 -0
- data/spec/pdfkit_spec.rb +255 -0
- data/spec/source_spec.rb +78 -0
- data/spec/spec_helper.rb +15 -0
- metadata +189 -0
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
## MAC OS
|
2
|
+
.DS_Store
|
3
|
+
|
4
|
+
## TEXTMATE
|
5
|
+
*.tmproj
|
6
|
+
tmtags
|
7
|
+
|
8
|
+
## EMACS
|
9
|
+
*~
|
10
|
+
\#*
|
11
|
+
.\#*
|
12
|
+
|
13
|
+
## VIM
|
14
|
+
*.swp
|
15
|
+
|
16
|
+
## PROJECT::GENERAL
|
17
|
+
coverage
|
18
|
+
rdoc
|
19
|
+
pkg
|
20
|
+
|
21
|
+
## PROJECT::SPECIFIC
|
22
|
+
.bundle
|
23
|
+
spec/custom_wkhtmltopdf_path.rb
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
2013-06-12
|
2
|
+
==================
|
3
|
+
* Bump to 0.5.4
|
4
|
+
* Fix broken page numbers (https://github.com/pdfkit/pdfkit/pull/181)
|
5
|
+
|
6
|
+
2013-02-21
|
7
|
+
==================
|
8
|
+
* Bump to 0.5.3
|
9
|
+
* Fix security vulnerability due to unsanitized strings being passed to `wkhtmltopdf` (https://github.com/pdfkit/pdfkit/issues/164)
|
10
|
+
|
11
|
+
2011-07-02
|
12
|
+
==================
|
13
|
+
* Bump to 0.5.2
|
14
|
+
* Fix of dealing with ActiveSupport::SafeBuffer >= 3.0.8.
|
15
|
+
* Fix for meta tag options getting dropped in REE 1.8.7.
|
16
|
+
* Fix on bundler environment detection.
|
17
|
+
|
18
|
+
2011-06-17
|
19
|
+
==================
|
20
|
+
* Bump to 0.5.1
|
21
|
+
* Fix for response body coming through as an array.
|
22
|
+
* Added root_url configuration for setup where a host my not know its own name.
|
23
|
+
* Awareness of Bundler when looking for the wkhtmltopdf executable.
|
24
|
+
* Fix for file data getting truncated in Ruby 1.8.6
|
25
|
+
* Fix for 0.5.0 release getting stuck rendering all requests as PDFs.
|
26
|
+
* More robust meta tag detection.
|
27
|
+
|
28
|
+
2010-12-27
|
29
|
+
==================
|
30
|
+
* Bump to 0.5.0
|
31
|
+
* Switched to popen - adds support for JRuby and Windows
|
32
|
+
* Pulled in support for pdf rendering conditions in middleware via Rémy Coutable
|
33
|
+
* Use `which` to try and determine path to wkhtmltopdf
|
34
|
+
* Removed wkhtmltopdf auto installer
|
35
|
+
* Changed :disable\_smart\_shrinking to false for default options.
|
36
|
+
* Added History.md
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pdfkit (0.5.4)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activesupport (3.0.9)
|
10
|
+
diff-lcs (1.1.2)
|
11
|
+
json (1.7.7)
|
12
|
+
mocha (0.9.12)
|
13
|
+
rack (1.3.0)
|
14
|
+
rack-test (0.6.0)
|
15
|
+
rack (>= 1.0)
|
16
|
+
rake (0.9.2)
|
17
|
+
rdoc (4.0.1)
|
18
|
+
json (~> 1.4)
|
19
|
+
rspec (2.2.0)
|
20
|
+
rspec-core (~> 2.2)
|
21
|
+
rspec-expectations (~> 2.2)
|
22
|
+
rspec-mocks (~> 2.2)
|
23
|
+
rspec-core (2.6.4)
|
24
|
+
rspec-expectations (2.6.0)
|
25
|
+
diff-lcs (~> 1.1.2)
|
26
|
+
rspec-mocks (2.6.0)
|
27
|
+
wkhtmltopdf-binary (0.9.5.3)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
activesupport (>= 3.0.8)
|
34
|
+
mocha (>= 0.9.10)
|
35
|
+
pdfkit!
|
36
|
+
rack-test (>= 0.5.6)
|
37
|
+
rake (~> 0.9.2)
|
38
|
+
rdoc (~> 4.0.1)
|
39
|
+
rspec (~> 2.2.0)
|
40
|
+
wkhtmltopdf-binary (~> 0.9.5)
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 jdpace
|
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/POST_INSTALL
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
******************************************************************
|
2
|
+
|
3
|
+
Install wkhtmltopdf:
|
4
|
+
|
5
|
+
1. Install by hand (recomended):
|
6
|
+
|
7
|
+
https://github.com/pdfkit/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
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
# What makes this version different
|
2
|
+
|
3
|
+
Works with Mac OSX 10.8.4
|
4
|
+
|
5
|
+
# PDFKit
|
6
|
+
|
7
|
+
Create PDFs using plain old HTML+CSS. Uses [wkhtmltopdf](http://github.com/antialize/wkhtmltopdf) on the back-end which renders HTML using Webkit.
|
8
|
+
|
9
|
+
## Install
|
10
|
+
|
11
|
+
### PDFKit
|
12
|
+
```
|
13
|
+
gem install pdfkit
|
14
|
+
```
|
15
|
+
### wkhtmltopdf
|
16
|
+
|
17
|
+
1. Install by hand (recommended):
|
18
|
+
|
19
|
+
<https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF>
|
20
|
+
|
21
|
+
2. Try using the wkhtmltopdf-binary gem (mac + linux i386)
|
22
|
+
```
|
23
|
+
gem install wkhtmltopdf-binary
|
24
|
+
```
|
25
|
+
*Note:* The automated installer has been removed.
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
```ruby
|
29
|
+
# PDFKit.new takes the HTML and any options for wkhtmltopdf
|
30
|
+
# run `wkhtmltopdf --extended-help` for a full list of options
|
31
|
+
kit = PDFKit.new(html, :page_size => 'Letter')
|
32
|
+
kit.stylesheets << '/path/to/css/file'
|
33
|
+
|
34
|
+
# Get an inline PDF
|
35
|
+
pdf = kit.to_pdf
|
36
|
+
|
37
|
+
# Save the PDF to a file
|
38
|
+
file = kit.to_file('/path/to/save/pdf')
|
39
|
+
|
40
|
+
# PDFKit.new can optionally accept a URL or a File.
|
41
|
+
# Stylesheets can not be added when source is provided as a URL of File.
|
42
|
+
kit = PDFKit.new('http://google.com')
|
43
|
+
kit = PDFKit.new(File.new('/path/to/html'))
|
44
|
+
|
45
|
+
# Add any kind of option through meta tags
|
46
|
+
PDFKit.new('<html><head><meta name="pdfkit-page_size" content="Letter"')
|
47
|
+
```
|
48
|
+
## Configuration
|
49
|
+
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:
|
50
|
+
```ruby
|
51
|
+
# config/initializers/pdfkit.rb
|
52
|
+
PDFKit.configure do |config|
|
53
|
+
# config.wkhtmltopdf = '/path/to/wkhtmltopdf'
|
54
|
+
# config.default_options = {
|
55
|
+
# :page_size => 'Legal',
|
56
|
+
# :print_media_type => true
|
57
|
+
# }
|
58
|
+
# config.root_url = "http://localhost" # Use only if your external hostname is unavailable on the server.
|
59
|
+
end
|
60
|
+
```
|
61
|
+
## Middleware
|
62
|
+
PDFKit comes with a middleware that allows users to get a PDF view of any page on your site by appending .pdf to the URL.
|
63
|
+
|
64
|
+
### Middleware Setup
|
65
|
+
**Non-Rails Rack apps**
|
66
|
+
```ruby
|
67
|
+
# in config.ru
|
68
|
+
require 'pdfkit'
|
69
|
+
use PDFKit::Middleware
|
70
|
+
```
|
71
|
+
**Rails apps**
|
72
|
+
```ruby
|
73
|
+
# in application.rb(Rails3) or environment.rb(Rails2)
|
74
|
+
require 'pdfkit'
|
75
|
+
config.middleware.use PDFKit::Middleware
|
76
|
+
```
|
77
|
+
**With PDFKit options**
|
78
|
+
```ruby
|
79
|
+
# options will be passed to PDFKit.new
|
80
|
+
config.middleware.use PDFKit::Middleware, :print_media_type => true
|
81
|
+
```
|
82
|
+
**With conditions to limit routes that can be generated in pdf**
|
83
|
+
```ruby
|
84
|
+
# conditions can be regexps (either one or an array)
|
85
|
+
config.middleware.use PDFKit::Middleware, {}, :only => %r[^/public]
|
86
|
+
config.middleware.use PDFKit::Middleware, {}, :only => [%r[^/invoice], %r[^/public]]
|
87
|
+
|
88
|
+
# conditions can be strings (either one or an array)
|
89
|
+
config.middleware.use PDFKit::Middleware, {}, :only => '/public'
|
90
|
+
config.middleware.use PDFKit::Middleware, {}, :only => ['/invoice', '/public']
|
91
|
+
|
92
|
+
# conditions can be regexps (either one or an array)
|
93
|
+
config.middleware.use PDFKit::Middleware, {}, :except => [%r[^/prawn], %r[^/secret]]
|
94
|
+
|
95
|
+
# conditions can be strings (either one or an array)
|
96
|
+
config.middleware.use PDFKit::Middleware, {}, :except => ['/secret']
|
97
|
+
```
|
98
|
+
## Troubleshooting
|
99
|
+
|
100
|
+
* **Single thread issue:** In development environments it is common to run a
|
101
|
+
single server process. This can cause issues when rendering your pdf
|
102
|
+
requires wkhtmltopdf to hit your server again (for images, js, css).
|
103
|
+
This is because the resource requests will get blocked by the initial
|
104
|
+
request and the initial request will be waiting on the resource
|
105
|
+
requests causing a deadlock.
|
106
|
+
|
107
|
+
This is usually not an issue in a production environment. To get
|
108
|
+
around this issue you may want to run a server with multiple workers
|
109
|
+
like Passenger or try to embed your resources within your HTML to
|
110
|
+
avoid extra HTTP requests.
|
111
|
+
|
112
|
+
Example solution (rails / bundler), add unicorn to the development
|
113
|
+
group in your Gemfile `gem 'unicorn'` then run `bundle`. Next, add a
|
114
|
+
file `config/unicorn.conf` with
|
115
|
+
|
116
|
+
worker_processes 3
|
117
|
+
|
118
|
+
Then to run the app `unicorn_rails -c config/unicorn.conf` (from rails_root)
|
119
|
+
|
120
|
+
* **Resources aren't included in the PDF:** Images, CSS, or JavaScript
|
121
|
+
does not seem to be downloading correctly in the PDF. This is due
|
122
|
+
to the fact that wkhtmltopdf does not know where to find those files.
|
123
|
+
Make sure you are using absolute paths (start with forward slash) to
|
124
|
+
your resources. If you are using PDFKit to generate PDFs from a raw
|
125
|
+
HTML source make sure you use complete paths (either file paths or
|
126
|
+
urls including the domain). In restrictive server environments the
|
127
|
+
root_url configuration may be what you are looking for change your
|
128
|
+
asset host.
|
129
|
+
|
130
|
+
* **Mangled output in the browser:** Be sure that your HTTP response
|
131
|
+
headers specify "Content-Type: application/pdf"
|
132
|
+
|
133
|
+
## Note on Patches/Pull Requests
|
134
|
+
|
135
|
+
* Fork the project.
|
136
|
+
* Setup your development environment with: gem install bundler; bundle install
|
137
|
+
* Make your feature addition or bug fix.
|
138
|
+
* Add tests for it. This is important so I don't break it in a
|
139
|
+
future version unintentionally.
|
140
|
+
* Commit, do not mess with rakefile, version, or history.
|
141
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
142
|
+
* Send me a pull request. Bonus points for topic branches.
|
143
|
+
|
144
|
+
## Copyright
|
145
|
+
|
146
|
+
Copyright (c) 2010 Jared Pace. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'bundler'
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
8
|
+
end
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
11
|
+
spec.rcov = true
|
12
|
+
end
|
13
|
+
|
14
|
+
task :default => :spec
|
15
|
+
|
16
|
+
require 'rdoc/task'
|
17
|
+
RDoc::Task.new do |rdoc|
|
18
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
19
|
+
|
20
|
+
rdoc.rdoc_dir = 'rdoc'
|
21
|
+
rdoc.title = "PDFKit #{version}"
|
22
|
+
rdoc.rdoc_files.include('README*')
|
23
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
24
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class PDFKit
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :meta_tag_prefix, :default_options, :root_url
|
4
|
+
attr_writer :wkhtmltopdf
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@meta_tag_prefix = 'pdfkit-'
|
8
|
+
@default_options = {
|
9
|
+
:disable_smart_shrinking => false,
|
10
|
+
:page_size => 'Letter',
|
11
|
+
:margin_top => '0.75in',
|
12
|
+
:margin_right => '0.75in',
|
13
|
+
:margin_bottom => '0.75in',
|
14
|
+
:margin_left => '0.75in',
|
15
|
+
:encoding => "UTF-8"
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def wkhtmltopdf
|
20
|
+
@wkhtmltopdf ||= (defined?(Bundler::GemfileError) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class << self
|
25
|
+
attr_accessor :configuration
|
26
|
+
end
|
27
|
+
|
28
|
+
# Configure PDFKit someplace sensible,
|
29
|
+
# like config/initializers/pdfkit.rb
|
30
|
+
#
|
31
|
+
# @example
|
32
|
+
# PDFKit.configure do |config|
|
33
|
+
# config.wkhtmltopdf = '/usr/bin/wkhtmltopdf'
|
34
|
+
# end
|
35
|
+
|
36
|
+
def self.configuration
|
37
|
+
@configuration ||= Configuration.new
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.configure
|
41
|
+
yield(configuration)
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
class PDFKit
|
2
|
+
|
3
|
+
class Middleware
|
4
|
+
|
5
|
+
def initialize(app, options = {}, conditions = {})
|
6
|
+
@app = app
|
7
|
+
@options = options
|
8
|
+
@conditions = conditions
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
@request = Rack::Request.new(env)
|
13
|
+
@render_pdf = false
|
14
|
+
|
15
|
+
set_request_to_render_as_pdf(env) if render_as_pdf?
|
16
|
+
status, headers, response = @app.call(env)
|
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
|
+
|
24
|
+
# Do not cache PDFs
|
25
|
+
headers.delete('ETag')
|
26
|
+
headers.delete('Cache-Control')
|
27
|
+
|
28
|
+
headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
|
29
|
+
headers["Content-Type"] = "application/pdf"
|
30
|
+
end
|
31
|
+
|
32
|
+
[status, headers, response]
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
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
|
+
elsif request_path_is_pdf && @conditions[:except]
|
62
|
+
rules = [@conditions[:except]].flatten
|
63
|
+
rules.map do |pattern|
|
64
|
+
if pattern.is_a?(Regexp)
|
65
|
+
return false if @request.path =~ pattern
|
66
|
+
else
|
67
|
+
return false if @request.path[0, pattern.length] == pattern
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
return true
|
72
|
+
else
|
73
|
+
request_path_is_pdf
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def set_request_to_render_as_pdf(env)
|
78
|
+
@render_pdf = true
|
79
|
+
|
80
|
+
path = @request.path.sub(%r{\.pdf$}, '')
|
81
|
+
path = path.sub(@request.script_name, '')
|
82
|
+
|
83
|
+
%w[PATH_INFO REQUEST_URI].each { |e| env[e] = path }
|
84
|
+
|
85
|
+
env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
|
86
|
+
env["Rack-Middleware-PDFKit"] = "true"
|
87
|
+
end
|
88
|
+
|
89
|
+
def concat(accepts, type)
|
90
|
+
(accepts || '').split(',').unshift(type).compact.join(',')
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'shellwords'
|
2
|
+
|
3
|
+
class PDFKit
|
4
|
+
|
5
|
+
class NoExecutableError < StandardError
|
6
|
+
def initialize
|
7
|
+
msg = "No wkhtmltopdf executable found at #{PDFKit.configuration.wkhtmltopdf}\n"
|
8
|
+
msg << ">> Please install wkhtmltopdf - https://github.com/pdfkit/PDFKit/wiki/Installing-WKHTMLTOPDF"
|
9
|
+
super(msg)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class ImproperSourceError < StandardError
|
14
|
+
def initialize(msg)
|
15
|
+
super("Improper Source: #{msg}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_accessor :source, :stylesheets
|
20
|
+
attr_reader :options
|
21
|
+
|
22
|
+
def initialize(url_file_or_html, options = {})
|
23
|
+
@source = Source.new(url_file_or_html)
|
24
|
+
|
25
|
+
@stylesheets = []
|
26
|
+
|
27
|
+
@options = PDFKit.configuration.default_options.merge(options)
|
28
|
+
@options.merge! find_options_in_meta(url_file_or_html) unless source.url?
|
29
|
+
@options = normalize_options(@options)
|
30
|
+
|
31
|
+
raise NoExecutableError.new unless File.exists?(PDFKit.configuration.wkhtmltopdf)
|
32
|
+
end
|
33
|
+
|
34
|
+
def command(path = nil)
|
35
|
+
args = [executable]
|
36
|
+
args += @options.to_a.flatten.compact
|
37
|
+
#args << '--quiet'
|
38
|
+
|
39
|
+
if @source.html?
|
40
|
+
args << '-' # Get HTML from stdin
|
41
|
+
else
|
42
|
+
args << @source.to_s
|
43
|
+
end
|
44
|
+
|
45
|
+
args << (path || '-') # Write to file or stdout
|
46
|
+
|
47
|
+
args.shelljoin
|
48
|
+
end
|
49
|
+
|
50
|
+
def executable
|
51
|
+
default = PDFKit.configuration.wkhtmltopdf
|
52
|
+
return default if default !~ /^\// # its not a path, so nothing we can do
|
53
|
+
if File.exist?(default)
|
54
|
+
default
|
55
|
+
else
|
56
|
+
default.split('/').last
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_pdf(path=nil)
|
61
|
+
append_stylesheets
|
62
|
+
|
63
|
+
invoke = command(path)
|
64
|
+
|
65
|
+
result = IO.popen(invoke, "wb+") do |pdf|
|
66
|
+
pdf.puts(@source.to_s) if @source.html?
|
67
|
+
pdf.close_write
|
68
|
+
pdf.gets(nil)
|
69
|
+
end
|
70
|
+
result = File.read(path) if path
|
71
|
+
|
72
|
+
# $? is thread safe per http://stackoverflow.com/questions/2164887/thread-safe-external-process-in-ruby-plus-checking-exitstatus
|
73
|
+
raise "command failed: #{invoke}" if result.to_s.strip.empty? or !$?.success?
|
74
|
+
return result
|
75
|
+
end
|
76
|
+
|
77
|
+
def to_file(path)
|
78
|
+
self.to_pdf(path)
|
79
|
+
File.new(path)
|
80
|
+
end
|
81
|
+
|
82
|
+
protected
|
83
|
+
|
84
|
+
def find_options_in_meta(content)
|
85
|
+
# Read file if content is a File
|
86
|
+
content = content.read if content.is_a?(File)
|
87
|
+
|
88
|
+
found = {}
|
89
|
+
content.scan(/<meta [^>]*>/) do |meta|
|
90
|
+
if meta.match(/name=["']#{PDFKit.configuration.meta_tag_prefix}/)
|
91
|
+
name = meta.scan(/name=["']#{PDFKit.configuration.meta_tag_prefix}([^"']*)/)[0][0]
|
92
|
+
found[name.to_sym] = meta.scan(/content=["']([^"']*)/)[0][0]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
found
|
97
|
+
end
|
98
|
+
|
99
|
+
def style_tag_for(stylesheet)
|
100
|
+
"<style>#{File.read(stylesheet)}</style>"
|
101
|
+
end
|
102
|
+
|
103
|
+
def append_stylesheets
|
104
|
+
raise ImproperSourceError.new('Stylesheets may only be added to an HTML source') if stylesheets.any? && !@source.html?
|
105
|
+
|
106
|
+
stylesheets.each do |stylesheet|
|
107
|
+
if @source.to_s.match(/<\/head>/)
|
108
|
+
@source = Source.new(@source.to_s.gsub(/(<\/head>)/, style_tag_for(stylesheet)+'\1'))
|
109
|
+
else
|
110
|
+
@source.to_s.insert(0, style_tag_for(stylesheet))
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def normalize_options(options)
|
116
|
+
normalized_options = {}
|
117
|
+
|
118
|
+
options.each do |key, value|
|
119
|
+
next if !value
|
120
|
+
normalized_key = "--#{normalize_arg key}"
|
121
|
+
normalized_options[normalized_key] = normalize_value(value)
|
122
|
+
end
|
123
|
+
normalized_options
|
124
|
+
end
|
125
|
+
|
126
|
+
def normalize_arg(arg)
|
127
|
+
arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
|
128
|
+
end
|
129
|
+
|
130
|
+
def normalize_value(value)
|
131
|
+
case value
|
132
|
+
when TrueClass #ie, ==true, see http://www.ruby-doc.org/core-1.9.3/TrueClass.html
|
133
|
+
nil
|
134
|
+
when Hash
|
135
|
+
value.to_a.flatten.collect{|x| x.to_s}
|
136
|
+
when Array
|
137
|
+
value.flatten.collect{|x| x.to_s}
|
138
|
+
else
|
139
|
+
value.to_s
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class PDFKit
|
2
|
+
class Source
|
3
|
+
def initialize(url_file_or_html)
|
4
|
+
@source = url_file_or_html
|
5
|
+
end
|
6
|
+
|
7
|
+
def url?
|
8
|
+
@source.is_a?(String) && @source.match(/\Ahttp/)
|
9
|
+
end
|
10
|
+
|
11
|
+
def file?
|
12
|
+
@source.kind_of?(File)
|
13
|
+
end
|
14
|
+
|
15
|
+
def html?
|
16
|
+
!(url? || file?)
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s
|
20
|
+
file? ? @source.path : @source
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/pdfkit.rb
ADDED
data/pdfkit.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "pdfkit/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "hssc_pdfkit"
|
7
|
+
s.version = PDFKit::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Andrew Cates", "Jared Pace", "Relevance"]
|
10
|
+
s.email = ["catesa@musc.edu"]
|
11
|
+
s.homepage = "https://github.com/HSSC/pdfkit"
|
12
|
+
s.summary = "HTML+CSS -> PDF"
|
13
|
+
s.description = "Uses wkhtmltopdf to create PDFs using HTML"
|
14
|
+
|
15
|
+
s.rubyforge_project = "pdfkit"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
# Developmnet Dependencies
|
23
|
+
s.add_development_dependency(%q<rake>, ["~>0.9.2"])
|
24
|
+
s.add_development_dependency(%q<rdoc>, ["~> 4.0.1"])
|
25
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.2.0"])
|
26
|
+
s.add_development_dependency(%q<mocha>, [">= 0.9.10"])
|
27
|
+
s.add_development_dependency(%q<rack-test>, [">= 0.5.6"])
|
28
|
+
s.add_development_dependency(%q<activesupport>, [">= 3.0.8"])
|
29
|
+
s.add_development_dependency(%q<wkhtmltopdf-binary>, ["~> 0.9.5"])
|
30
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
body { font-size: 20px; }
|