Search code examples
ruby-on-railsx-sendfile

Displaying a PDF using send_file


I'm attempting to display uploaded pdf files via a controller method

class DocumentsController < ApplicationController
  def show
    response.headers['X-Sendfile-Type'] = "X-Sendfile"
    send_file(@document.file.path, filename: @document.file.name, type: "application/pdf", disposition: :inline)
  end
end

This works fine locally, but on heroku I get the inexplicit application error with no further stack trace

at=info method=GET path="/documents/21" host=myapp.herokuapp.com request_id=754f6d15-cb06-43a1-aa3c-308e5308b54e fwd="76.21.40.212" dyno=web.1 connect=29ms service=27ms status=500 bytes=1543

I've tried setting config.action_dispatch.x_sendfile_header = "X-Sendfile" in config/environments/production.rb but this causes all the assets on the application to not be loaded in production.

What am I missing?


Solution

  • Quoting Heroku's documentation (emphasis mine):

    Heroku does not support the use of Rack::Sendfile. Rack:Sendfile usually requires that there is a frontend webserver like nginx or apache is running on the same machine as the application server. This is not how Heroku is architected.

    To serve static files and uploads from a Heroku app, your best bet is to use a service like Amazon S3 for storage:

    AWS S3, or similar storage services, are important when architecting applications for scale and are a perfect complement to Heroku’s ephemeral filesystem.