ruby – Wicked-PDF没有显示图像,’wicked_pdf_image_tag’未定义

我想生成一个包含我们部门徽标的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无法渲染图像?

解决方法

首先创建一个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>

应用程序/视图/ 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

这可能对你有帮助..

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...