使用wicked_pdf gem和wkhtmltopdf在PDF上的图像标题

我的控制器:

def show respond_to do |format| format.pdf do #render :pdf => "show",:template => "welcome/show",:footer => { :right => 'Page [page] of [topage]' }) #render :pdf => "show",:template => "welcome/show", :header => {:content => render_to_string({:template => 'welcome/pdf_header.html.erb'})}, :footer=> { :right => 'Page [page] of [topage]' },:margin => { :top => 38, :bottom => 35} #render :pdf => "show",:handlers => [:html],:template => "welcome/show.pdf.erb", :header => {:content => render_to_string({:layout => 'pdf_header.html.erb'})}, :footer=> { :right => 'Page [page] of [topage]' },:margin => { :top => 38, :bottom => 35} render :pdf => "show",:template => "welcome/show.pdf.erb", :header => {:content => ActionController::Base.new().render_to_string({:template => 'welcome/pdf_header.html.erb', :layout => false})}, :footer=> { :right => 'Page [page] of [topage]' },:margin => { :top => 38, :bottom => 35} end end end 

我正在获取PDF以及页码,但我无法获得图像标题。

这是布局:

pdf_header.html.erb

  "90", :height=>"85" %>  

一旦我打开pdf_header作为HTML文件,我就会显示图像,但是一旦我调用PDF,我就无法显示图像

在控制台中,我得到了这个

 Started GET "/welcome/show.pdf" for 127.0.0.1 at 2014-04-17 09:47:05 +0530 Processing by WelcomeController#show as PDF Rendered welcome/pdf_header.html.erb (0.4ms) ***************WICKED*************** Rendered welcome/show.pdf.erb (0.7ms) Rendered text template (0.0ms) Sent data show.pdf (1.8ms) Completed 200 OK in 782ms (Views: 1.3ms | ActiveRecord: 0.0ms) 

评论的东西是我尝试过但没有成功的东西。 是否有另一种方法通过提供图像的路径而不是通过html传递图像直接在标题中显示图像?

加。 官方documnetation

 <%= wicked_pdf_image_tag 'path' %> instead of <%= image_tag 'path' %> 

这一定对你有用

 <%= wicked_pdf_image_tag "gla/image.jpg" ,:width => "90", :height=>"85" %> 

这是自定义助手的实现:

 def wicked_pdf_image_tag(img, options={}) image_tag "file:///#{WickedPdfHelper.root_path.join('public', 'images', img)}", options end 

这需要像这样,因为WickedPDF在运行生成器时不加载rails,它只能访问ruby。 因此没有localhost,没有根路径,没有端口等。

您获得的“不允许加载本地资源”的错误似乎是权限问题。 确保运行控制台的用户拥有/images文件夹的权限。

如果所有这些都失败了,您可以在视图中为图像指定绝对URL:

 Image 

但是如果你这样做,请记住,无论何时主机或端口或协议发生变化,都需要更改此字符串。