lib/pdfkit/middleware.rb in pdfkit-0.5.0 vs lib/pdfkit/middleware.rb in pdfkit-0.5.1
- old
+ new
@@ -8,16 +8,18 @@
@conditions = conditions
end
def call(env)
@request = Rack::Request.new(env)
+ @render_pdf = false
set_request_to_render_as_pdf(env) if render_as_pdf?
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
response = [body]
# Do not cache PDFs
headers.delete('ETag')
@@ -33,10 +35,10 @@
private
# Change relative paths to absolute
def translate_paths(body, env)
# Host with protocol
- root = "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
+ root = PDFKit.configuration.root_url || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
body.gsub(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')
end
def rendering_pdf?