Image header on PDF using wicked_pdf gem and wkhtmltopdf
Asked Answered
G

2

2

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?

Gemini answered 15/4, 2014 at 7:26 Comment(2)
Why ActionController::Base.new().render_to_string instead of just render_to_string? I would write as: :header => { :html => { :template => 'welcome/pdf_header.html.erb', :layout => false } }Homozygote
@Homozygote i tried it but i get cant convert nil to integerColeoptile
I
2

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" %>
Incipit answered 15/4, 2014 at 7:32 Comment(5)
provide name of the file with extensions. welcome/pdf_header.html.erbIncipit
I tried dat too...using wicked_pdf_image_tag and once I open the page as HTML I get this image source <img alt="Image" src="file:///home/user/Ruby_projects/wicked_pdf/images/gla/image.jpg">and in the console I get this error Not allowed to load local resource: file:///home/user/Ruby_projects/wicked_pdf/images/gla/image.jpg Coleoptile
yes it is proper..with page number and some text on the show viewColeoptile
I believe that must be the prob. in your case. If image_tag works fine in html page, then wicked_pdf_image_tag must also work fine I beleiveIncipit
is there another way to put the image by passing the path in the header?Coleoptile
E
1

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.

Engler answered 25/4, 2014 at 14:4 Comment(1)
I am using an html img tag in the layout that I have defined and I am providing the absolute URL as you have mentioned above. I tried with both http and https, but neither of them is working. I also tried putting :show_as_html => true, but thi is also failing.Skaggs

© 2022 - 2024 — McMap. All rights reserved.