class PDFKit::Middleware

Public Class Methods

new(app, options = {}, conditions = {}) click to toggle source
# File lib/pdfkit/middleware.rb, line 3
def initialize(app, options = {}, conditions = {})
  @app        = app
  @options    = options
  @conditions = conditions
  @render_pdf = false
  @caching    = @conditions.delete(:caching) { false }
end

Public Instance Methods

call(env) click to toggle source
# File lib/pdfkit/middleware.rb, line 11
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]

    if headers['PDFKit-save-pdf']
      File.open(headers['PDFKit-save-pdf'], 'wb') { |file| file.write(body) } rescue nil
      headers.delete('PDFKit-save-pdf')
    end

    unless @caching
      # 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'
  end

  [status, headers, response]
end

Private Instance Methods

concat(accepts, type) click to toggle source
# File lib/pdfkit/middleware.rb, line 97
def concat(accepts, type)
  (accepts || '').split(',').unshift(type).compact.join(',')
end
conditions_as_regexp(conditions) click to toggle source
# File lib/pdfkit/middleware.rb, line 101
def conditions_as_regexp(conditions)
  [conditions].flatten.map do |pattern|
    pattern.is_a?(Regexp) ? pattern : Regexp.new('^' + pattern)
  end
end
render_as_pdf?() click to toggle source
# File lib/pdfkit/middleware.rb, line 66
def render_as_pdf?
  request_path = @request.path
  request_path_is_pdf = request_path.match(%r{\.pdf$})

  if request_path_is_pdf && @conditions[:only]
    conditions_as_regexp(@conditions[:only]).any? do |pattern|
      request_path =~ pattern
    end
  elsif request_path_is_pdf && @conditions[:except]
    conditions_as_regexp(@conditions[:except]).each do |pattern|
      return false if request_path =~ pattern
    end

    return true
  else
    request_path_is_pdf
  end
end
rendering_pdf?() click to toggle source
# File lib/pdfkit/middleware.rb, line 62
def rendering_pdf?
  @render_pdf
end
set_request_to_render_as_pdf(env) click to toggle source
# File lib/pdfkit/middleware.rb, line 85
def set_request_to_render_as_pdf(env)
  @render_pdf = true

  path = @request.path.sub(%r{\.pdf$}, '')
  path = path.sub(@request.script_name, '')

  %w[PATH_INFO REQUEST_URI].each { |e| env[e] = path }

  env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
  env['Rack-Middleware-PDFKit'] = 'true'
end
translate_paths(body, env) click to toggle source

Change relative paths to absolute, and relative protocols to absolute protocols

# File lib/pdfkit/middleware.rb, line 45
def translate_paths(body, env)
  body = translate_relative_paths(body, env)
  translate_relative_protocols(body, env)
end
translate_relative_paths(body, env) click to toggle source
# File lib/pdfkit/middleware.rb, line 50
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")
end
translate_relative_protocols(body, env) click to toggle source
# File lib/pdfkit/middleware.rb, line 56
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