Skip to content

Commit

Permalink
It should now works with ActionDispatch::Response
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémy Coutable committed Dec 10, 2010
1 parent 2de388d commit 77151e3
Showing 1 changed file with 48 additions and 47 deletions.
95 changes: 48 additions & 47 deletions lib/pdfkit/middleware.rb
Expand Up @@ -7,67 +7,68 @@ def initialize(app, options = {}, conditions = {})
@options = options
@conditions = conditions
end

def call(env)
@request = Rack::Request.new(env)

status, headers, body = @app.call(env)
status, headers, response = @app.call(env)

if render_as_pdf?
if render_as_pdf? && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
set_request_to_render_as_pdf(env)
if headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
body = PDFKit.new(translate_paths(body.first, env), @options).to_pdf

# Do not cache PDFs
headers.delete('ETag')
headers.delete('Cache-Control')

headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
headers["Content-Type"] = "application/pdf"
end
body = response.respond_to?(:body) ? response.body : response.join
body = PDFKit.new(translate_paths(body, env), @options).to_pdf

# Do not cache PDFs
headers.delete('ETag')
headers.delete('Cache-Control')

headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
headers["Content-Type"] = "application/pdf"
else
body = response
end

[status, headers, body]
[status, headers, [body].flatten]
end

private

# Change relative paths to absolute
def translate_paths(body, env)
# Host with protocol
root = "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"

body.gsub(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')
end

def request_path_is_pdf?
@request.path =~ /\.pdf$/
end
# Change relative paths to absolute
def translate_paths(body, env)
# Host with protocol
root = "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"

def render_as_pdf?
if request_path_is_pdf? && @conditions[:only]
rules = [@conditions[:only]].flatten
rules.any? do |pattern|
if pattern.is_a?(Regexp)
@request.path =~ pattern
else
@request.path[0, pattern.length] == pattern
end
body.gsub(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')
end

def request_path_is_pdf?
@request.path =~ /\.pdf$/
end

def render_as_pdf?
if request_path_is_pdf? && @conditions[:only]
rules = [@conditions[:only]].flatten
rules.any? do |pattern|
if pattern.is_a?(Regexp)
@request.path =~ pattern
else
@request.path[0, pattern.length] == pattern
end
else
request_path_is_pdf?
end
else
request_path_is_pdf?
end

def set_request_to_render_as_pdf(env)
path = Pathname(env['PATH_INFO'])
['PATH_INFO','REQUEST_URI'].each { |e| env[e] = path.to_s.sub(/#{path.extname}$/,'') } if path.extname == '.pdf'
env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
end

def concat(accepts, type)
(accepts || '').split(',').unshift(type).compact.join(',')
end

end

def set_request_to_render_as_pdf(env)
path = Pathname(env['PATH_INFO'])
%w[PATH_INFO REQUEST_URI].each { |e| env[e] = path.to_s.sub(/#{path.extname}$/,'') }
env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
end

def concat(accepts, type)
(accepts || '').split(',').unshift(type).compact.join(',')
end

end
end

0 comments on commit 77151e3

Please sign in to comment.