Skip to content

Instantly share code, notes, and snippets.

@djbrowning
Created September 30, 2010 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djbrowning/604937 to your computer and use it in GitHub Desktop.
Save djbrowning/604937 to your computer and use it in GitHub Desktop.
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)
puts "PDFKit OBJECT CREATED"
body = pdf.to_pdf
puts "PDF BODY CONVERTED"
# 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]
puts "FINISHING UP"
end
puts "RETURNING RESPONSE"
[status, headers, response]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment