dragonfly_phantomjs 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/dragonfly_phantomjs/plugin.rb +1 -1
- data/lib/dragonfly_phantomjs/processors/rasterize.rb +8 -8
- data/lib/dragonfly_phantomjs/version.rb +1 -1
- data/script/rasterize.coffee +21 -2
- data/test/dragonfly_phantomjs/processors/rasterize_test.rb +46 -19
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f10f860936a87eb0db8c933b363f6533e6d79b88
|
4
|
+
data.tar.gz: f13237acf25cdbae2095a4c73e5440dc36318e3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48ee7327877f0704ffc7fac84b15907c90629a0d061275416ac162464439b60f080d9eca5720d9198f6f53477cf6eda97dd4ff405d6a771c832245e32be5b7b8
|
7
|
+
data.tar.gz: abf665d554a9e35117384565891402b783f088408ecdaede38fc3552c51b82f43600e787ae98432d216cf240eb0d5c9720100173c48d4a7815d9818bbdcfab39
|
data/README.md
CHANGED
@@ -48,8 +48,12 @@ Options:
|
|
48
48
|
:paper_size - {string}, 'width*height', '300mm*300mm', supported units are 'mm', 'cm', 'in', 'px'
|
49
49
|
:viewport_size - {string}, 'width*height', '1440*900'
|
50
50
|
:zoom_factor - {number}, defaults to 1
|
51
|
+
:header - {hash}, {height: '10mm', content: 'Header content'}
|
52
|
+
:footer - {hash}, {height: '10mm', content: 'Footer content'}
|
51
53
|
```
|
52
54
|
|
55
|
+
For now refer to the phantomjs [api](http://phantomjs.org/api/webpage/property/paper-size.html) for more details on how to construct the string for the header/footer. You can use `pageNum` and `numPages` as variables.
|
56
|
+
|
53
57
|
## Contributing
|
54
58
|
|
55
59
|
1. Fork it ( https://github.com/tomasc/dragonfly_phantomjs/fork )
|
@@ -11,28 +11,28 @@ module DragonflyPhantomjs
|
|
11
11
|
# Incase of .svg file, the viewport_size is automatically set to the
|
12
12
|
# dimensions in the .svg file, unless explicitly specified in options
|
13
13
|
#
|
14
|
-
# IMPORTANT: Requires +phantomjs+
|
14
|
+
# IMPORTANT: Requires +phantomjs (~> 1.9)+
|
15
15
|
|
16
16
|
class Rasterize
|
17
17
|
|
18
18
|
class UnsupportedFormat < RuntimeError; end
|
19
19
|
|
20
|
-
#
|
21
|
-
#
|
22
|
-
#
|
23
|
-
#
|
24
|
-
#
|
20
|
+
# :margin - {number, string}, defaults to 0, supported units are 'mm', 'cm', 'in', 'px'
|
21
|
+
# :format - {string}, defaults to 'A4', supported formats are 'A4', 'A3', 'A5', 'Legal', 'Letter', 'Tabloid'
|
22
|
+
# :paper_size - {string}, 'width*height', '300mm*300mm', supported units are 'mm', 'cm', 'in', 'px'
|
23
|
+
# :viewport_size - {string}, 'width*height', '1440*900'
|
24
|
+
# :zoom_factor - {number}, defaults to 1
|
25
|
+
# :header - {hash}, {height: '10mm', contents: 'foo'}
|
26
|
+
# :footer - {hash}, {height: '10mm', contents: 'foo'}
|
25
27
|
|
26
28
|
def call content, format=:pdf, options={}
|
27
29
|
raise UnsupportedFormat unless %w(gif jpeg pdf png).include?(format.to_s)
|
28
|
-
|
29
30
|
content.shell_update(ext: format) do |old_path, new_path|
|
30
31
|
if File.extname(old_path) == 'svg' && options[:viewport_size].blank?
|
31
32
|
width = Dragonfly::Analysers::SvgAnalyser.new(content)[:width]
|
32
33
|
height = Dragonfly::Analysers::SvgAnalyser.new.(content)[:height]
|
33
34
|
options[:viewport_size] = "#{width.to_i}*#{height.to_i}"
|
34
35
|
end
|
35
|
-
|
36
36
|
"#{phantomjs_command} #{rasterize_script} #{old_path} #{new_path} '#{options.to_json}'"
|
37
37
|
end
|
38
38
|
end
|
data/script/rasterize.coffee
CHANGED
@@ -24,6 +24,7 @@ else
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
+
margin = options['margin'] || 0
|
27
28
|
border = options['border'] || 0
|
28
29
|
format = options['format'] || 'A4'
|
29
30
|
paper_size = options['paper_size']
|
@@ -33,6 +34,24 @@ user_agent = options['user_agent']
|
|
33
34
|
referer = options['referer']
|
34
35
|
element_id = options['element_id']
|
35
36
|
|
37
|
+
header =
|
38
|
+
if options['header']
|
39
|
+
height: options['header']['height']
|
40
|
+
contents: phantom.callback((pageNum, numPages) ->
|
41
|
+
eval options['header']['content']
|
42
|
+
)
|
43
|
+
else
|
44
|
+
null
|
45
|
+
|
46
|
+
footer =
|
47
|
+
if options['footer']
|
48
|
+
height: options['footer']['height']
|
49
|
+
contents: phantom.callback((pageNum, numPages) ->
|
50
|
+
eval options['footer']['content']
|
51
|
+
)
|
52
|
+
else
|
53
|
+
null
|
54
|
+
|
36
55
|
|
37
56
|
|
38
57
|
# ---------------------------------------------------------------------
|
@@ -53,10 +72,10 @@ if output.substr(-4) is ".pdf"
|
|
53
72
|
if paper_size != undefined
|
54
73
|
page_width = paper_size.split('*')[0]
|
55
74
|
page_height = paper_size.split('*')[1]
|
56
|
-
page.paperSize = { width: page_width, height: page_height, border: border }
|
75
|
+
page.paperSize = { width: page_width, height: page_height, margin: margin, border: border, header: header, footer: footer }
|
57
76
|
|
58
77
|
else if format != undefined
|
59
|
-
page.paperSize = { format: format, border: border }
|
78
|
+
page.paperSize = { format: format, margin: margin, border: border, header: header, footer: footer }
|
60
79
|
|
61
80
|
if viewport_size != undefined
|
62
81
|
viewport_width = viewport_size.split('*')[0]
|
@@ -4,68 +4,95 @@ module DragonflyPhantomjs
|
|
4
4
|
module Processors
|
5
5
|
describe Rasterize do
|
6
6
|
|
7
|
-
let(:app) { test_app.configure_with(:
|
7
|
+
let(:app) { test_app.configure_with(:phantomjs) }
|
8
8
|
let(:processor) { DragonflyPhantomjs::Processors::Rasterize.new }
|
9
9
|
|
10
10
|
let(:html) { Dragonfly::Content.new(app, SAMPLES_DIR.join('sample.html')) }
|
11
11
|
let(:svg) { Dragonfly::Content.new(app, SAMPLES_DIR.join('sample.svg')) }
|
12
12
|
|
13
|
+
let(:html_string) { Dragonfly::Content.new(app, '<html><head></head><body>foo</body></html>', name: 'foo.html') }
|
14
|
+
|
13
15
|
let(:options) {
|
14
16
|
{
|
15
|
-
|
17
|
+
margin: 0,
|
16
18
|
format: 'A4',
|
17
19
|
paper_size: '210mm*297mm',
|
18
20
|
viewport_size: '1440*900',
|
19
|
-
zoom_factor: 1
|
21
|
+
zoom_factor: 1,
|
22
|
+
header: {
|
23
|
+
height: '10mm',
|
24
|
+
content: 'foo'
|
25
|
+
},
|
26
|
+
footer: {
|
27
|
+
height: '10mm',
|
28
|
+
content: 'foo'
|
29
|
+
}
|
20
30
|
}
|
21
31
|
}
|
22
32
|
|
23
33
|
describe 'html' do
|
24
34
|
it 'returns PDF' do
|
25
|
-
|
26
|
-
get_mime_type(
|
35
|
+
processor.call(html, :pdf)
|
36
|
+
get_mime_type(html.path).must_include "application/pdf"
|
37
|
+
assert is_pdf(html.data)
|
27
38
|
end
|
28
39
|
|
29
40
|
it 'returns PNG' do
|
30
|
-
|
31
|
-
get_mime_type(
|
41
|
+
processor.call(html, :png, options)
|
42
|
+
get_mime_type(html.path).must_include "image/png"
|
32
43
|
end
|
33
44
|
|
34
45
|
it 'returns GIF' do
|
35
|
-
|
36
|
-
get_mime_type(
|
46
|
+
processor.call(html, :gif, options)
|
47
|
+
get_mime_type(html.path).must_include "image/gif"
|
37
48
|
end
|
38
49
|
|
39
50
|
it 'returns JPEG' do
|
40
|
-
|
41
|
-
get_mime_type(
|
51
|
+
processor.call(html, :jpeg, options)
|
52
|
+
get_mime_type(html.path).must_include "image/jpeg"
|
42
53
|
end
|
43
54
|
end
|
44
55
|
|
45
56
|
describe 'svg' do
|
46
57
|
it 'returns PDF' do
|
47
|
-
|
48
|
-
get_mime_type(
|
58
|
+
processor.call(svg, :pdf, options)
|
59
|
+
get_mime_type(svg.path).must_include "application/pdf"
|
60
|
+
assert is_pdf(svg.data)
|
49
61
|
end
|
50
62
|
|
51
63
|
it 'returns PNG' do
|
52
|
-
|
53
|
-
get_mime_type(
|
64
|
+
processor.call(svg, :png, options)
|
65
|
+
get_mime_type(svg.path).must_include "image/png"
|
54
66
|
end
|
55
67
|
|
56
68
|
it 'returns GIF' do
|
57
|
-
|
58
|
-
get_mime_type(
|
69
|
+
processor.call(svg, :gif, options)
|
70
|
+
get_mime_type(svg.path).must_include "image/gif"
|
59
71
|
end
|
60
72
|
|
61
73
|
it 'returns JPEG' do
|
62
|
-
|
63
|
-
get_mime_type(
|
74
|
+
processor.call(svg, :jpeg, options)
|
75
|
+
get_mime_type(svg.path).must_include "image/jpeg"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe 'string' do
|
80
|
+
it 'returns PDF' do
|
81
|
+
obj = '<html><head></head><body>foo</body></html>'
|
82
|
+
def obj.original_filename; 'something.html'; end
|
83
|
+
html_string.update(obj)
|
84
|
+
processor.call(html_string, :pdf)
|
85
|
+
get_mime_type(html_string.path).must_include "application/pdf"
|
86
|
+
assert is_pdf(html_string.data)
|
64
87
|
end
|
65
88
|
end
|
66
89
|
|
67
90
|
# ---------------------------------------------------------------------
|
68
91
|
|
92
|
+
def is_pdf data
|
93
|
+
data =~ /\A%PDF-1.4/
|
94
|
+
end
|
95
|
+
|
69
96
|
def get_mime_type file_path
|
70
97
|
`file --mime-type #{file_path}`.gsub(/\n/, "")
|
71
98
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dragonfly_phantomjs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomas Celizna
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dragonfly
|