Parent

Methods

Files

PDFKit::Middleware

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/pdfkit/middleware.rb, line 5
def initialize(app, options = {})
  @app = app
  @options = options
end

Public Instance Methods

call(env) click to toggle source
# File lib/pdfkit/middleware.rb, line 10
def call(env)
  @render_pdf = false
  set_request_to_render_as_pdf(env) if env['PATH_INFO'].match(/\.pdf$/)
  
  status, headers, response = @app.call(env)
  
  request = Rack::Request.new(env)
  if @render_pdf && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
    body = response.body
    
    body = translate_paths(body, env)
    
    pdf = PDFKit.new(body, @options)
    body = pdf.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"
    
    response = [body]
  end
  
  [status, headers, response]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.