问题
My controller:
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
I'm getting the PDF along with page numbers, but I can't get the image header.
This is the layout:
pdf_header.html.erb
<%= image_tag "gla/image.jpg" ,:width => "90", :height=>"85" %>
<%#= wicked_pdf_image_tag "gla/image.jpg" %>
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
In the console I get this
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)
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?
回答1:
Acc. to official documnetation
<%= wicked_pdf_image_tag 'path' %> instead of <%= image_tag 'path' %>
this must work for you
<%= wicked_pdf_image_tag "gla/image.jpg" ,:width => "90", :height=>"85" %>
回答2:
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
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.
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.
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.
来源:https://stackoverflow.com/questions/23077118/image-header-on-pdf-using-wicked-pdf-gem-and-wkhtmltopdf