command_wrap 0.5 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of command_wrap might be problematic. Click here for more details.
- data/lib/command_wrap.rb +64 -2
- data/tests/unit/command_wrap_test.rb +14 -0
- metadata +4 -3
data/lib/command_wrap.rb
CHANGED
@@ -9,9 +9,71 @@ module CommandWrap
|
|
9
9
|
`#{command}`
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
INJECT_SCRIPT = "
|
13
|
+
<script type='text/javascript'>
|
14
|
+
function subst() {
|
15
|
+
var vars = {};
|
16
|
+
var params = window.location.search.substring(1).split('&');
|
17
|
+
for (var i = 0; i < params.length; i++) {
|
18
|
+
var parts = params[i].split('=');
|
19
|
+
vars[parts[0]] = unescape(parts[1]);
|
20
|
+
}
|
21
|
+
var opts = [ 'frompage', 'topage', 'page', 'webpage', 'section', 'subsection', 'subsubsection' ];
|
22
|
+
for (var i in opts) {
|
23
|
+
var elems = document.getElementsByClassName(key[i]);
|
24
|
+
for (var j = 0; j < elems.length; j++) {
|
25
|
+
elems[j].textContent = vars[key[i]];
|
26
|
+
}
|
27
|
+
}
|
28
|
+
}
|
29
|
+
window.onload = subst;
|
30
|
+
</script>
|
31
|
+
"
|
32
|
+
|
33
|
+
def self.htmltopdf (source, target, options = {})
|
34
|
+
header_html = ''
|
35
|
+
footer_html = ''
|
36
|
+
|
37
|
+
params = ''
|
38
|
+
|
39
|
+
options.each do |key, value|
|
40
|
+
key = key.to_s
|
41
|
+
if value.nil? || value == ''
|
42
|
+
params += " --#{key}"
|
43
|
+
elsif key == 'header'
|
44
|
+
header_html = CommandWrap.temp('html')
|
45
|
+
# Inject parameters
|
46
|
+
if value.index('<head>')
|
47
|
+
value = value.gsub('<head>', '<head>' + INJECT_SCRIPT)
|
48
|
+
end
|
49
|
+
|
50
|
+
File.open(header_html, 'w') do |f|
|
51
|
+
f.write value
|
52
|
+
end
|
53
|
+
|
54
|
+
params += " --header-html #{header_html}"
|
55
|
+
elsif key == 'footer'
|
56
|
+
footer_html = CommandWrap.temp('html')
|
57
|
+
# Inject parameters
|
58
|
+
if value.index('<head>')
|
59
|
+
value = value.gsub('<head>', '<head>' + INJECT_SCRIPT)
|
60
|
+
end
|
61
|
+
|
62
|
+
File.open(footer_html, 'w') do |f|
|
63
|
+
f.write value
|
64
|
+
end
|
65
|
+
|
66
|
+
params += " --footer-html #{footer_html}"
|
67
|
+
else
|
68
|
+
params += " --#{key} #{value}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
command = CommandWrap::Config::Xvfb.command(File.dirname(__FILE__) + "/../bin/wkhtmltopdf --print-media-type #{source} #{params} #{target}")
|
14
73
|
`#{command}`
|
74
|
+
|
75
|
+
File.delete(header_html) if header_html != '' && File.exist?(header_html)
|
76
|
+
File.delete(footer_html) if footer_html != '' && File.exist?(footer_html)
|
15
77
|
end
|
16
78
|
|
17
79
|
# Sources consists of paths followed by the filename that must be used in the zip
|
@@ -122,4 +122,18 @@ class CommandWrapTest < Test::Unit::TestCase
|
|
122
122
|
end
|
123
123
|
end
|
124
124
|
|
125
|
+
def test_htmltopdf_with_commands
|
126
|
+
sleep 1
|
127
|
+
source = File.dirname(__FILE__) + "/../helpers/test.html"
|
128
|
+
target = CommandWrap.temp('pdf')
|
129
|
+
assert_nothing_raised do
|
130
|
+
footer = "<html><head><title>Footer</title></head><body><p>testing\nmultiple\nlines</p></body></html>"
|
131
|
+
header = "<html><head><title>Header</title></head><body><p>testing\nmultiple\nlines</p></body></html>"
|
132
|
+
CommandWrap.htmltopdf(source, target, :footer => footer, :header => header)
|
133
|
+
puts target
|
134
|
+
assert File.exists?(target)
|
135
|
+
File.delete(target)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
125
139
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: command_wrap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 5
|
9
|
-
|
9
|
+
- 1
|
10
|
+
version: 0.5.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Stefaan Colman
|
@@ -14,7 +15,7 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2011-
|
18
|
+
date: 2011-05-30 00:00:00 +02:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|