15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/pdfkit/middleware.rb', line 15
def _call(env)
@request = Rack::Request.new(env)
@render_pdf = false
set_request_to_render_as_pdf(env) if render_as_pdf?
status, , response = @app.call(env)
if rendering_pdf? && ['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
body = response.respond_to?(:body) ? response.body : response.join
body = body.join if body.is_a?(Array)
root_url = root_url(env)
protocol = protocol(env)
options = @options.merge(root_url: root_url, protocol: protocol)
if ['PDFKit-javascript-delay']
options.merge!(javascript_delay: .delete('PDFKit-javascript-delay').to_i)
end
body = PDFKit.new(body, options).to_pdf
response = [body]
if ['PDFKit-save-pdf']
File.open(['PDFKit-save-pdf'], 'wb') { |file| file.write(body) } rescue nil
.delete('PDFKit-save-pdf')
end
unless @caching
.delete('ETag')
.delete('Cache-Control')
end
['Content-Length'] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
['Content-Type'] = 'application/pdf'
['Content-Disposition'] = @conditions[:disposition] || 'inline'
end
[status, , response]
end
|