lib/pdfkit/middleware.rb in pdfkit-0.8.2 vs lib/pdfkit/middleware.rb in pdfkit-0.8.3
- old
+ new
@@ -16,11 +16,20 @@
status, headers, response = @app.call(env)
if rendering_pdf? && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
body = response.respond_to?(:body) ? response.body : response.join
body = body.join if body.is_a?(Array)
- body = PDFKit.new(translate_paths(body, env), @options).to_pdf
+
+ root_url = root_url(env)
+ protocol = protocol(env)
+ options = @options.merge(root_url: root_url, protocol: protocol)
+
+ if headers['PDFKit-javascript-delay']
+ options.merge!(javascript_delay: headers.delete('PDFKit-javascript-delay').to_i)
+ end
+
+ body = PDFKit.new(body, options).to_pdf
response = [body]
if headers['PDFKit-save-pdf']
File.open(headers['PDFKit-save-pdf'], 'wb') { |file| file.write(body) } rescue nil
headers.delete('PDFKit-save-pdf')
@@ -30,57 +39,45 @@
# Do not cache PDFs
headers.delete('ETag')
headers.delete('Cache-Control')
end
- headers['Content-Length'] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
- headers['Content-Type'] = 'application/pdf'
+ headers['Content-Length'] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
+ headers['Content-Type'] = 'application/pdf'
end
[status, headers, response]
end
private
- # Change relative paths to absolute, and relative protocols to absolute protocols
- def translate_paths(body, env)
- body = translate_relative_paths(body, env)
- translate_relative_protocols(body, env)
+ def root_url(env)
+ PDFKit.configuration.root_url || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
end
- def translate_relative_paths(body, env)
- root = PDFKit.configuration.root_url || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
- # Try out this regexp using rubular http://rubular.com/r/hiAxBNX7KE
- body.gsub(/(href|src)=(['"])\/([^\/"']([^\"']*|[^"']*))?['"]/, "\\1=\\2#{root}\\3\\2")
+ def protocol(env)
+ env['rack.url_scheme']
end
- def translate_relative_protocols(body, env)
- protocol = "#{env['rack.url_scheme']}://"
- # Try out this regexp using rubular http://rubular.com/r/0Ohk0wFYxV
- body.gsub(/(href|src)=(['"])\/\/([^\"']*|[^"']*)['"]/, "\\1=\\2#{protocol}\\3\\2")
- end
-
def rendering_pdf?
@render_pdf
end
def render_as_pdf?
request_path = @request.path
- request_path_is_pdf = request_path.match(%r{\.pdf$})
+ return false unless request_path.end_with?('.pdf')
- if request_path_is_pdf && @conditions[:only]
+ if @conditions[:only]
conditions_as_regexp(@conditions[:only]).any? do |pattern|
- request_path =~ pattern
+ pattern === request_path
end
- elsif request_path_is_pdf && @conditions[:except]
- conditions_as_regexp(@conditions[:except]).each do |pattern|
- return false if request_path =~ pattern
+ elsif @conditions[:except]
+ conditions_as_regexp(@conditions[:except]).none? do |pattern|
+ pattern === request_path
end
-
- return true
else
- request_path_is_pdf
+ true
end
end
def set_request_to_render_as_pdf(env)
@render_pdf = true
@@ -97,11 +94,11 @@
def concat(accepts, type)
(accepts || '').split(',').unshift(type).compact.join(',')
end
def conditions_as_regexp(conditions)
- [conditions].flatten.map do |pattern|
- pattern.is_a?(Regexp) ? pattern : Regexp.new('^' + pattern)
+ Array(conditions).map do |pattern|
+ pattern.is_a?(Regexp) ? pattern : Regexp.new("^#{pattern}")
end
end
end
end