Class: GTK::Middleware

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Middleware.



3
4
5
6
7
# File 'lib/gtk_webkit_pdf/gtk_middleware_pdf.rb', line 3

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

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  @request = Rack::Request.new(env)
  @render_pdf = false

  convert_pdf_request_to_html(env) if pdf_request?
  status, headers, response = @app.call(env)

  if @render_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)

    printer = GTK::Webkit.new(prepend_absolute_path(body, env), 0).gtk_printer
    body = printer.pdf_content
    response = [body]

    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