使用wicked_pdf gem和wkhtmltopdf的PDF图像标题 [英] Image header on PDF using wicked_pdf gem and wkhtmltopdf

查看:107
本文介绍了使用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和页码,但无法获取图像标题.

I'm getting the PDF along with page numbers, but I can't get the image header.

这是布局:

pdf_header.html.erb

pdf_header.html.erb

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

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

Once I open pdf_header as an HTML file I get the image displayed, but once I call the PDF I'm not able to display the image

在控制台中,我得到了

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传递图像,从而直接在标题中显示图像?

The commented stuff is what I have tried and not was successful. Is there another way to display an image directly in the header by giving the path of the image instead of passing it via the html?

推荐答案

这是自定义帮助程序的实现:

This is the implementation of the custom helper:

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

这需要像这样,因为WickedPDF在运行发电机时不会加载导轨,它只能访问ruby.因此,没有本地主机,没有根路径,没有端口等.

This needs to be like this because WickedPDF doesn't load rails when it runs the generator, it only has access to ruby. Therefore there is no localhost, no root path, no port, etc.

您收到的不允许加载本地资源"错误似乎是一个权限问题.确保运行控制台的用户具有/images文件夹的权限.

The error you're getting of 'not allowed to load local resource' seems like a permissions issue. Make sure that the user running the console has rights over the /images folder.

如果所有操作均失败,则可以为图像指定一个绝对URL,如下所示:

If all of this fails, you can specify an absolute url for your image like so in your view:

<img alt="Image" src="http://localhost:3000/images/gla/image.jpg">

但是,如果要执行此操作,请记住,只要主机,端口或协议发生更改,此字符串也将需要更改.

But if you do this, keep in mind that whenever your host or port or protocol change, this string will need to be changed as well.

这篇关于使用wicked_pdf gem和wkhtmltopdf的PDF图像标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆