Skip to content

Instantly share code, notes, and snippets.

@juggy
Created January 6, 2011 22:05
Show Gist options
  • Save juggy/768696 to your computer and use it in GitHub Desktop.
Save juggy/768696 to your computer and use it in GitHub Desktop.
ActionController::Base.asset_host = Proc.new { |source, request|
if request.env["REQUEST_PATH"].include? ".pdf"
"file://#{Rails.root.join('public')}"
else
"#{request.protocol}#{request.host_with_port}"
end
}
@gasova
Copy link

gasova commented Dec 15, 2011

this does not work for me: I've got the following error, can you help me please? I use Rails 3.0.10, and I'm trying to make PDKKit to work because of broken pipe error

ActionView::Template::Error (undefined method include?' for nil:NilClass): 7: <h1><%= @post.title %></h1> 8: <% if @post.headpic.file? %> 9: <div style="float:left;margin-top:5px;padding-right:5px;"> 10: <%= image_tag @post.headpic.url(:original) %> 11: <div class="image_cc">&copy; <%= @post.headpic_copyright %></div> 12: </div> 13: <% end %> config/initializers/assets.rb:2:inblock in <top (required)>'

@juggy
Copy link
Author

juggy commented Dec 16, 2011

@gasova It looks like you are trying to generate the pdf without going through the pdfkit middleware. If the request does not have a request path, it means you might not even go through rack... How are you calling this?

@gasova
Copy link

gasova commented Dec 16, 2011 via email

@timurvafin
Copy link

You can use root_url option with the same effect

PDFKit.configure do |config|
  config.root_url = "file://#{Rails.root.join('public')}"
end

@juggy
Copy link
Author

juggy commented Jan 23, 2012

@timurvafin not sure. I believe root_url is for relative paths. If my html or css is requiring a file at full url (http://domain.com/file) then it won't work.

@timurvafin
Copy link

@juggy, think solution with asset_host will not work either b/c it's used only by assets helpers which generate only paths (without proto & domain)

@juggy
Copy link
Author

juggy commented Jan 23, 2012

Think again. Asset hosts are there in production to roll out things like cloudfront and multiple assets servers. It can be used to avoid pipelining of assets in the rowser also (using asset1, asset2 hosts for example) . This particular gist is used in production for 2 of our apps for more than a year without any glitches.

@timurvafin
Copy link

Thank you for the gist!
Here is correct version with slash at the end for root_url option. Works for me too on heroku.

PDFKit.configure do |config|
  config.root_url = "file://#{Rails.root.join('public')}/"
end

@alexparker
Copy link

If you use the OP's method:

consider using this instead

if request.path.include? ".pdf"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment