Parent

Methods

PDFKit::Middleware

Public Class Methods

new(app, options = {}, conditions = {}) click to toggle source
# File lib/pdfkit/middleware.rb, line 5
def initialize(app, options = {}, conditions = {})
  @app        = app
  @options    = options
  @conditions = conditions
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]

    # 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

  [status, headers, response]
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.