wicked_pdf 图像渲染
如何获取产品图像并以 PDF 格式显示?
我在视图文件中有这段代码
<div id="img-slide">
<% for asset in @car.assets %>
<%= image_tag(asset.asset.url(:medium)) %>
<% end %>
</div>
,它显示了这辆车的所有图像。
但如果我在 show.pdf.erb 中使用相同的代码,那么我得到的不是图像而是问号......就像图像丢失的东西一样。 那么,有没有办法把它们写在纸上呢?谢谢。
ps 控制台显示的内容
***************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 - - ***************"
更新
<%= 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) + '' + image
tag(:img, options)
end
但这仅适用于您服务器上的图像,并且对于所有汽车都相同,我如何才能获得每辆车的至少一张图像以 pdf 形式显示?拜托了。帮助!!!
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!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
版本 0.7.9
基本上返回文件系统上的绝对路径:
另请参阅:https://github.com/mileszs/wicked_pdf
Version 0.7.9 is
It basically returns the absolute path on the file system:
Also see: https://github.com/mileszs/wicked_pdf
可以不使用
wicked_pdf_image_tag
帮助程序,而是使用image_tag
和image_url
Rails 帮助程序来获取图像的绝对路径(这是>wicked_pdf
gem):这里
dir/image.png
是相对于 Rails 应用程序中标准位置的图像路径(对于图像而言为app/assets/images
)。Instead of using
wicked_pdf_image_tag
helper it's possible to useimage_tag
andimage_url
rails helpers together to get absolute path to image (this is what required forwicked_pdf
gem):here
dir/image.png
is image path relative to standard location in rails app (app/assets/images
for images).好的,所以我找到了答案
<%= image_tag(asset.asset.url(:medium)) %>
代码生成图像的 url,在 html 中看起来像所以我的图像从系统映射开始,我所要做的就是在
application_helper.rb< 中编写一个方法/code> 像这样:
这样 wiked_pdf 就会知道名为 pdf_image_tag 的图像标签将从 public 的系统文件夹开始,并且pdf.html.erb 的最终代码将是:
很简单,但我仍然花了几天时间才弄清楚。美好的一天。
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 inapplication_helper.rb
like this: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:
was easy, but still took me a few days to figure it out. Nice day.
唯一对我有用的是使用这个:
The only thing that worked for me is to use this:
虽然 @tap349 的答案对我来说有点用,但我必须对项目中已经存储的图像做一些完全不同的事情。
app/javascript/images
中):在另一篇文章中,我看到这个命令帮助我查看路径是否存在,你可以在红宝石中尝试一下 安慰:
While the answer of @tap349 worked a bit for me, I had to do something completely different for images already stored in the project.
app/javascript/images
):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: