Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.1wkhtmltopdf

ruby on rails (wkhtmltopdf header footer issue)


In local environment pdf with header footer generated successfully. but in hosting linux server environment pdf generated without header and footer.Below is my wkhtmltopdf (WickedPdf) config

WickedPdf.config = {
    #:wkhtmltopdf => "#{RAILS_ROOT}/pdfbin/wkhtmltopdf-amd64",
    :exe_path => "/home/software/.gems/bin/wkhtmltopdf",
    :layout => "layouts/pdf.html.erb",
    :margin => {    :top=> 40,
                    :bottom => 20,
                    :left=> 30,
                    :right => 30},
    :header => {:html => { :template=> 'layouts/pdf_header.html.erb'}},
    :footer => {:html => { :template=> 'layouts/pdf_footer.html.erb'}}
    #:exe_path => '/usr/bin/wkhtmltopdf'
}

for additional info :

this is my dir structure i am on linux rails hosting app\views\layouts , inside layouts i am having pdf.html.erb , pdf_footer.html.erb , pdf_header.html.erb The above stuff works perfectly on my local windows development environment , but in production pdf generated without headers and footers.so guys please help me to produce pdf with headers and footers

app/views/layouts/pdf.html.erb

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html dir="<%= (rtl?) ? 'rtl' : 'ltr' %>">
  <head>
    <% @direction = (rtl?) ? 'rtl/' : '' %>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <%= stylesheet_link_tag([@direction+'application', @direction+'_styles/ui.all'])%>
    <%= stylesheet_link_tag(*get_stylesheets) %>
    <%= stylesheet_link_tag  @direction+"_layouts/pdf" %>
    <link rel="stylesheet" type="text/css" href="<%="#{RAILS_ROOT}/public/stylesheets/#{@direction}_layouts/pdf.css" %>" media="all" />
    <link rel="stylesheet" type="text/css" href="<%="#{RAILS_ROOT}/public/stylesheets/#{get_stylesheets}.css"%>" media="all" />
    <link rel="stylesheet" type="text/css" href="<%= "#{RAILS_ROOT}/public/stylesheets/#{@direction}_styles/ui.all.css"%>" media="all" />


  </head>
  <body>

    <%= yield %>

  </body>
</html>

app/views/layouts/pdf_header.html.erb

<div id="pdf-header">
      <div class="logo">
        <% if FileTest.exists?("#{RAILS_ROOT}/public/uploads/image/institute_logo.jpg") %>
            <%= image_tag  "#{request.protocol}#{request.host_with_port}/uploads/image/institute_logo.jpg" ,:width => "90",  :height=>"85" %>
          <% else %>

          <div class="fed_logo">
            <%= image_tag  "#{request.protocol}#{request.host_with_port}/images/application/app_fedena_logo.jpg" ,:width => "90",  :height=>"85" %>
    </div>
          <% end %>
      </div>
      <div class="header-content">
        <p><%=Configuration.get_config_value('InstitutionName'); %></p>
        <p><%=Configuration.get_config_value('InstitutionAddress'); %></p>
      </div>

</div>

app/views/layouts/pdf_footer.html.erb

<div id="pdf-footer">
  <%= t('signature') %>
</div>

Note* The above codes working perfectly on my windows development environment (ie) PDF generated properly with header and footers. But when i hosted my application in linux production server PDF generated without headers and footers.*


Resolved Header Footer Issue , the above is the working code. The above problem is due to my files don't have enough executable permissions in production Linux environment.



Solution

  • Put .erb at the end of filename

    :header => {:html => { :template=> 'layouts/pdf_header.html.erb'}},
    :footer => {:html => { :template=> 'layouts/pdf_footer.html.erb'}}