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

问题描述 投票:9回答:2

我想生成一个包含我们部门徽标的PDF。当我尝试在我的控制器中使用WickedPdf类时(使用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的帮助文件。

StackOverflow上类似问题的答案建议编写自定义“图像标记”帮助程序来定位图像或安装wkhtmltopdf。对我来说,当放置在View(whatever.html.erb)中时,image-tag显示徽标就好了。 “logo.jpg”已经位于资产管道和#{RailsRoot} / public / images中。最后,我在Ubuntu 14.04上使用wkhtmltopdf 0.9.9,wicked-pdf 0.11.0和rails 4。

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

ruby ruby-on-rails-4 wicked-pdf
2个回答
14
投票

首先创建一个pdf模板来渲染和使用该模板中的wicked_pdf标签..例如 -

应用程序/视图/布局/ application.pdf.erb-

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

Ap /ゔie ws / pdf / pdf_ゔiew。 PDF格式。例如在RB

<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

这可能对你有帮助..


0
投票

我从'https'转换了'http'的图片网址。比工作。

Heroku-18
Rails 4.2
wicked_pdf (1.1.0)
wkhtmltopdf-binary (0.12.4)
© www.soinside.com 2019 - 2024. All rights reserved.