Created
September 30, 2010 17:14
-
-
Save djbrowning/604937 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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