5

I user mac osx and try my html file to pdf file via wickedpdf. I want to put a string every page of my pdf file but I have a problem about header which is not rendering.

My wickedpdf method is,

format.pdf do
        render :pdf => '#{@examination.name}.pdf',
               :disposition => 'inline',
               :layout => 'examination_session_pdf.html.erb',
               :no_background => true,
               :header =>{:html =>{:template=>'shared/pdf/header.pdf.erb'}}
      end

and the header file contains just "hello" string or nothing. However, every time I see this error,

can't convert nil into String

The problem line is ":header =>{:html =>{:template=>'shared/pdf/header.pdf.erb'". In addition, I cannot see any logs about rendering the header page on the console.

How can I fix it?

eayurt
  • 1,169
  • 1
  • 19
  • 42
  • possible duplicate of [wicked\_pdf is not rendering header](http://stackoverflow.com/questions/11529912/wicked-pdf-is-not-rendering-header) – Besi Feb 27 '15 at 10:06

2 Answers2

6

I hit the exact same problem earlier today!

This is what Ive done to get it to work instead


    format.pdf do
        render :pdf => "#{@inv.invno}.pdf",
               :template => "inv/show.pdf",
               :layout =>'pdf',
               :header => { :content => render_to_string({:template => 'inv/header.pdf.erb'})},
               :footer => { :content => render_to_string({:template => 'inv/footer.pdf.erb'})},
               :margin => { :top => 38, :bottom => 35}
        end

You will see Ive actually used the render_to_string and then stuck the result in to the header or footer via :content. This works very well for me.

You can ignore the :margin section as Im just using that to space things out nicely as the header and footers both contain graphics.

Hope this helps!

Jonathan
  • 73
  • 3
  • This gives me: `config/initializers/wicked_pdf.rb:46:in ': undefined method render_to_string' for main:Object (NoMethodError)` I'm using rails 3.2.16 – Don Giulio Jul 29 '14 at 12:58
  • @DonGiulio You need to share your full code to check this issue at your end or i think you need to remove gem and install gem again. – Ayush Kanoongo Apr 27 '19 at 04:16
3

The solution for me was noted on this issue.

Make sure you have

<!DOCTYPE html>

at the top of your header template file.

rmcsharry
  • 5,363
  • 6
  • 65
  • 108
  • 1
    This was exactly what I needed. I had to add it to my header template even though the exact same template rendered perfectly as a footer without it. – ChrisW Nov 16 '17 at 12:44