my_pdfkit 0.1.0.0
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.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.github/workflows/release-drafter.yml +19 -0
- data/.github/workflows/stale.yml +19 -0
- data/.github/workflows/test.yml +71 -0
- data/.gitignore +25 -0
- data/.rspec +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +46 -0
- data/CHANGELOG.md +154 -0
- data/Gemfile +12 -0
- data/LICENSE +20 -0
- data/POST_INSTALL +14 -0
- data/README.md +190 -0
- data/Rakefile +24 -0
- data/lib/my_pdfkit/configuration.rb +89 -0
- data/lib/my_pdfkit/html_preprocessor.rb +25 -0
- data/lib/my_pdfkit/middleware.rb +117 -0
- data/lib/my_pdfkit/os.rb +21 -0
- data/lib/my_pdfkit/pdfkit.rb +153 -0
- data/lib/my_pdfkit/source.rb +52 -0
- data/lib/my_pdfkit/version.rb +5 -0
- data/lib/my_pdfkit/wkhtmltopdf.rb +82 -0
- data/lib/my_pdfkit.rb +10 -0
- data/my_pdfkit.gemspec +33 -0
- data/spec/configuration_spec.rb +171 -0
- data/spec/fixtures/example.css +1 -0
- data/spec/fixtures/example.html +5 -0
- data/spec/fixtures/example_with_hex_symbol.css +3 -0
- data/spec/html_preprocessor_spec.rb +71 -0
- data/spec/middleware_spec.rb +531 -0
- data/spec/os_spec.rb +67 -0
- data/spec/pdfkit_spec.rb +608 -0
- data/spec/source_spec.rb +125 -0
- data/spec/spec_helper.rb +33 -0
- metadata +175 -0
data/spec/pdfkit_spec.rb
ADDED
@@ -0,0 +1,608 @@
|
|
1
|
+
#encoding: UTF-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'spec_helper'
|
5
|
+
|
6
|
+
describe MyPDFKit do
|
7
|
+
describe "initialization" do
|
8
|
+
# Source
|
9
|
+
it "accepts HTML as the source" do
|
10
|
+
pdfkit = MyPDFKit.new('<h1>Oh Hai</h1>')
|
11
|
+
expect(pdfkit.source).to be_html
|
12
|
+
expect(pdfkit.source.to_s).to eq('<h1>Oh Hai</h1>')
|
13
|
+
end
|
14
|
+
|
15
|
+
it "accepts a URL as the source" do
|
16
|
+
pdfkit = MyPDFKit.new('http://google.com')
|
17
|
+
expect(pdfkit.source).to be_url
|
18
|
+
expect(pdfkit.source.to_s).to eq('http://google.com')
|
19
|
+
end
|
20
|
+
|
21
|
+
it "accepts a File as the source" do
|
22
|
+
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
|
23
|
+
pdfkit = MyPDFKit.new(File.new(file_path))
|
24
|
+
expect(pdfkit.source).to be_file
|
25
|
+
expect(pdfkit.source.to_s).to eq(file_path)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "accepts a Tempfile as the source" do
|
29
|
+
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
|
30
|
+
pdfkit = MyPDFKit.new(Tempfile.new(file_path))
|
31
|
+
expect(pdfkit.source).to be_file
|
32
|
+
expect(pdfkit.source.to_s).to match(/^#{Dir.tmpdir}/)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Options
|
36
|
+
## options keys
|
37
|
+
it "drops options without values" do
|
38
|
+
pdfkit = MyPDFKit.new('html', :page_size => nil)
|
39
|
+
expect(pdfkit.options).not_to have_key('--page-size')
|
40
|
+
end
|
41
|
+
|
42
|
+
it "transforms keys into command-line arguments" do
|
43
|
+
pdfkit = MyPDFKit.new('html', :page_size => 'Letter')
|
44
|
+
expect(pdfkit.options).to have_key('--page-size')
|
45
|
+
end
|
46
|
+
|
47
|
+
it "transforms complex keys into command-line arguments" do
|
48
|
+
pdfkit = MyPDFKit.new('html', :header_left => {'value' => 'something else'} )
|
49
|
+
expect(pdfkit.options).to have_key('--header-left')
|
50
|
+
end
|
51
|
+
|
52
|
+
it "drops options with false or falsey values" do
|
53
|
+
pdfkit = MyPDFKit.new('html', disable_smart_shrinking: false)
|
54
|
+
expect(pdfkit.options).not_to have_key('--disable-smart-shrinking')
|
55
|
+
end
|
56
|
+
|
57
|
+
## options values
|
58
|
+
it "parses string option values into strings" do
|
59
|
+
pdfkit = MyPDFKit.new('html', :page_size => 'Letter')
|
60
|
+
expect(pdfkit.options['--page-size']).to eql 'Letter'
|
61
|
+
end
|
62
|
+
|
63
|
+
it "drops option values of 'true'" do
|
64
|
+
pdfkit = MyPDFKit.new('html', disable_smart_shrinking: true)
|
65
|
+
expect(pdfkit.options).to have_key('--disable-smart-shrinking')
|
66
|
+
expect(pdfkit.options['--disable-smart-shrinking']).to be_nil
|
67
|
+
end
|
68
|
+
|
69
|
+
it "parses unknown value formats by transforming them into strings" do
|
70
|
+
pdfkit = MyPDFKit.new('html', image_dpi: 300)
|
71
|
+
expect(pdfkit.options['--image-dpi']).to eql '300'
|
72
|
+
end
|
73
|
+
|
74
|
+
it "parses hash option values into an array" do
|
75
|
+
pdfkit = MyPDFKit.new('html', :header_left => {'value' => 'something else'} )
|
76
|
+
expect(pdfkit.options['--header-left']).to eql ['value', 'something else']
|
77
|
+
end
|
78
|
+
|
79
|
+
it "flattens hash options into the key" do
|
80
|
+
pdfkit = MyPDFKit.new('html', :cookie => {:cookie_name1 => :cookie_val1, :cookie_name2 => :cookie_val2})
|
81
|
+
expect(pdfkit.options).not_to have_key('--cookie')
|
82
|
+
expect(pdfkit.options[['--cookie', 'cookie_name1']]).to eql 'cookie_val1'
|
83
|
+
expect(pdfkit.options[['--cookie', 'cookie_name2']]).to eql 'cookie_val2'
|
84
|
+
end
|
85
|
+
|
86
|
+
it "parses array option values into a string" do
|
87
|
+
pdfkit = MyPDFKit.new('html', :header_left => ['value', 'something else'] )
|
88
|
+
expect(pdfkit.options['--header-left']).to eql ['value', 'something else']
|
89
|
+
end
|
90
|
+
|
91
|
+
it "flattens array options" do
|
92
|
+
pdfkit = MyPDFKit.new('html', :cookie => [[:cookie_name1, :cookie_val1], [:cookie_name2, :cookie_val2]])
|
93
|
+
expect(pdfkit.options).not_to have_key('--cookie')
|
94
|
+
expect(pdfkit.options[['--cookie', 'cookie_name1']]).to eql 'cookie_val1'
|
95
|
+
expect(pdfkit.options[['--cookie', 'cookie_name2']]).to eql 'cookie_val2'
|
96
|
+
end
|
97
|
+
|
98
|
+
## default options
|
99
|
+
it "provides default options" do
|
100
|
+
pdfkit = MyPDFKit.new('<h1>Oh Hai</h1>')
|
101
|
+
['--margin-top', '--margin-right', '--margin-bottom', '--margin-left'].each do |option|
|
102
|
+
expect(pdfkit.options).to have_key(option)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
it "allows overriding default options" do
|
107
|
+
pdfkit = MyPDFKit.new('html', :page_size => 'A4')
|
108
|
+
expect(pdfkit.options['--page-size']).to eql 'A4'
|
109
|
+
end
|
110
|
+
|
111
|
+
it "defaults to 'UTF-8' encoding" do
|
112
|
+
pdfkit = MyPDFKit.new('Captación')
|
113
|
+
expect(pdfkit.options['--encoding']).to eq('UTF-8')
|
114
|
+
end
|
115
|
+
|
116
|
+
it "handles repeatable values which are strings" do
|
117
|
+
pdfkit = MyPDFKit.new('html', allow: 'http://myapp.com')
|
118
|
+
expect(pdfkit.options).to have_key ['--allow', 'http://myapp.com']
|
119
|
+
expect(pdfkit.options[['--allow', 'http://myapp.com']]).to eql nil
|
120
|
+
end
|
121
|
+
|
122
|
+
it "handles repeatable values which are hashes" do
|
123
|
+
pdfkit = MyPDFKit.new('html', allow: { 'http://myapp.com' => nil, 'http://google.com' => nil })
|
124
|
+
expect(pdfkit.options).to have_key ['--allow', 'http://myapp.com']
|
125
|
+
expect(pdfkit.options).to have_key ['--allow', 'http://google.com']
|
126
|
+
expect(pdfkit.options[['--allow', 'http://myapp.com']]).to eql nil
|
127
|
+
expect(pdfkit.options[['--allow', 'http://google.com']]).to eql nil
|
128
|
+
end
|
129
|
+
|
130
|
+
it "handles repeatable values which are arrays" do
|
131
|
+
pdfkit = MyPDFKit.new('html', allow: ['http://myapp.com', 'http://google.com'])
|
132
|
+
expect(pdfkit.options).to have_key ['--allow', 'http://myapp.com']
|
133
|
+
expect(pdfkit.options).to have_key ['--allow', 'http://google.com']
|
134
|
+
expect(pdfkit.options[['--allow', 'http://myapp.com']]).to eql nil
|
135
|
+
expect(pdfkit.options[['--allow', 'http://google.com']]).to eql nil
|
136
|
+
end
|
137
|
+
|
138
|
+
it "does not prepend cover option with --" do
|
139
|
+
pdfkit = MyPDFKit.new('html', "cover" => 'http://google.com')
|
140
|
+
expect(pdfkit.options).to have_key('cover')
|
141
|
+
end
|
142
|
+
|
143
|
+
it "does not prepend the toc option with --" do
|
144
|
+
pdfkit = MyPDFKit.new('html', 'toc' => '')
|
145
|
+
expect(pdfkit.options).to have_key('toc')
|
146
|
+
end
|
147
|
+
|
148
|
+
it "handles cover and toc params passed as symbols" do
|
149
|
+
pdfkit = MyPDFKit.new('html', {toc: true})
|
150
|
+
expect(pdfkit.options).to have_key('toc')
|
151
|
+
end
|
152
|
+
|
153
|
+
# Stylesheets
|
154
|
+
it "has no stylesheet by default" do
|
155
|
+
pdfkit = MyPDFKit.new('<h1>Oh Hai</h1>')
|
156
|
+
expect(pdfkit.stylesheets).to be_empty
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
describe "#options" do
|
161
|
+
# #options is an attr_reader, but it doesn't really matter. See these two examples:
|
162
|
+
it "cannot be externally overwritten entirely" do
|
163
|
+
pdfkit = MyPDFKit.new('html', :page_size => 'A4')
|
164
|
+
expect{ pdfkit.options = {} }.to raise_error(NoMethodError)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "has attributes that are externally mutable" do
|
168
|
+
pdfkit = MyPDFKit.new('html', :page_size => 'A4')
|
169
|
+
pdfkit.options['--page-size'] = 'Letter'
|
170
|
+
expect(pdfkit.options['--page-size']).to eql 'Letter'
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
describe "#command" do
|
175
|
+
it "constructs the correct command" do
|
176
|
+
pdfkit = MyPDFKit.new('html', :page_size => 'Letter', :toc_l1_font_size => 12, :replace => {'foo' => 'bar'})
|
177
|
+
command = pdfkit.command
|
178
|
+
expect(command.first).to match(/wkhtmltopdf/)
|
179
|
+
expect(command).to contain %w[--page-size Letter]
|
180
|
+
expect(command).to contain %w[--toc-l1-font-size 12]
|
181
|
+
expect(command).to contain %w[--replace foo bar]
|
182
|
+
end
|
183
|
+
|
184
|
+
it "contains a specified by path argument" do
|
185
|
+
pdfkit = MyPDFKit.new('html')
|
186
|
+
command = pdfkit.command("/foo/bar")
|
187
|
+
expect(command.first).to match(/wkhtmltopdf/)
|
188
|
+
expect(command.last).to eq("/foo/bar")
|
189
|
+
end
|
190
|
+
|
191
|
+
it "contains a specified by path argument of Pathname" do
|
192
|
+
pdfkit = MyPDFKit.new('html')
|
193
|
+
command = pdfkit.command(Pathname.new("/foo/bar"))
|
194
|
+
expect(command.first).to match(/wkhtmltopdf/)
|
195
|
+
expect(command.last).to eq("/foo/bar")
|
196
|
+
end
|
197
|
+
|
198
|
+
it "sets up one cookie when hash has only one cookie" do
|
199
|
+
pdfkit = MyPDFKit.new('html', cookie: {cookie_name: :cookie_value})
|
200
|
+
command = pdfkit.command
|
201
|
+
expect(command).to contain %w[--cookie cookie_name cookie_value]
|
202
|
+
end
|
203
|
+
|
204
|
+
it "does not split Windows paths that contain spaces" do
|
205
|
+
pdfkit = MyPDFKit.new('html')
|
206
|
+
allow(MyPDFKit.configuration).to receive(:wkhtmltopdf).and_return 'c:/Program Files/wkhtmltopdf/wkhtmltopdf.exe'
|
207
|
+
expect(pdfkit.command).not_to contain(%w[c:/Program Files/wkhtmltopdf/wkhtmltopdf.exe])
|
208
|
+
end
|
209
|
+
|
210
|
+
it "does not shell escape source URLs" do
|
211
|
+
pdfkit = MyPDFKit.new('https://www.google.com/search?q=pdfkit')
|
212
|
+
expect(pdfkit.command).to include "https://www.google.com/search?q=pdfkit"
|
213
|
+
end
|
214
|
+
|
215
|
+
it "formats source for the command" do
|
216
|
+
pdfkit = MyPDFKit.new('https://www.google.com/search?q=pdfkit')
|
217
|
+
expect(pdfkit.source).to receive(:to_input_for_command)
|
218
|
+
pdfkit.command
|
219
|
+
end
|
220
|
+
|
221
|
+
it "sets up multiple cookies when passed multiple cookies" do
|
222
|
+
pdfkit = MyPDFKit.new('html', :cookie => {:cookie_name1 => :cookie_val1, :cookie_name2 => :cookie_val2})
|
223
|
+
command = pdfkit.command
|
224
|
+
expect(command).to contain %w[--cookie cookie_name1 cookie_val1]
|
225
|
+
expect(command).to contain %w[--cookie cookie_name2 cookie_val2]
|
226
|
+
end
|
227
|
+
|
228
|
+
it "sets up multiple cookies when passed an array of tuples" do
|
229
|
+
pdfkit = MyPDFKit.new('html', :cookie => [[:cookie_name1, :cookie_val1], [:cookie_name2, :cookie_val2]])
|
230
|
+
command = pdfkit.command
|
231
|
+
expect(command).to contain %w[--cookie cookie_name1 cookie_val1]
|
232
|
+
expect(command).to contain %w[--cookie cookie_name2 cookie_val2]
|
233
|
+
end
|
234
|
+
|
235
|
+
it "will not include default options it is told to omit" do
|
236
|
+
MyPDFKit.configure do |config|
|
237
|
+
config.default_options[:disable_smart_shrinking] = true
|
238
|
+
end
|
239
|
+
|
240
|
+
pdfkit = MyPDFKit.new('html')
|
241
|
+
expect(pdfkit.command).to include('--disable-smart-shrinking')
|
242
|
+
pdfkit = MyPDFKit.new('html', :disable_smart_shrinking => false)
|
243
|
+
expect(pdfkit.command).not_to include('--disable-smart-shrinking')
|
244
|
+
end
|
245
|
+
|
246
|
+
it "must not split string arguments containing spaces" do
|
247
|
+
pdfkit = MyPDFKit.new('html', :header_center => "foo [page]")
|
248
|
+
expect(pdfkit.command).to contain ['--header-center', 'foo [page]']
|
249
|
+
end
|
250
|
+
|
251
|
+
it "paramatarizes string arguments" do
|
252
|
+
pdfkit = MyPDFKit.new('html', :header_center => "$(ls)")
|
253
|
+
expect(pdfkit.command).to contain %w[--header-center $(ls)]
|
254
|
+
end
|
255
|
+
|
256
|
+
it "read the source from stdin if it is html" do
|
257
|
+
pdfkit = MyPDFKit.new('html')
|
258
|
+
command = pdfkit.command
|
259
|
+
expect(command[-2]).to eq('-')
|
260
|
+
expect(command[-1]).to eq('-')
|
261
|
+
end
|
262
|
+
|
263
|
+
it "specifies the URL to the source if it is a url" do
|
264
|
+
pdfkit = MyPDFKit.new('http://google.com')
|
265
|
+
command = pdfkit.command
|
266
|
+
expect(command[-2]).to eq("http://google.com")
|
267
|
+
expect(command[-1]).to eq("-")
|
268
|
+
end
|
269
|
+
|
270
|
+
it "does not break Windows paths" do
|
271
|
+
pdfkit = MyPDFKit.new('html')
|
272
|
+
allow(MyPDFKit.configuration).to receive(:wkhtmltopdf).and_return 'c:/Program Files/wkhtmltopdf/wkhtmltopdf.exe'
|
273
|
+
expect(pdfkit.command).not_to contain ['Program', 'Files']
|
274
|
+
end
|
275
|
+
|
276
|
+
it "specifies the path to the source if it is a file" do
|
277
|
+
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
|
278
|
+
pdfkit = MyPDFKit.new(File.new(file_path))
|
279
|
+
command = pdfkit.command
|
280
|
+
expect(command[-2]).to eq(file_path)
|
281
|
+
expect(command[-1]).to eq('-')
|
282
|
+
end
|
283
|
+
|
284
|
+
it "specifies the path to the source if it is a tempfile" do
|
285
|
+
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
|
286
|
+
pdfkit = MyPDFKit.new(Tempfile.new(file_path))
|
287
|
+
command = pdfkit.command
|
288
|
+
expect(command[-2]).to start_with(Dir.tmpdir)
|
289
|
+
expect(command[-1]).to eq('-')
|
290
|
+
end
|
291
|
+
|
292
|
+
it "specifies the path for the ouput if a path is given" do
|
293
|
+
file_path = "/path/to/output.pdf"
|
294
|
+
pdfkit = MyPDFKit.new("html")
|
295
|
+
command = pdfkit.command(file_path)
|
296
|
+
expect(command.last).to eq(file_path)
|
297
|
+
end
|
298
|
+
|
299
|
+
it "detects special pdfkit meta tags" do
|
300
|
+
body = %{
|
301
|
+
<html>
|
302
|
+
<head>
|
303
|
+
<meta name="pdfkit-page_size" content="Legal"/>
|
304
|
+
<meta name="pdfkit-orientation" content="Landscape"/>
|
305
|
+
</head>
|
306
|
+
</html>
|
307
|
+
}
|
308
|
+
pdfkit = MyPDFKit.new(body)
|
309
|
+
command = pdfkit.command
|
310
|
+
expect(command).to contain %w[--page-size Legal]
|
311
|
+
expect(command).to contain %w[--orientation Landscape]
|
312
|
+
end
|
313
|
+
|
314
|
+
it "detects cookies meta tag" do
|
315
|
+
body = %{
|
316
|
+
<html>
|
317
|
+
<head>
|
318
|
+
<meta name="pdfkit-cookie rails_session" content='rails_session_value' />
|
319
|
+
<meta name="pdfkit-cookie cookie_variable" content='cookie_variable_value' />
|
320
|
+
</head>
|
321
|
+
</html>
|
322
|
+
}
|
323
|
+
pdfkit = MyPDFKit.new(body)
|
324
|
+
command = pdfkit.command
|
325
|
+
expect(command).to contain %w[--cookie rails_session rails_session_value --cookie cookie_variable cookie_variable_value]
|
326
|
+
end
|
327
|
+
|
328
|
+
it "detects disable_smart_shrinking meta tag" do
|
329
|
+
body = %{
|
330
|
+
<html>
|
331
|
+
<head>
|
332
|
+
<meta name="pdfkit-disable_smart_shrinking" content="true"/>
|
333
|
+
</head>
|
334
|
+
</html>
|
335
|
+
}
|
336
|
+
pdfkit = MyPDFKit.new(body)
|
337
|
+
command = pdfkit.command
|
338
|
+
expect(command).to include "--disable-smart-shrinking"
|
339
|
+
expect(command).not_to contain %w[--disable-smart-shrinking true]
|
340
|
+
end
|
341
|
+
|
342
|
+
it "detects names with hyphens instead of underscores" do
|
343
|
+
body = %{
|
344
|
+
<html>
|
345
|
+
<head>
|
346
|
+
<meta content='Portrait' name='pdfkit-orientation'/>
|
347
|
+
<meta content="10mm" name="pdfkit-margin-bottom"/>
|
348
|
+
</head>
|
349
|
+
<br>
|
350
|
+
</html>
|
351
|
+
}
|
352
|
+
pdfkit = MyPDFKit.new(body)
|
353
|
+
expect(pdfkit.command).not_to include 'name\='
|
354
|
+
end
|
355
|
+
|
356
|
+
it "detects special pdfkit meta tags despite bad markup" do
|
357
|
+
body = %{
|
358
|
+
<html>
|
359
|
+
<head>
|
360
|
+
<meta name="pdfkit-page_size" content="Legal"/>
|
361
|
+
<meta name="pdfkit-orientation" content="Landscape"/>
|
362
|
+
</head>
|
363
|
+
<br>
|
364
|
+
</html>
|
365
|
+
}
|
366
|
+
pdfkit = MyPDFKit.new(body)
|
367
|
+
command = pdfkit.command
|
368
|
+
expect(command).to contain %w[--page-size Legal]
|
369
|
+
expect(command).to contain %w[--orientation Landscape]
|
370
|
+
end
|
371
|
+
|
372
|
+
it "skips non-pdfkit meta tags" do
|
373
|
+
body = %{
|
374
|
+
<html>
|
375
|
+
<head>
|
376
|
+
<meta name="test-page_size" content="Legal"/>
|
377
|
+
<meta name="pdfkit-orientation" content="Landscape"/>
|
378
|
+
</head>
|
379
|
+
<br>
|
380
|
+
</html>
|
381
|
+
}
|
382
|
+
pdfkit = MyPDFKit.new(body)
|
383
|
+
command = pdfkit.command
|
384
|
+
expect(command).not_to contain %w[--page-size Legal]
|
385
|
+
expect(command).to contain %w[--orientation Landscape]
|
386
|
+
end
|
387
|
+
|
388
|
+
it "does not use quiet when told to" do
|
389
|
+
pdfkit = MyPDFKit.new('html', quiet: false)
|
390
|
+
expect(pdfkit.command).not_to include '--quiet'
|
391
|
+
end
|
392
|
+
|
393
|
+
it "uses quiet option by default" do
|
394
|
+
pdfkit = MyPDFKit.new('html')
|
395
|
+
expect(pdfkit.command).to include '--quiet'
|
396
|
+
end
|
397
|
+
|
398
|
+
it "does not use quiet option in verbose mode" do
|
399
|
+
MyPDFKit.configure do |config|
|
400
|
+
config.verbose = true
|
401
|
+
end
|
402
|
+
|
403
|
+
pdfkit = MyPDFKit.new('html')
|
404
|
+
expect(pdfkit.command).not_to include '--quiet'
|
405
|
+
|
406
|
+
MyPDFKit.configure do |config|
|
407
|
+
config.verbose = false
|
408
|
+
end
|
409
|
+
end
|
410
|
+
|
411
|
+
it "does not use quiet option in verbose mode when option of quiet is configured" do
|
412
|
+
MyPDFKit.configure do |config|
|
413
|
+
config.verbose = true
|
414
|
+
config.default_options[:quiet] = true
|
415
|
+
end
|
416
|
+
|
417
|
+
pdfkit = MyPDFKit.new('html')
|
418
|
+
expect(pdfkit.command).not_to include '--quiet'
|
419
|
+
|
420
|
+
MyPDFKit.configure do |config|
|
421
|
+
config.verbose = false
|
422
|
+
end
|
423
|
+
end
|
424
|
+
|
425
|
+
it "does not use xvfb-run wrapper by default" do
|
426
|
+
pdfkit = MyPDFKit.new('html')
|
427
|
+
expect(pdfkit.command).not_to include 'xvfb-run'
|
428
|
+
end
|
429
|
+
|
430
|
+
it "uses xvfb-run wrapper when option of using xvfb is configured" do
|
431
|
+
MyPDFKit.configure do |config|
|
432
|
+
config.use_xvfb = true
|
433
|
+
end
|
434
|
+
|
435
|
+
pdfkit = MyPDFKit.new('html')
|
436
|
+
expect(pdfkit.command).to include 'xvfb-run'
|
437
|
+
|
438
|
+
MyPDFKit.configure do |config|
|
439
|
+
config.use_xvfb = false
|
440
|
+
end
|
441
|
+
end
|
442
|
+
|
443
|
+
context "on windows" do
|
444
|
+
before do
|
445
|
+
allow(MyPDFKit::OS).to receive(:host_is_windows?).and_return(true)
|
446
|
+
end
|
447
|
+
|
448
|
+
it "quotes spaces in options" do
|
449
|
+
pdf = MyPDFKit.new('html', :title => 'hello world')
|
450
|
+
expect(pdf.command).to contain ['--title', "hello world"]
|
451
|
+
end
|
452
|
+
end
|
453
|
+
end
|
454
|
+
|
455
|
+
describe "#to_pdf" do
|
456
|
+
it "does not read the contents of the pdf when saving it as a file" do
|
457
|
+
file_path = "/my/file/path.pdf"
|
458
|
+
pdfkit = MyPDFKit.new('html', :page_size => 'Letter')
|
459
|
+
|
460
|
+
mock_pdf = double
|
461
|
+
expect(mock_pdf).to receive(:puts)
|
462
|
+
expect(mock_pdf).not_to receive(:gets) # do no read the contents when given a file path
|
463
|
+
expect(mock_pdf).to receive(:close_write)
|
464
|
+
|
465
|
+
|
466
|
+
expect(IO).to receive(:popen) do |args, mode, &block|
|
467
|
+
block.call(mock_pdf)
|
468
|
+
end
|
469
|
+
|
470
|
+
expect(::File).to receive(:size).with(file_path).and_return(50)
|
471
|
+
|
472
|
+
pdfkit.to_pdf(file_path)
|
473
|
+
end
|
474
|
+
|
475
|
+
it "generates a PDF of the HTML" do
|
476
|
+
pdfkit = MyPDFKit.new('html', :page_size => 'Letter')
|
477
|
+
pdf = pdfkit.to_pdf
|
478
|
+
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at beginning of file
|
479
|
+
end
|
480
|
+
|
481
|
+
it "generates a PDF with a numerical parameter" do
|
482
|
+
pdfkit = MyPDFKit.new('html', :header_spacing => 1)
|
483
|
+
pdf = pdfkit.to_pdf
|
484
|
+
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at beginning of file
|
485
|
+
end
|
486
|
+
|
487
|
+
it "generates a PDF with a symbol parameter" do
|
488
|
+
pdfkit = MyPDFKit.new('html', :page_size => :Letter)
|
489
|
+
pdf = pdfkit.to_pdf
|
490
|
+
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at beginning of file
|
491
|
+
end
|
492
|
+
|
493
|
+
it "adds the stylesheet to the head tag if it has a head tag" do
|
494
|
+
pdfkit = MyPDFKit.new("<html><head></head><body>Hai!</body></html>")
|
495
|
+
css = File.join(SPEC_ROOT,'fixtures','example.css')
|
496
|
+
pdfkit.stylesheets << css
|
497
|
+
pdfkit.to_pdf
|
498
|
+
expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style>")
|
499
|
+
end
|
500
|
+
|
501
|
+
it "prepends style tags if the HTML doesn't have a head tag" do
|
502
|
+
pdfkit = MyPDFKit.new("<html><body>Hai!</body></html>")
|
503
|
+
css = File.join(SPEC_ROOT,'fixtures','example.css')
|
504
|
+
pdfkit.stylesheets << css
|
505
|
+
pdfkit.to_pdf
|
506
|
+
expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style><html>")
|
507
|
+
end
|
508
|
+
|
509
|
+
it "throws an error if the source is not html and stylesheets have been added" do
|
510
|
+
pdfkit = MyPDFKit.new('http://google.com')
|
511
|
+
css = File.join(SPEC_ROOT,'fixtures','example.css')
|
512
|
+
pdfkit.stylesheets << css
|
513
|
+
expect { pdfkit.to_pdf }.to raise_error(MyPDFKit::ImproperSourceError)
|
514
|
+
end
|
515
|
+
|
516
|
+
it "can deal with ActiveSupport::SafeBuffer" do
|
517
|
+
pdfkit = MyPDFKit.new(ActiveSupport::SafeBuffer.new "<html><head></head><body>Hai!</body></html>")
|
518
|
+
css = File.join(SPEC_ROOT,'fixtures','example.css')
|
519
|
+
pdfkit.stylesheets << css
|
520
|
+
pdfkit.to_pdf
|
521
|
+
expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style></head>")
|
522
|
+
end
|
523
|
+
|
524
|
+
it "can deal with ActiveSupport::SafeBuffer if the HTML doesn't have a head tag" do
|
525
|
+
pdfkit = MyPDFKit.new(ActiveSupport::SafeBuffer.new "<html><body>Hai!</body></html>")
|
526
|
+
css = File.join(SPEC_ROOT,'fixtures','example.css')
|
527
|
+
pdfkit.stylesheets << css
|
528
|
+
pdfkit.to_pdf
|
529
|
+
expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style>")
|
530
|
+
end
|
531
|
+
|
532
|
+
it "escapes \\X in stylesheets" do
|
533
|
+
pdfkit = MyPDFKit.new("<html><head></head><body>Hai!</body></html>")
|
534
|
+
css = File.join(SPEC_ROOT,'fixtures','example_with_hex_symbol.css')
|
535
|
+
pdfkit.stylesheets << css
|
536
|
+
pdfkit.to_pdf
|
537
|
+
expect(pdfkit.source.to_s).to include("<style>#{File.read(css)}</style></head>")
|
538
|
+
end
|
539
|
+
|
540
|
+
#NOTICE: This test is failed if use wkhtmltopdf-binary (0.9.9.1)
|
541
|
+
it "throws an error if it is unable to connect" do
|
542
|
+
pdfkit = MyPDFKit.new("http://google.com/this-should-not-be-found/404.html")
|
543
|
+
expect { pdfkit.to_pdf }.to raise_error MyPDFKit::ImproperWkhtmltopdfExitStatus, /exitstatus=1/
|
544
|
+
end
|
545
|
+
|
546
|
+
it "does not throw an error if it is unable to connect", pending: 'this test works for wkhtmltopdf-binary (0.9.9.1)' do
|
547
|
+
pdfkit = MyPDFKit.new("http://localhost/this-should-not-be-found/404.html")
|
548
|
+
pdf = pdfkit.to_pdf
|
549
|
+
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at the beginning
|
550
|
+
end
|
551
|
+
|
552
|
+
it "generates a PDF if there are missing assets" do
|
553
|
+
pdfkit = MyPDFKit.new("<html><body><img alt='' src='http://example.com/surely-it-doesnt-exist.gif' /></body></html>")
|
554
|
+
pdf = pdfkit.to_pdf
|
555
|
+
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at the beginning
|
556
|
+
end
|
557
|
+
|
558
|
+
it "can handle ampersands in URLs" do
|
559
|
+
pdfkit = MyPDFKit.new('https://www.google.com/search?q=pdfkit&sort=ASC')
|
560
|
+
pdf = pdfkit.to_pdf
|
561
|
+
expect(pdf[0...4]).to eq("%PDF") # PDF Signature at the beginning
|
562
|
+
end
|
563
|
+
end
|
564
|
+
|
565
|
+
describe "#to_file" do
|
566
|
+
before do
|
567
|
+
@file_path = File.join(SPEC_ROOT,'fixtures','test.pdf')
|
568
|
+
File.delete(@file_path) if File.exist?(@file_path)
|
569
|
+
end
|
570
|
+
|
571
|
+
after do
|
572
|
+
File.delete(@file_path)
|
573
|
+
end
|
574
|
+
|
575
|
+
it "creates a file with the PDF as content" do
|
576
|
+
pdfkit = MyPDFKit.new('html', :page_size => 'Letter')
|
577
|
+
file = pdfkit.to_file(@file_path)
|
578
|
+
expect(file).to be_instance_of(File)
|
579
|
+
expect(File.read(file.path)[0...4]).to eq("%PDF") # PDF Signature at beginning of file
|
580
|
+
end
|
581
|
+
|
582
|
+
it "does not truncate data (in Ruby 1.8.6)" do
|
583
|
+
file_path = File.join(SPEC_ROOT,'fixtures','example.html')
|
584
|
+
pdfkit = MyPDFKit.new(File.new(file_path))
|
585
|
+
pdf_data = pdfkit.to_pdf
|
586
|
+
pdfkit.to_file(@file_path)
|
587
|
+
file_data = open(@file_path, 'rb') {|io| io.read }
|
588
|
+
expect(pdf_data.size).to eq(file_data.size)
|
589
|
+
end
|
590
|
+
end
|
591
|
+
|
592
|
+
context "security" do
|
593
|
+
before do
|
594
|
+
@test_path = File.join(SPEC_ROOT,'fixtures','security-oops')
|
595
|
+
File.delete(@test_path) if File.exist?(@test_path)
|
596
|
+
end
|
597
|
+
|
598
|
+
after do
|
599
|
+
File.delete(@test_path) if File.exist?(@test_path)
|
600
|
+
end
|
601
|
+
|
602
|
+
it "does not allow shell injection in options" do
|
603
|
+
pdfkit = MyPDFKit.new('html', :header_center => "a title\"; touch #{@test_path} #")
|
604
|
+
pdfkit.to_pdf
|
605
|
+
expect(File.exist?(@test_path)).to eq(false)
|
606
|
+
end
|
607
|
+
end
|
608
|
+
end
|