Wicked-PDF 不显示图像,'wicked_pdf_image_tag' 未定义

Wicked-PDF not showing images, 'wicked_pdf_image_tag' undefined

我想生成一个包含我们部门徽标的 PDF。当我尝试在我的控制器中使用 WickedPdf class 时(使用 https://github.com/mileszs/wicked_pdf 中描述的方法):

def some_action
  image_tag_string = image_tag('logo.jpg')
  pdf = WickedPdf.new.pdf_from_string(image_tag_string)

  save_path = Rails.root.join('testpdfs','logotest.pdf')
  File.open(save_path, 'wb') do |file|
    file << pdf
  end
end

...应用程序将 PDF 保存到目标目录,但它有一个蓝白的'?'标记图像应该在的位置。

如果我这样做:

  image_tag_string = wicked_pdf_image_tag('logo.jpg')
  pdf = WickedPdf.new.pdf_from_string(image_tag_string)

我收到以下错误:

NoMethodError:
   undefined method `wicked_pdf_image_tag' for #<...

我的 Rails 应用似乎也丢失了/没有链接到属于 wicked-pdf gem.

的帮助文件

Whosebug 上类似问题的答案建议编写自定义 "image-tag" 帮助程序来定位图像或安装 wkhtmltopdf。对我来说,image-tag 在视图中显示徽标时效果很好 (whatever.html.erb)。 "logo.jpg" 已经位于资产管道和#{RailsRoot}/public/images 中。最后,我在 Ubuntu 14.04.

上使用 wkhtmltopdf 0.9.9、wicked-pdf 0.11.0 和 rails 4

简而言之 - 我做错了什么导致 WickedPDF 无法渲染图像?

首先创建一个 pdf 模板以在该模板中呈现和使用您的 wicked_pdf 标签。 例如-

app/views/layout/application.pdf.erb-

<!doctype html>
<html>
  <head>
    <meta charset='utf-8' />
  </head>
  <body onload='number_pages'>
    <div id="content">
      <%= yield %>
    </div>
  </body>
</html>

app/views/pdf/pdf_view.pdf.erb-

<div>
  <%= wicked_pdf_image_tag 'logo.jpg' %>
</div>

改用此模板

def save
  pdf = WickedPdf.new.pdf_from_string(
                        render_to_string(
                          template: 'example/pdf_view.pdf.erb',
                          layout: 'layouts/application.pdf.erb'))
  send_data(pdf,
            filename: 'file_name.pdf',
            type: 'application/pdf',
            disposition: 'attachment') 
end

这可能对你有帮助..

我将图像 url 从 'https' 转换为 'http'。比工作。

Heroku-18
Rails 4.2
wicked_pdf (1.1.0)
wkhtmltopdf-binary (0.12.4)

在您的视图中使用 wicked_pdf_image_tag 帮助器,如果您的图像在 public/images 中,则使用 asset_url 引用图像,如果图像在 [=] 中,则使用 asset_pack_url 16=]

<%= wicked_pdf_image_tag asset_url('/images/footer_logo.png') %>

<%= wicked_pdf_image_tag asset_pack_url('media/images/footer_logo.png') %>

就我而言,我使用的是载波,解决方案来自this post

<img src="<%= root_url + "/" +file.service_url %>">

这适用于 rails 5.