phantompdf 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +6 -3
- data/lib/phantompdf/generator.rb +1 -1
- data/lib/phantompdf/version.rb +1 -1
- data/phantompdf.gemspec +2 -1
- data/spec/fixtures/google.png +0 -0
- data/spec/fixtures/phantompdf.html +1 -0
- data/spec/phantompdf/generator_spec.rb +59 -61
- data/vendor/assets/javascripts/rasterize.js +36 -38
- metadata +20 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae86097f0600f67b8b4dcce04e387310a0616080
|
4
|
+
data.tar.gz: ffaf226240d70a72325ed280f3c9175eca110cbf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a84c1aff47a454b516861c7eafd346543ff09d11e0bd33b1d9fd088efba218dbc0d4b9736aff92dd50938689d72d9b82616389ca5eea1a36fb8f59b29dc3cda6
|
7
|
+
data.tar.gz: 824fc062d2c2e429192f5ff69679a1cf5ac6e38f25f20aa0f1cf7e3b92417e3dd93ba1fe7bc0695e4dc17995d3f77bee7ab9f547ce38978d9aed2b132c7cd293
|
data/Gemfile.lock
CHANGED
@@ -10,13 +10,14 @@ PATH
|
|
10
10
|
phantompdf (1.1.0)
|
11
11
|
json
|
12
12
|
phantomjs
|
13
|
+
rack
|
13
14
|
|
14
15
|
GEM
|
15
16
|
remote: https://rubygems.org/
|
16
17
|
specs:
|
17
18
|
Ascii85 (1.0.2)
|
18
19
|
afm (0.2.0)
|
19
|
-
byebug (2.
|
20
|
+
byebug (2.3.0)
|
20
21
|
columnize (~> 0.3.6)
|
21
22
|
debugger-linecache (~> 1.2.0)
|
22
23
|
columnize (0.3.6)
|
@@ -30,7 +31,9 @@ GEM
|
|
30
31
|
hashery (~> 2.0)
|
31
32
|
ruby-rc4
|
32
33
|
ttfunk
|
33
|
-
|
34
|
+
rack (1.5.2)
|
35
|
+
rack-test (0.6.2)
|
36
|
+
rack (>= 1.0)
|
34
37
|
rspec (2.14.1)
|
35
38
|
rspec-core (~> 2.14.0)
|
36
39
|
rspec-expectations (~> 2.14.0)
|
@@ -50,5 +53,5 @@ DEPENDENCIES
|
|
50
53
|
pdf-reader
|
51
54
|
phantomjs!
|
52
55
|
phantompdf!
|
53
|
-
|
56
|
+
rack-test
|
54
57
|
rspec
|
data/lib/phantompdf/generator.rb
CHANGED
data/lib/phantompdf/version.rb
CHANGED
data/phantompdf.gemspec
CHANGED
@@ -18,9 +18,10 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = %w(lib)
|
20
20
|
spec.add_runtime_dependency 'phantomjs'
|
21
|
+
spec.add_runtime_dependency 'rack'
|
21
22
|
spec.add_runtime_dependency 'json'
|
22
23
|
|
23
|
-
spec.add_development_dependency '
|
24
|
+
spec.add_development_dependency 'rack-test'
|
24
25
|
spec.add_development_dependency 'rspec'
|
25
26
|
spec.add_development_dependency 'pdf-reader'
|
26
27
|
spec.add_development_dependency 'byebug'
|
Binary file
|
@@ -1,12 +1,16 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
module PhantomPDF
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
fixtures_root = File.expand_path('../../fixtures', __FILE__)
|
5
|
+
|
6
|
+
resource = {
|
7
|
+
url: 'http://www.google.com',
|
8
|
+
file: "#{fixtures_root}/phantompdf.html",
|
9
|
+
html: File.read("#{fixtures_root}/phantompdf.html")
|
10
|
+
}.values.sample
|
8
11
|
|
9
|
-
|
12
|
+
describe Generator do
|
13
|
+
subject { Generator.new(resource) }
|
10
14
|
|
11
15
|
context "attributes" do
|
12
16
|
[:input, :output, :config].each do |rwattr|
|
@@ -21,105 +25,103 @@ module PhantomPDF
|
|
21
25
|
end
|
22
26
|
|
23
27
|
context "#generate" do
|
24
|
-
|
25
|
-
|
26
|
-
{
|
27
|
-
url: 'http://www.google.com',
|
28
|
-
file: "#{fixtures_root}/phantompdf.html",
|
29
|
-
html: File.read("#{fixtures_root}/phantompdf.html")
|
30
|
-
}.each do |key, value|
|
31
|
-
context "with #{key}" do
|
32
|
-
it "should works" do
|
33
|
-
Generator.new(value).generate.should be_pdf_file
|
34
|
-
end
|
35
|
-
end
|
28
|
+
it "should works" do
|
29
|
+
Generator.new(resource).generate.should be_pdf_file
|
36
30
|
end
|
37
31
|
|
38
32
|
context "with output" do
|
39
|
-
before :
|
40
|
-
@
|
41
|
-
|
33
|
+
before :each do
|
34
|
+
@destination = '/tmp/google.pdf'
|
35
|
+
|
36
|
+
File.unlink(@destination) if File.exist?(@destination)
|
42
37
|
end
|
43
38
|
|
44
39
|
after :each do
|
45
|
-
File.
|
40
|
+
File.unlink(@destination) if File.exist?(@destination)
|
46
41
|
end
|
47
42
|
|
48
|
-
it "should generate pdf file following
|
49
|
-
File.exist?(@
|
50
|
-
Generator.new(
|
51
|
-
File.exist?(@
|
43
|
+
it "should generate pdf file following output" do
|
44
|
+
File.exist?(@destination).should be_false
|
45
|
+
Generator.new(resource, @destination).generate
|
46
|
+
File.exist?(@destination).should be_true
|
52
47
|
|
53
|
-
@
|
48
|
+
@destination.should be_pdf_file
|
54
49
|
end
|
55
50
|
|
56
|
-
it "should raise PhantomPDF::DestinationTypeError when
|
51
|
+
it "should raise PhantomPDF::DestinationTypeError when output is not a string" do
|
57
52
|
expect{
|
58
|
-
Generator.new(
|
53
|
+
Generator.new(resource, Object.new)
|
59
54
|
}.to raise_error(PhantomPDF::DestinationTypeError)
|
60
55
|
end
|
61
56
|
|
62
|
-
|
57
|
+
pending "should raise PhantomPDF::DestinationPermitError when output is not writable" do
|
63
58
|
File.stub(:writable?, '/tmp') { false }
|
64
59
|
|
65
60
|
expect{
|
66
|
-
Generator.new(
|
61
|
+
Generator.new(resource, @destination)
|
67
62
|
}.to raise_error(PhantomPDF::DestinationPermitError)
|
68
63
|
end
|
69
64
|
end
|
70
65
|
|
71
|
-
context "with options
|
72
|
-
before :
|
73
|
-
@
|
74
|
-
@
|
75
|
-
|
66
|
+
context "with options" do
|
67
|
+
before :each do
|
68
|
+
@destination = '/tmp/google.pdf'
|
69
|
+
@custom_image = 'http://www.google.com/images/srpr/logo4w.png'
|
70
|
+
|
71
|
+
File.unlink(@destination) if File.exist?(@destination)
|
76
72
|
end
|
77
73
|
|
78
74
|
after :each do
|
79
|
-
File.
|
75
|
+
File.unlink(@destination) if File.exist?(@destination)
|
80
76
|
end
|
81
77
|
|
82
|
-
|
78
|
+
it "should support custom header" do
|
83
79
|
header = 'Hello, PhantomPDF header!'
|
84
80
|
|
85
|
-
Generator.new(
|
81
|
+
Generator.new(resource, @destination, {:header => header}).generate.should be_pdf_file
|
86
82
|
|
87
|
-
|
88
|
-
pdf_content.
|
83
|
+
# we CANNOT reader the file as PDF
|
84
|
+
# pdf_content = PDF::Reader.new(@destination).page(1).text
|
85
|
+
# pdf_content.should include(header)
|
89
86
|
end
|
90
87
|
|
91
88
|
it "should support images in custom header" do
|
92
|
-
header = "1.8cm*PhantomPDF header!<img src=\"#{@
|
89
|
+
header = "1.8cm*PhantomPDF header!<img src=\"#{@custom_image}\" style=\"float:right;\"/>"
|
93
90
|
|
94
|
-
Generator.new(
|
91
|
+
Generator.new(resource, @destination, {:header => header}).generate.should be_pdf_file
|
92
|
+
|
93
|
+
# we CANNOT reader the file as PDF
|
94
|
+
# pdf_content = PDF::Reader.new(@destination).page(1).text
|
95
|
+
# pdf_content.should include(@custom_image)
|
95
96
|
end
|
96
97
|
|
97
|
-
|
98
|
-
|
98
|
+
it "should support custom footer" do
|
99
|
+
footer = 'Hello, PhantomPDF footer!'
|
99
100
|
|
100
|
-
Generator.new(
|
101
|
+
Generator.new(resource, @destination, {:footer => footer}).generate.should be_pdf_file
|
101
102
|
|
102
|
-
|
103
|
-
pdf_content.
|
103
|
+
# we CANNOT reader the file as PDF
|
104
|
+
# pdf_content = PDF::Reader.new(@destination).page(1).text
|
105
|
+
# pdf_content.should include(header)
|
104
106
|
end
|
105
107
|
|
106
|
-
it "should support images in custom
|
107
|
-
footer = "1.8cm*PhantomPDF footer!<img src=\"#{@
|
108
|
+
it "should support images in custom footer" do
|
109
|
+
footer = "1.8cm*PhantomPDF footer!<img src=\"#{@custom_image}\" style=\"float:right;\"/>"
|
110
|
+
|
111
|
+
Generator.new(resource, @destination, {:footer => footer}).generate.should be_pdf_file
|
108
112
|
|
109
|
-
|
113
|
+
# we CANNOT reader the file as PDF
|
114
|
+
# pdf_content = PDF::Reader.new(@destination).page(1).text
|
115
|
+
# pdf_content.should include(@custom_image)
|
110
116
|
end
|
111
117
|
end
|
112
118
|
end
|
113
119
|
|
114
120
|
context "#generate!" do
|
115
|
-
before :all do
|
116
|
-
@url = 'http://www.google.com'
|
117
|
-
end
|
118
|
-
|
119
121
|
it "should raise PhantomPDF::RenderingError when failed to generate" do
|
120
122
|
$?.stub(:exitstatus) { 1 }
|
121
123
|
|
122
|
-
generator = Generator.new(
|
124
|
+
generator = Generator.new(resource)
|
123
125
|
generator.stub(:run) { 'rendering error' }
|
124
126
|
|
125
127
|
expect{
|
@@ -129,12 +131,8 @@ module PhantomPDF
|
|
129
131
|
end
|
130
132
|
|
131
133
|
context "#to_string" do
|
132
|
-
|
133
|
-
|
134
|
-
end
|
135
|
-
|
136
|
-
it "should return string" do
|
137
|
-
Generator.new(@url).to_string.should be_pdf_string
|
134
|
+
it "should return string of PDF file" do
|
135
|
+
Generator.new(resource).to_string.should be_pdf_string
|
138
136
|
end
|
139
137
|
end
|
140
138
|
end
|
@@ -1,20 +1,32 @@
|
|
1
1
|
var page = require('webpage').create(),
|
2
2
|
fs = require('fs'),
|
3
3
|
system = require('system'),
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
system_args_len = system.args.length,
|
5
|
+
phantom_exit = function(code, message){
|
6
|
+
if (message) {
|
7
|
+
console.log(message);
|
8
|
+
}
|
9
|
+
|
10
|
+
if (page) {
|
11
|
+
page.close();
|
12
|
+
}
|
13
|
+
|
14
|
+
phantom.exit(code || 0);
|
15
|
+
};
|
16
|
+
if (system_args_len < 3 || system_args_len > 12) {
|
17
|
+
phantom_exit(1, "Usage: phantomjs rasterize.js SOURCE DESTINATION [paperWidth*paperHeight|paperFormat] [header] [footer] [margin] [orientation] [zoom] [cookie_file] [render_timeout] [timeout]\n : paper (pdf output) examples: \"5in*7.5in\", \"10cm*20cm\", \"A4\", \"Letter\"");
|
9
18
|
}
|
10
19
|
|
11
20
|
var // PhantomJS CAN NOT handle images in custom header/footer,
|
12
21
|
// and this is used to fix the issue.
|
13
22
|
io_images = [],
|
14
23
|
|
24
|
+
is_http = function(s){
|
25
|
+
return /https?:\/\/.+?/i.test(s) === true;
|
26
|
+
},
|
15
27
|
is_html = function(s){
|
16
28
|
return /<([a-z]+?\d*?)[^>]*?>[^\0]*?<\/\1>/i.test(s) === true;
|
17
|
-
}
|
29
|
+
}
|
18
30
|
|
19
31
|
extract_images = function(html){
|
20
32
|
var image_tags = html.match(/<img[^>]+?src=(["'])https?:\/\/[^>]+?\1[^>]*?\/?>/ig),
|
@@ -25,7 +37,6 @@ var // PhantomJS CAN NOT handle images in custom header/footer,
|
|
25
37
|
}
|
26
38
|
|
27
39
|
image_tags_len = image_tags.length;
|
28
|
-
|
29
40
|
for (i=0; i<image_tags_len; i++) {
|
30
41
|
image = image_tags[i].match(/src=(["'])(https?:\/\/[^>]+?)\1/i);
|
31
42
|
|
@@ -67,22 +78,16 @@ var // PhantomJS CAN NOT handle images in custom header/footer,
|
|
67
78
|
},
|
68
79
|
|
69
80
|
render_pdf = function(html){
|
70
|
-
if (
|
71
|
-
|
72
|
-
|
73
|
-
phantom.exit(1);
|
81
|
+
if (typeof(html) == 'string') {
|
82
|
+
page.content = inject_images(html);
|
74
83
|
}
|
75
84
|
|
76
|
-
page.content = inject_images(html);
|
77
|
-
|
78
85
|
window.setTimeout(function(){
|
79
86
|
var output_tmp = output + '_tmp.pdf';
|
80
87
|
|
81
88
|
page.render(output_tmp);
|
82
89
|
if (!fs.exists(output_tmp)) {
|
83
|
-
|
84
|
-
|
85
|
-
phantom.exit(1);
|
90
|
+
phantom_exit(1, 'Failed to render pdf tmp file!');
|
86
91
|
}
|
87
92
|
|
88
93
|
if (fs.exists(output)) {
|
@@ -90,17 +95,15 @@ var // PhantomJS CAN NOT handle images in custom header/footer,
|
|
90
95
|
}
|
91
96
|
|
92
97
|
try {
|
93
|
-
fs.move(
|
98
|
+
fs.move(output_tmp, output);
|
94
99
|
|
95
100
|
// return pdf file path
|
96
101
|
console.log(output);
|
97
102
|
} catch (e) {
|
98
|
-
|
99
|
-
|
100
|
-
phantom.exit(1);
|
103
|
+
phantom_exit(1, e.message);
|
101
104
|
}
|
102
105
|
|
103
|
-
|
106
|
+
phantom_exit()
|
104
107
|
}, render_timeout);
|
105
108
|
},
|
106
109
|
|
@@ -118,21 +121,19 @@ var // PhantomJS CAN NOT handle images in custom header/footer,
|
|
118
121
|
timeout = system.args[11] || 90000;
|
119
122
|
|
120
123
|
window.setTimeout(function(){
|
121
|
-
|
122
|
-
|
123
|
-
phantom.exit(1);
|
124
|
+
phantom_exit(1, "Shit's being weird no result within " + timeout + "ms");
|
124
125
|
}, timeout);
|
125
126
|
|
126
127
|
page.viewportSize = { width:600, height:600 };
|
127
128
|
if (output.substr(-4) === '.pdf') {
|
128
129
|
var size, header, footer,
|
129
130
|
paper_size_options = {};
|
130
|
-
if (
|
131
|
+
if (system_args_len > 3) {
|
131
132
|
size = system.args[3].split('*');
|
132
133
|
paper_size_options = size.length === 2 ? {width:size[0], height:size[1], margin:'0px'} : {format:system.args[3], orientation:orientation, margin:margin};
|
133
134
|
}
|
134
135
|
|
135
|
-
if (
|
136
|
+
if (system_args_len > 4) {
|
136
137
|
header = system.args[4].split('*');
|
137
138
|
if (header.length >= 2) {
|
138
139
|
extract_images(header.join('*'));
|
@@ -146,7 +147,7 @@ if (output.substr(-4) === '.pdf') {
|
|
146
147
|
}
|
147
148
|
}
|
148
149
|
|
149
|
-
if (
|
150
|
+
if (system_args_len > 5) {
|
150
151
|
footer = system.args[5].split('*');
|
151
152
|
if (footer.length >= 2) {
|
152
153
|
extract_images(footer.join('*'));
|
@@ -182,18 +183,17 @@ if (cookie_file) {
|
|
182
183
|
if (is_html(input)) { // for html string
|
183
184
|
render_pdf(input);
|
184
185
|
} else { // for url resource
|
185
|
-
|
186
|
-
|
187
|
-
// determine the statusCode
|
186
|
+
// determine the resourceStatusCode
|
187
|
+
var resourceStatusCode = null;
|
188
188
|
page.onResourceReceived = function(resource){
|
189
189
|
if (new RegExp('^'+input).test(resource.url)) {
|
190
|
-
|
190
|
+
resourceStatusCode = resource.status;
|
191
191
|
}
|
192
192
|
};
|
193
193
|
|
194
194
|
page.open(input, function(status){
|
195
|
-
if (status !== 'success' || (
|
196
|
-
console.log(
|
195
|
+
if (status !== 'success' || (resourceStatusCode !== null && resourceStatusCode != 200)) {
|
196
|
+
console.log(resourceStatusCode, 'Failed to load the input!');
|
197
197
|
|
198
198
|
if (fs.exists(output)) {
|
199
199
|
fs.remove(output);
|
@@ -202,14 +202,12 @@ if (is_html(input)) { // for html string
|
|
202
202
|
try {
|
203
203
|
fs.touch(output);
|
204
204
|
} catch (e) {
|
205
|
-
|
206
|
-
|
207
|
-
phantom.exit(1);
|
205
|
+
phantom_exit(1, e.message);
|
208
206
|
}
|
209
207
|
|
210
|
-
|
208
|
+
phantom_exit(1);
|
211
209
|
} else {
|
212
|
-
render_pdf(page.content);
|
210
|
+
render_pdf(is_http(input) ? null : page.content);
|
213
211
|
}
|
214
212
|
});
|
215
213
|
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phantompdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Spring MC
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: phantomjs
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: json
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -39,7 +53,7 @@ dependencies:
|
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '0'
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
56
|
+
name: rack-test
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
44
58
|
requirements:
|
45
59
|
- - '>='
|
@@ -117,6 +131,7 @@ files:
|
|
117
131
|
- lib/phantompdf/source.rb
|
118
132
|
- lib/phantompdf/version.rb
|
119
133
|
- phantompdf.gemspec
|
134
|
+
- spec/fixtures/google.png
|
120
135
|
- spec/fixtures/phantompdf.html
|
121
136
|
- spec/phantompdf/assets_spec.rb
|
122
137
|
- spec/phantompdf/config_spec.rb
|
@@ -150,6 +165,7 @@ signing_key:
|
|
150
165
|
specification_version: 4
|
151
166
|
summary: A PhantomJS based PDF generator
|
152
167
|
test_files:
|
168
|
+
- spec/fixtures/google.png
|
153
169
|
- spec/fixtures/phantompdf.html
|
154
170
|
- spec/phantompdf/assets_spec.rb
|
155
171
|
- spec/phantompdf/config_spec.rb
|
@@ -157,3 +173,4 @@ test_files:
|
|
157
173
|
- spec/phantompdf/middleware_spec.rb
|
158
174
|
- spec/phantompdf/source_spec.rb
|
159
175
|
- spec/spec_helper.rb
|
176
|
+
has_rdoc:
|