wicked_pdf 图像渲染

发布于 2024-12-10 12:02:56 字数 1412 浏览 3 评论 0原文

如何获取产品图像并以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

む无字情书 2024-12-17 12:02:56

版本 0.7.9

<%= wicked_pdf_image_tag 'myfile.jpg' %>

基本上返回文件系统上的绝对路径:

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

另请参阅:https://github.com/mileszs/wicked_pdf

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

手心的海 2024-12-17 12:02:56

可以不使用 wicked_pdf_image_tag 帮助程序,而是使用 image_tagimage_url Rails 帮助程序来获取图像的绝对路径(这是 >wicked_pdf gem):

image_tag image_url('dir/image.png')

这里 dir/image.png 是相对于 Rails 应用程序中标准位置的图像路径(对于图像而言为 app/assets/images)。

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).

深海蓝天 2024-12-17 12:02:56

好的,所以我找到了答案

<%= image_tag(asset.asset.url(:medium)) %> 代码生成图像的 url,在 html 中看起来像 2012_bmw_7_series_gearshift 所以我的图像从系统映射开始,我所要做的就是在 application_helper.rb< 中编写一个方法/code> 像这样:

module ApplicationHelper

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

这样 wiked_pdf 就会知道名为 pdf_image_tag 的图像标签将从 public 的系统文件夹开始,并且pdf.html.erb 的最终代码将是:

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

很简单,但我仍然花了几天时间才弄清楚。美好的一天。

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.

夜未央樱花落 2024-12-17 12:02:56

唯一对我有用的是使用这个:

<%= image_tag wicked_pdf_asset_base64('logo') %>

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

<%= image_tag wicked_pdf_asset_base64('logo') %>
╰ゝ天使的微笑 2024-12-17 12:02:56

虽然 @tap349 的答案对我来说有点用,但我必须对项目中已经存储的图像做一些完全不同的事情。

  • 因此,对于用户上传的图像,我使用 mini_magick gem,在我的视图中(我使用 slime 语法):
= image_tag image_url(picture.url(:medium))
  • 对于已经存储在项目中的图像,我必须通过返回图像位置并加入来手动创建绝对路径它具有这样的请求协议和主机(我使用 Webpacker 并且我的图像位于 app/javascript/images 中):
= wicked_pdf_image_tag URI.join(request.base_url, asset_pack_path('media/images/logo.png'))

在另一篇文章中,我看到这个命令帮助我查看路径是否存在,你可以在红宝石中尝试一下 安慰:

Webpacker.manifest.send(:data).keys.grep /logo/

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/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文