Including CSS files in PDF using wicked_pdf helpers - ruby-on-rails

I am successfully loading CSS files in my PDF file using wicked_pdf gem helpers:
<%= wicked_pdf_stylesheet_link_tag "pdf" %>
This works great in development, however when i deploy in production the pdf CSS file is not correctly imported. Also note that the file is "pdf.scss.erb" and not just "pdf.css".
I am not sure how to fix this problem. Looking at the server logs, i do not see any missed file warnings.

I myself solved the problem by duplicating the styles right onto the layout page.
However at this github thread people are advising to do several things:
Include the files in your precompile list in config/initializers/assets.rb as described in the Asset pipeline usage section of the README.
Using stylesheet_link_tag wicked_pdf_asset_base64('pdf')

wkthmltopdf not bundling with dependency library to wkthmltopdf can install the gem after installed gem run bundle install
gem "wkhtmltopdf-binary"

Related

Vendored assets in a Rails engine not loading when used as a Gem

Im writing an isolated Rails Engine which has it's own javascript in app/assets which in turn loads a bunch of dependencies that are kept in the engine's vendor/assets.
I've been using the dummy app in the test folder for development and everything has worked as I expect.
If I package the engine up as a gem and install it into a separate rails app, when I try to access the engine in a browser I get the Sprockets::FileNotFound exception couldn't find file.
If I fire up the console and have a look at Rails.application.config.assets.paths it includes mygem/app/assets but not mygem/vendor/assets.
This is where it gets weird. If I change the rails app's Gemfile and load the engine directly from a path, I don't have these problems. I can view my engine in browser without any Sprockets issues. Loading up the console and looking at Rails.application.config.assets.paths shows both path/to/mygem/app/assets AND path/to/mygem/vendor/assets.
I don't get this. Why would I get different behaviour if the engine is being loaded as a packaged gem or directly from a path?
Answering my own question. School-boy error, nothing to do with the asset pipeline, everything to do with adding the vendor path to the gemspec configuration.
s.files = Dir['{app,config,db,lib,vendor}/**/*', 'README.md', 'LICENSE.md']

Heroku does not load assets from asset pipeline

In my Rails app I have css and js that is unique to each action.
So users#new has its own css. It's named users_new.css.scss.
I deployed my app to Heroku, and Heroku isn't loading any of the assets. You can see the errors here. How do I fix this so Heroku correctly precompiles and loads the assets?
I read through Heroku's docs on using the asset pipeline, but didn't see any solution.
Precompilation
The problem is your timelines_index.js is not precompiled:
The way you can tell this is a problem is that when you call the asset in your layout (which I presume you're doing with the correct path helpers), Rails will automatically reference the latest precompiled version of that file (hence why application.js has the appended MD5 fingerprint)
I believe you'll need to add your custom files to your precompiled_assets array:
#config/environments/production.rb
Rails.application.config.assets.precompile += ['timelines_index.js']
This will give Rails the ability to precompile those assets individually, allowing you to call them in your layout:
#app/views/layouts/application.html.erb
<%= javascript_include_tag "timelines_index" %>
This will have to be done for all the custom asset files you wish to call in your layout.
Manifest
As mentioned by Anil, you may wish to use the manifest system of Rails to concatenate all your asset files into their respective application files.
This will quash the need to append the files to the Rails precompile array, allowing you to call a single file for your various assets:
#app/assets/javascripts/application.js
//= require_tree .
Add in your Gemfile
gem 'rails_12factor', group: :production

Why are wicked_pdf asset helpers generating errors on Heroku

I'm using wicked_pdf in a Rails 4.1 app.
PDF generation works perfectly within my local dev setup. But I get errors when deployed to Heroku.
Checking the logs I see
ActionView::Template::Error (No such file or directory - /app/public/photos/application.js):
associated with the wicked_pdf helpers
<%= wicked_pdf_javascript_include_tag "application" %>
I understood that the wicked_pdf helpers generated absolute urls, so
why am I seeing a relate url originating from the app folder?
How is this path being generated? I have no public/photos folder in
my app!
How do I go about debugging this problem? I'm unsure if this is a Heroku issue, a wkhtmltopdf issue, an asset compilation issue, or...?
Is anyone using wicked_pdf successfully on Heroku?
This is a new issue in Rails 4 (4.1?), where the compiled assets come out with digests like this:
application-4dd5b109ee3439da54f5bdfd78a80473.js
but no plain
application.js
is generated anymore.
You can try this gem https://github.com/alexspeller/non-stupid-digest-assets
or I would recommend base64 encoding your assets like described in this issue:
https://github.com/mileszs/wicked_pdf/issues/257
I'm working on rolling this into WickedPdf very soon, as this is a common issue to have differences in environments due to asset pipeline stuff.
I see that there is no an asets digest in log info that rails use for assets in producation environment.
I think the gem should resolve the issue because it was written for using wikedpdf on Heroku.
You can add it like that:
group :production do
gem "wkhtmltopdf-heroku"
end

addressing asset stylesheets with file extensions

I have some stylesheets in a subfolder /app/assets/stylesheets/themes of my rails app. These assets have the file extension .css.scss extension.
In my development environment I've been addressing those files with:
asset_path 'themes/theme-name.css.scss'
However, when I go to production Rails won't find those files. When I use just .css extension it seems to be working okay:
asset_path 'theme/theme-name.css'
My question is: what is the correct way to address asset files with multiple extensions?
Thanks for help
The correct way should be:
stylesheet_link_tag 'themes/theme-name'
If Rails is configured correct, the assets pipeline will figure out the file extensions itself .
Do you have sass loaded in your Gemfile?

Ruby Gem works when pointing to local gem but not once published

Here is the situation:
I decided to start moving all the js code that I reuse in every project in a gem and created the gem in:
/Users/alain/Dropbox/rails_app/alain_toolbox
So in my project, to test if things are working, I add this line to the gem file:
gem 'alain_toolbox', '=0.0.1', path:'/Users/alain/Dropbox/rails_app/alain_toolbox
everything works fine.
Then, I do a
gem build alain_toolbox.gemspec
gem push alain_toolbox-0.0.1.gem
and everything seem to work. So I replace the gem require in the gemfile with:
gem 'alain_toolbox'
then do a bundle and the gem install successfully. The problem is when I open up the app, I get an error that it cant find 'alain_toolbox' in the line
<%= javascript_include_tag "application" %>
and application.js looks like this:
//= require alain_toolbox
which was working when I was in local.
So the question is: why?(!)
Thx.
Found the error: I dont know how, but I forgot to do a 'git add' on the asset files. Once the file were added and the gem rebuilt, everything worked as expected.
The Rails Guide indicates the gem needs to implement a rails engine. In addition, the assets would still need to be in one of the specified directories within the gem (app/assets, lib/assets, or vendor/assets). I'm assuming in the local case somehow your gem is already in that path.
According to this AsciiCast you can see the asset pipeline paths in the rails console by printing the Rails.application.config.assets.paths configuratio
Related question: Get Gem vendor files in asset pipeline path

Resources