class WickedPdf::Middleware

Public Class Methods

new(app, options = {}, conditions = {}) click to toggle source
# File lib/wicked_pdf/middleware.rb, line 3
def initialize(app, options = {}, conditions = {})
  @app = app
  @options = (WickedPdf.config || {}).merge(options)
  @conditions = conditions
end

Public Instance Methods

call(env) click to toggle source
# File lib/wicked_pdf/middleware.rb, line 9
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 = WickedPdf.new(@options[:wkhtmltopdf]).pdf_from_string(translate_paths(body, env), @options)

    response = [body]

    # 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'
    if @options.fetch(:disposition, '') == 'attachment'
      headers['Content-Disposition'] = 'attachment'
      headers['Content-Transfer-Encoding'] = 'binary'
    end
  end

  [status, headers, response]
end

Private Instance Methods

concat(accepts, type) click to toggle source
# File lib/wicked_pdf/middleware.rb, line 88
def concat(accepts, type)
  (accepts || '').split(',').unshift(type).compact.join(',')
end
render_as_pdf?() click to toggle source
# File lib/wicked_pdf/middleware.rb, line 53
def render_as_pdf?
  request_path_is_pdf = @request.path.match(%r{\.pdf$})

  if request_path_is_pdf && @conditions[:only]
    rules = [@conditions[:only]].flatten
    rules.any? do |pattern|
      if pattern.is_a?(Regexp)
        @request.fullpath =~ pattern
      else
        @request.path[0, pattern.length] == pattern
      end
    end
  elsif request_path_is_pdf && @conditions[:except]
    rules = [@conditions[:except]].flatten
    rules.map do |pattern|
      if pattern.is_a?(Regexp)
        return false if @request.fullpath =~ pattern
      elsif @request.path[0, pattern.length] == pattern
        return false
      end
    end

    return true
  else
    request_path_is_pdf
  end
end
rendering_pdf?() click to toggle source
# File lib/wicked_pdf/middleware.rb, line 49
def rendering_pdf?
  @render_pdf
end
set_request_to_render_as_pdf(env) click to toggle source
# File lib/wicked_pdf/middleware.rb, line 81
def set_request_to_render_as_pdf(env)
  @render_pdf = true
  %w[PATH_INFO REQUEST_URI].each { |e| env[e] = env[e].sub(%r{\.pdf\b}, '') }
  env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
  env['Rack-Middleware-WickedPdf'] = 'true'
end
translate_paths(body, env) click to toggle source

Change relative paths to absolute

# File lib/wicked_pdf/middleware.rb, line 42
def translate_paths(body, env)
  # Host with protocol
  root = WickedPdf.config[:root_url] || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"

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