-
Notifications
You must be signed in to change notification settings - Fork 646
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Images and CSS not displaying - suggestion for fix #1
Comments
I was just about to post on this problem. A more-inclusive take on the GSUB is: .gsub(/(src|href)="//) { |s| "#{$1}="http://#{request.host_with_port}/" } This covers javascript, images, stylesheets and probably a few others without having app-specific selectors in there. |
That could definitely work, but as I mentioned above, I ran into a problem where javascript was causing wkhtmltopdf to hang. I'm not sure if it was a problem with ruby 1.8.5 or the version of wkhtmltopdf I was using, but removing javascript fixed it. I tested on a Mac system running 1.8.7 and did not experience the same issue. Just a heads up for anyone who might also experience that. |
There's a flag on the wkhtmltopdf command that prevents JS/plugins from running, plus a few other useful flags such as rendering in 'print' media mode. |
Just to point out a small fix to dougal's code, .gsub(/(src|href)="//) { |s| "#{$1}="http://#{request.host_with_port}/" } |
This helper works perfectly for making correct image tags: |
Another small fix to handle SSL and single quotes: |
Hi, I'm relatively new to RoR and currently trying to render images inside the pdf (using wickedpdf). I added the method , suggested by gipnokote for "pdf_image_tag" but I'm not really sure how to access it now. Any help or point in the right direction is appreciated. |
mgharios, I use it in the view simply like this (image should be stored in the public/images/ folder):
You can also add any HTML options after the image name like this:
|
gpnokote, thanks for the quick reply! I tried this and it tells me the method is not defined "undefined method `pdf_image_tag' for #ActionView::Base:0x477aef0". thanks! |
mgharios, I don't think you shoult put anything under vendor/plugin folder in this case.
|
This is GREAT! Thank you! I didn't realize I can put this method in any helper ( for some reason I thought it had to be inside the pdf_helper.rb which is part of the wickedpdf. Anyway, I ended using method # (1) from your options and put it into customers_helpe.rb which is where I'm generating my pdf from :) thanks again! |
The i get :
Prefix "/stylesheets/" for fullpath css is wrong. Foton |
Fix assets inclusion for Rails 5 and greater
This helper code works for me.
|
I was trying to render a page within my application as a PDF and the images weren't loading and CSS styles weren't being applied. The reason is because wkhtmltopdf doesn't know which server to load them from. The fork at http://github.com/lleirborras/wicked_pdf suggests creating a custom layout for the PDF and specify the request host/port. This works, but is an unnecessary step if you just want to render your standard templates as is. My suggestion is to replace all the paths for things like /stylesheets and /images after render_to_string:
I have hardcoded a few different paths that I am using (/system is where paperclip stores uploaded images), but these could easily be added to the options hash. You can do this with javascript files too, but one of the systems I tested on (Centos 5 w/ Ruby 1.8.5) was hanging with that included so I pulled it out.
The text was updated successfully, but these errors were encountered: