Class: WickedPdf::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/wicked_pdf_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}, conditions = {}) ⇒ Middleware

Returns a new instance of Middleware


4
5
6
7
8
# File 'lib/wicked_pdf_middleware.rb', line 4

def initialize(app, options = {}, conditions = {})
  @app        = app
  @options    = options
  @conditions = conditions
end

Instance Method Details

#call(env) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wicked_pdf_middleware.rb', line 10

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.pdf_from_string(translate_paths(body, env))
    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