pdfkit 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of pdfkit might be problematic. Click here for more details.
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/lib/pdfkit/middleware.rb +9 -15
- data/pdfkit.gemspec +7 -4
- data/spec/PDFKit_spec.rb +2 -2
- data/spec/middleware_spec.rb +22 -0
- data/spec/pdfkit_spec.rb +2 -2
- data/spec/source_spec.rb +2 -2
- data/spec/spec_helper.rb +6 -4
- metadata +26 -7
- data/spec/spec.opts +0 -1
data/Rakefile
CHANGED
@@ -12,6 +12,7 @@ begin
|
|
12
12
|
gem.authors = ["jdpace"]
|
13
13
|
gem.add_dependency "activesupport"
|
14
14
|
gem.add_development_dependency "rspec", "~> 2.0.0.beta.8"
|
15
|
+
gem.add_development_dependency "rspec-core", "~> 2.0.0.beta.8"
|
15
16
|
gem.add_development_dependency 'mocha'
|
16
17
|
gem.files = [
|
17
18
|
".document",
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.2
|
data/lib/pdfkit/middleware.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
class PDFKit
|
2
2
|
|
3
|
-
# A rack middleware for validating HTML via w3c validator
|
4
3
|
class Middleware
|
5
4
|
|
6
5
|
def initialize(app, options = {})
|
@@ -12,9 +11,9 @@ class PDFKit
|
|
12
11
|
@render_pdf = false
|
13
12
|
set_request_to_render_as_pdf(env) if env['PATH_INFO'].match(/\.pdf$/)
|
14
13
|
|
15
|
-
status, headers, response = @app.call(
|
14
|
+
status, headers, response = @app.call(env)
|
16
15
|
|
17
|
-
request = Rack::Request.new(
|
16
|
+
request = Rack::Request.new(env)
|
18
17
|
if @render_pdf && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
|
19
18
|
body = response.body
|
20
19
|
|
@@ -28,7 +27,7 @@ class PDFKit
|
|
28
27
|
headers.delete('ETag')
|
29
28
|
headers.delete('Cache-Control')
|
30
29
|
|
31
|
-
headers["Content-Length"] = body.
|
30
|
+
headers["Content-Length"] = body.bytes.to_a.size.to_s
|
32
31
|
headers["Content-Type"] = "application/pdf"
|
33
32
|
|
34
33
|
response = [body]
|
@@ -39,17 +38,12 @@ class PDFKit
|
|
39
38
|
|
40
39
|
private
|
41
40
|
|
41
|
+
# Change relative paths to absolute
|
42
42
|
def translate_paths(body, env)
|
43
|
-
#
|
44
|
-
|
45
|
-
uri += '/' unless uri.match(/\/$/)
|
46
|
-
root = env['rack.url_scheme'] + "://" + env['HTTP_HOST']
|
43
|
+
# Host with protocol
|
44
|
+
root = env['rack.url_scheme'] + "://" + env['HTTP_HOST'] + "/"
|
47
45
|
|
48
|
-
|
49
|
-
body.gsub!(/(href|src)=['"]([^\/][^\"']*)['"]/,'\1="'+root+'/\2"')
|
50
|
-
|
51
|
-
# translate absolute urls
|
52
|
-
body.gsub!(/(href|src)=['"]\/([^\"]*|[^']*)['"]/,'\1="'+uri+'\2"')
|
46
|
+
body.gsub!(/(href|src)=['"]\/([^\"']*|[^"']*)['"]/,'\1="'+root+'\2"')
|
53
47
|
end
|
54
48
|
|
55
49
|
def set_request_to_render_as_pdf(env)
|
@@ -58,7 +52,7 @@ class PDFKit
|
|
58
52
|
|
59
53
|
path = Pathname(env['PATH_INFO'])
|
60
54
|
env['PATH_INFO'] = path.to_s.sub(/#{path.extname}$/,'') if path.extname == '.pdf'
|
61
|
-
env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('html'))
|
55
|
+
env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
|
62
56
|
end
|
63
57
|
|
64
58
|
def concat(accepts, type)
|
@@ -66,4 +60,4 @@ class PDFKit
|
|
66
60
|
end
|
67
61
|
|
68
62
|
end
|
69
|
-
end
|
63
|
+
end
|
data/pdfkit.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{pdfkit}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["jdpace"]
|
12
|
-
s.date = %q{2010-06-
|
12
|
+
s.date = %q{2010-06-18}
|
13
13
|
s.description = %q{Uses wkhtmltopdf to create PDFs using HTML}
|
14
14
|
s.email = %q{jared@codewordstudios.com}
|
15
15
|
s.executables = ["wkhtmltopdf-linux-i386-0-9-9", "wkhtmltopdf-osx-i386-0-9-9", "wkhtmltopdf-proxy"]
|
@@ -36,7 +36,6 @@ Gem::Specification.new do |s|
|
|
36
36
|
"spec/fixtures/example.html",
|
37
37
|
"spec/pdfkit_spec.rb",
|
38
38
|
"spec/source_spec.rb",
|
39
|
-
"spec/spec.opts",
|
40
39
|
"spec/spec_helper.rb"
|
41
40
|
]
|
42
41
|
s.homepage = %q{http://github.com/jdpace/PDFKit}
|
@@ -45,7 +44,8 @@ Gem::Specification.new do |s|
|
|
45
44
|
s.rubygems_version = %q{1.3.7}
|
46
45
|
s.summary = %q{HTML+CSS -> PDF}
|
47
46
|
s.test_files = [
|
48
|
-
"spec/
|
47
|
+
"spec/middleware_spec.rb",
|
48
|
+
"spec/PDFKit_spec.rb",
|
49
49
|
"spec/source_spec.rb",
|
50
50
|
"spec/spec_helper.rb"
|
51
51
|
]
|
@@ -57,15 +57,18 @@ Gem::Specification.new do |s|
|
|
57
57
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
58
58
|
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
|
59
59
|
s.add_development_dependency(%q<rspec>, ["~> 2.0.0.beta.8"])
|
60
|
+
s.add_development_dependency(%q<rspec-core>, ["~> 2.0.0.beta.8"])
|
60
61
|
s.add_development_dependency(%q<mocha>, [">= 0"])
|
61
62
|
else
|
62
63
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
63
64
|
s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.8"])
|
65
|
+
s.add_dependency(%q<rspec-core>, ["~> 2.0.0.beta.8"])
|
64
66
|
s.add_dependency(%q<mocha>, [">= 0"])
|
65
67
|
end
|
66
68
|
else
|
67
69
|
s.add_dependency(%q<activesupport>, [">= 0"])
|
68
70
|
s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.8"])
|
71
|
+
s.add_dependency(%q<rspec-core>, ["~> 2.0.0.beta.8"])
|
69
72
|
s.add_dependency(%q<mocha>, [">= 0"])
|
70
73
|
end
|
71
74
|
end
|
data/spec/PDFKit_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe PDFKit do
|
4
4
|
|
@@ -68,7 +68,7 @@ describe PDFKit do
|
|
68
68
|
it "should generate a PDF of the HTML" do
|
69
69
|
pdfkit = PDFKit.new('html', :page_size => 'Letter')
|
70
70
|
pdf = pdfkit.to_pdf
|
71
|
-
pdf.should
|
71
|
+
pdf[0...4].should == "%PDF" # PDF Signature at beginning of file
|
72
72
|
end
|
73
73
|
|
74
74
|
it "should have the stylesheet added to the head if it has one" do
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PDFKit::Middleware do
|
4
|
+
describe "#translate_paths" do
|
5
|
+
before do
|
6
|
+
@pdf = PDFKit::Middleware.new({})
|
7
|
+
@env = {'REQUEST_URI' => 'http://example.com/document.pdf', 'rack.url_scheme' => 'http', 'HTTP_HOST' => 'example.com'}
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should correctly parse relative url with single quotes" do
|
11
|
+
@body = %{<html><head><link href='/stylesheets/application.css' media='screen' rel='stylesheet' type='text/css' /></head><body><img alt='test' src='/test.png' /></body></html>}
|
12
|
+
body = @pdf.send :translate_paths, @body, @env
|
13
|
+
body.should == "<html><head><link href=\"http://example.com/stylesheets/application.css\" media='screen' rel='stylesheet' type='text/css' /></head><body><img alt='test' src=\"http://example.com/test.png\" /></body></html>"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should correctly parse relative url with double quotes" do
|
17
|
+
@body = %{<link href="/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />}
|
18
|
+
body = @pdf.send :translate_paths, @body, @env
|
19
|
+
body.should == "<link href=\"http://example.com/stylesheets/application.css\" media=\"screen\" rel=\"stylesheet\" type=\"text/css\" />"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/spec/pdfkit_spec.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
describe PDFKit do
|
4
4
|
|
@@ -68,7 +68,7 @@ describe PDFKit do
|
|
68
68
|
it "should generate a PDF of the HTML" do
|
69
69
|
pdfkit = PDFKit.new('html', :page_size => 'Letter')
|
70
70
|
pdf = pdfkit.to_pdf
|
71
|
-
pdf.should
|
71
|
+
pdf[0...4].should == "%PDF" # PDF Signature at beginning of file
|
72
72
|
end
|
73
73
|
|
74
74
|
it "should have the stylesheet added to the head if it has one" do
|
data/spec/source_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -2,14 +2,16 @@ SPEC_ROOT = File.dirname(__FILE__)
|
|
2
2
|
$LOAD_PATH.unshift(SPEC_ROOT)
|
3
3
|
$LOAD_PATH.unshift(File.join(SPEC_ROOT, '..', 'lib'))
|
4
4
|
require 'pdfkit'
|
5
|
-
require '
|
6
|
-
require '
|
5
|
+
require 'rspec'
|
6
|
+
require 'rspec/autorun'
|
7
7
|
require 'mocha'
|
8
8
|
|
9
|
-
|
9
|
+
RSpec.configure do |config|
|
10
10
|
|
11
11
|
config.before do
|
12
|
-
PDFKit.any_instance.stubs(:wkhtmltopdf).returns(
|
12
|
+
PDFKit.any_instance.stubs(:wkhtmltopdf).returns(
|
13
|
+
File.join(SPEC_ROOT,'..','bin','wkhtmltopdf-proxy')
|
14
|
+
)
|
13
15
|
end
|
14
16
|
|
15
17
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pdfkit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 2
|
10
|
+
version: 0.3.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- jdpace
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-06-
|
18
|
+
date: 2010-06-18 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -51,9 +51,27 @@ dependencies:
|
|
51
51
|
type: :development
|
52
52
|
version_requirements: *id002
|
53
53
|
- !ruby/object:Gem::Dependency
|
54
|
-
name:
|
54
|
+
name: rspec-core
|
55
55
|
prerelease: false
|
56
56
|
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 62196467
|
62
|
+
segments:
|
63
|
+
- 2
|
64
|
+
- 0
|
65
|
+
- 0
|
66
|
+
- beta
|
67
|
+
- 8
|
68
|
+
version: 2.0.0.beta.8
|
69
|
+
type: :development
|
70
|
+
version_requirements: *id003
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: mocha
|
73
|
+
prerelease: false
|
74
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
57
75
|
none: false
|
58
76
|
requirements:
|
59
77
|
- - ">="
|
@@ -63,7 +81,7 @@ dependencies:
|
|
63
81
|
- 0
|
64
82
|
version: "0"
|
65
83
|
type: :development
|
66
|
-
version_requirements: *
|
84
|
+
version_requirements: *id004
|
67
85
|
description: Uses wkhtmltopdf to create PDFs using HTML
|
68
86
|
email: jared@codewordstudios.com
|
69
87
|
executables:
|
@@ -94,8 +112,8 @@ files:
|
|
94
112
|
- spec/fixtures/example.html
|
95
113
|
- spec/pdfkit_spec.rb
|
96
114
|
- spec/source_spec.rb
|
97
|
-
- spec/spec.opts
|
98
115
|
- spec/spec_helper.rb
|
116
|
+
- spec/middleware_spec.rb
|
99
117
|
- spec/PDFKit_spec.rb
|
100
118
|
has_rdoc: true
|
101
119
|
homepage: http://github.com/jdpace/PDFKit
|
@@ -132,6 +150,7 @@ signing_key:
|
|
132
150
|
specification_version: 3
|
133
151
|
summary: HTML+CSS -> PDF
|
134
152
|
test_files:
|
153
|
+
- spec/middleware_spec.rb
|
135
154
|
- spec/PDFKit_spec.rb
|
136
155
|
- spec/source_spec.rb
|
137
156
|
- spec/spec_helper.rb
|
data/spec/spec.opts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color
|