wicked_pdf image rendering
Asked Answered
E

5

8

how do I get images of a product to show in pdf?

I have this code in view file

<div id="img-slide">
    <% for asset in @car.assets %>
    <%= image_tag(asset.asset.url(:medium)) %>
    <% end %>
</div>

and it shows all images for this car.

but if I use the same code in show.pdf.erb then instead of images I got only question marks.. like the image missing thing. So, is there a way to get them on paper? Thanks.

p.s. there is what console is showing

***************WICKED***************
  Asset Load (0.2ms)  SELECT `assets`.* FROM `assets` WHERE (`assets`.car_id = 29)
  Carmodel Load (0.2ms)  SELECT `carmodels`.* FROM `carmodels` WHERE `carmodels`.`id` = 28 LIMIT 1
Rendered cars/show.pdf.erb (255.2ms)
"***************/usr/bin/wkhtmltopdf       -q - - ***************"

update

<%= pdf_image_tag('/public/system/assets/163/medium/2011_lincoln_navigator_l_angularfront.jpg', :style=>"margin:0px;padding:0px", :width=>"300", :height=>"240")%>

this code shows how the link should look like in html, with this code I can render one photo of the car, but it will be the same for all cars, so I didn't do much.

the 163 number is the id of assets that is assigned to car, here I keep one image with more sizes(thumb, medium, large..) and I got 5 maps with different numbers for one car. So I have lots of maps with numberes like this as I have at least 5 photos for each car. each car have 5 assets. In show.html I can see them, but not in pdf. I did put this in application helper:

def pdf_image_tag(image, options = {})
  options[:src] = File.expand_path(RAILS_ROOT) + '' + image
  tag(:img, options)
end

but this is only for images that you have on your server and will be the same for all cars, how can I get at least one image of each car to show in pdf? Pleaseeeee. help!!!

Electrokinetics answered 15/10, 2011 at 12:54 Comment(2)
may be this answer help to us https://mcmap.net/q/1321315/-wicked_pdf-working-fine-in-development-but-not-in-productionBroadloom
This answer is helpful to you https://mcmap.net/q/1321315/-wicked_pdf-working-fine-in-development-but-not-in-productionBroadloom
E
8

ok, so I found the answer

the <%= image_tag(asset.asset.url(:medium)) %> code generates the url to the image that in html will look like <img alt="2012_bmw_7_series_gearshift" src="/system/assets/174/original/2012_bmw_7_series_gearshift.jpg?1318267462"> so my images starts in system map and all I had to do is to write a method in application_helper.rb like this:

module ApplicationHelper

 def pdf_image_tag(image, options = {})
  options[:src] = File.expand_path(RAILS_ROOT) + '/public' + image
  tag(:img, options)
 end
end

this way wiked_pdf will know that image tag called pdf_image_tag will start from system folder in public and the final code for the pdf.html.erb will be:

<% for asset in @car.assets %>
  <%= pdf_image_tag(asset.asset.url(:medium), :style=>"margin:0px;padding:0px", :width=>"250", :height=>"200")%>
<% end %>

was easy, but still took me a few days to figure it out. Nice day.

Electrokinetics answered 17/10, 2011 at 19:28 Comment(1)
Clean and easy solution. Thanks. But for Rails 4.2 you should change RAILS_ROOT for Rails.root, that's all.Simoneaux
D
34

Version 0.7.9 is

<%= wicked_pdf_image_tag 'myfile.jpg' %>

It basically returns the absolute path on the file system:

file:///path/to/image/myfile.jpg

Also see: https://github.com/mileszs/wicked_pdf

Dyarchy answered 8/11, 2012 at 1:5 Comment(0)
R
11

Instead of using wicked_pdf_image_tag helper it's possible to use image_tag and image_url rails helpers together to get absolute path to image (this is what required for wicked_pdf gem):

image_tag image_url('dir/image.png')

here dir/image.png is image path relative to standard location in rails app (app/assets/images for images).

Repatriate answered 6/7, 2015 at 15:51 Comment(0)
E
8

ok, so I found the answer

the <%= image_tag(asset.asset.url(:medium)) %> code generates the url to the image that in html will look like <img alt="2012_bmw_7_series_gearshift" src="/system/assets/174/original/2012_bmw_7_series_gearshift.jpg?1318267462"> so my images starts in system map and all I had to do is to write a method in application_helper.rb like this:

module ApplicationHelper

 def pdf_image_tag(image, options = {})
  options[:src] = File.expand_path(RAILS_ROOT) + '/public' + image
  tag(:img, options)
 end
end

this way wiked_pdf will know that image tag called pdf_image_tag will start from system folder in public and the final code for the pdf.html.erb will be:

<% for asset in @car.assets %>
  <%= pdf_image_tag(asset.asset.url(:medium), :style=>"margin:0px;padding:0px", :width=>"250", :height=>"200")%>
<% end %>

was easy, but still took me a few days to figure it out. Nice day.

Electrokinetics answered 17/10, 2011 at 19:28 Comment(1)
Clean and easy solution. Thanks. But for Rails 4.2 you should change RAILS_ROOT for Rails.root, that's all.Simoneaux
S
2

The only thing that worked for me is to use this:

<%= image_tag wicked_pdf_asset_base64('logo') %>
Subastral answered 12/1, 2022 at 13:50 Comment(1)
This is the most up to date solutionImplausible
A
0

While the answer of @tap349 worked a bit for me, I had to do something completely different for images already stored in the project.

  • So, for images uploaded by the users, I use the mini_magick gem and in my views I have (I use slime syntax):
= image_tag image_url(picture.url(:medium))
  • For images already stored in the project I had to manually create the absolute path, by returning the image location and joining it with the request protocol and host like this (I use Webpacker and my images are in app/javascript/images):
= wicked_pdf_image_tag URI.join(request.base_url, asset_pack_path('media/images/logo.png'))

In another post I saw this command that helped me to see if the path existed or not, you can try it in the ruby console:

Webpacker.manifest.send(:data).keys.grep /logo/
Ablaze answered 17/3, 2021 at 16:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.