wicked_pdf 图像渲染 [英] wicked_pdf image rendering
问题描述
如何让产品的图片以 pdf 格式显示?
我在视图文件中有此代码
<% 用于@car.assets 中的资产 %><%= image_tag(asset.asset.url(:medium))%><%结束%>
它显示了这辆车的所有图片.
但是如果我在 show.pdf.erb 中使用相同的代码,那么我只会得到问号而不是图像.就像图像丢失一样.那么,有没有办法把它们写在纸上?谢谢.
附言有什么控制台显示
***************邪恶的***************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渲染的汽车/show.pdf.erb(255.2ms)***************/usr/bin/wkhtmltopdf -q - - ***************"
更新
<%= pdf_image_tag('/public/system/assets/163/medium/2011_lincoln_navigator_l_angularfront.jpg', :style=>"margin:0px;padding:0px", :width=>"300", :height=>"240")%>
这段代码显示了链接在 html 中的样子,通过这段代码我可以渲染一张汽车的照片,但对于所有汽车都是一样的,所以我没有做太多.
163 数字是分配给汽车的资产的 id,这里我保留了一张具有更多尺寸(拇指、中、大..)的图像,并且我为一辆车获得了 5 张不同数字的地图.所以我有很多带有这样数字的地图,因为每辆车至少有 5 张照片.每辆车有5个资产.在 show.html 中我可以看到它们,但在 pdf 中看不到.我确实把它放在了应用程序助手中:
def pdf_image_tag(image, options = {})options[:src] = File.expand_path(RAILS_ROOT) + '' + 图像标签(:img,选项)结尾
但这仅适用于您服务器上的图像,并且对所有汽车都是相同的,我如何才能以 pdf 格式显示每辆车的至少一张图像?拜托了帮助!!!
好的,我找到了答案
<%= image_tag(asset.asset.url(:medium)) %>
代码生成图片的 url,在 html 中看起来像 <img alt="2012_bmw_7_series_gearshift" src="/system/assets/174/original/2012_bmw_7_series_gearshift.jpg?1318267462">
所以我的图像从系统映射开始,我所要做的就是在 中编写一个方法application_helper.rb
像这样:
module ApplicationHelperdef pdf_image_tag(image, options = {})options[:src] = File.expand_path(RAILS_ROOT) + '/public' + 图像标签(:img,选项)结尾结尾
这样 wiked_pdf 将知道名为 pdf_image_tag 的图像标签将从公开的系统文件夹开始,pdf.html.erb 的最终代码将是:
<% for asset in @car.assets %><%= pdf_image_tag(asset.asset.url(:medium), :style=>"margin:0px;padding:0px", :width=>"250", :height=>"200")%><%结束%>
很容易,但我还是花了几天时间才弄明白.美好的一天.
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!!!
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.
这篇关于wicked_pdf 图像渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!