July 12, 2007...2:06 pm

streaming with send_file, or redirect_to and apache

Jump to Comments

We recently had cause to generate and fling large pdf files from our rails app. In development, we used send_file to stream the pdf file out via mongrel, but it seemed sensible to redirect_to apache for the heavy lifting in production, freeing up mongrel and rails for the delicate stuff.

So, at the end of the action, with the pdf file already generated and sitting in pdf_dir=’public/static/’:

if ENV['RAILS_ENV'].eql?(‘production’)
pdf_url = @request.protocol + [
@request.host_with_port,
'pdfing',
'static',
pdf_filename].join(‘/’)

redirect_to( pdf_url )
else
send_file( pdf_filename,
:type => ‘application/pdf’,
:disposition => ‘inline’,
:filename => pdf_dir + pdf_filename)
fi

There seems to be no nice railsy way of generating the absolute url other than using the @request object.

Presumably we could extract the project name from the request url rather than hardcoding it as ‘pdfing’ above.

Our apache is configured to serve the files in pdfing/static directly.

Leave a Reply