Fernando Flores
Fernando Flores

Reputation: 23

wicked_pdf not rendering template nor footer on rails 7

I am trying to render a document as pdf using wickedpdf on a rails 7 app. It works fine BUT when I try to use the --header and --footer to print pdf as stationery, wicked/wkhtmltopdf just ignores the template html and the footer template, so I end up with a nice empty stationery page (header is rendered correctly with images and erb). I have read several posts here about the same problem but none helps my problem. Please help me, I should be missing some details These are the wickedpdf and wkhtmltopdf versions:

wicked_pdf (2.7.0)
wkhtmltopdf 0.12.6.1 (with patched qt)

Also... This is what bundle install shows:

Using wicked_pdf 2.7.0
Using wkhtmltopdf-binary 0.12.6.6
Using wkhtmltopdf-heroku 2.12.6.0

Here is the render command I'm using in the controller action that triggers the pdf directly:

def approve
    @quotation.update(state: "autorizada")
    render pdf: "cotizacion#{@quotation.id}",
           template: 'authorize/approve',
           layout: 'pdf',
           page_size: 'letter',
           page_height: '11in',
           page_width: '8.5in',
           default_header: false,
           print_media_type: true,
           formats: [:html],
           margin: { top: 10, bottom: 10, left: 5, right: 5 },
           header: { html: { template: 'layouts/pdf_header' }, layout: 'pdf' },
           # This was also tried:
           # footer: { html: { template: 'layouts/pdf_footer' }, layout: 'pdf' },
           footer: { content: render_to_string('layouts/pdf_footer') },
           delete_temporary_files: true
  end

Here is the app/views/layouts/pdf.html.erb layout

<!DOCTYPE html>
<HTMl>
  <head>
    <title><%= "Cotización: #{@quotation.id}" %></title>
    <%= wicked_pdf_stylesheet_link_tag "pdf" %>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
  </head>
  <body>
    <aside>
      <div id="upper-bar"></div>
      <div id="lower-bar"></div>
    </aside>
    <div style="padding: 0em 1em 0em 1em;">
      <%= yield %>
    </div>
  </body>
</HTMl>

This is the app/views/authorize/approve.html.erb template

<div>
    <table class="show-table pdf-table" style="margin-left:15px;">
      <tr>
        <td>Cotización:</td>
        <td><%= @quotation.id %></td>
      </tr>
      <tr>
        <td>Cliente:</td>
        <td><%= @quotation.request.client.name %></td>
      </tr>
      <tr>
        <td>Contacto:</td>
        <td><%= @quotation.request.client.email %></td>
      </tr>
      <tr>
        <td>Total:</td>
        <td><strong><%= number_to_currency(@quotation.total) %></strong></td>
      </tr>
    </table>
    <div>
      <h4>Please see below the quotation for the required service.</h4>
    </div>
  </div>
    
  <% if @quotation.real_service_group.present? %>
    <% @quotation.real_service_group.tap do |rsg| %>
      <table class="cot-table">
        <thead>
          <tr>
            <th colspan="4">Servicio: <%= rsg.title %></th>
          </tr>
          <% servicio_total = 0 %>
          <% rsg.real_segments.each do |rs| %>
            <tr>
              <th>Segmento: <%= rs.title %></th>
              <th colspan="3">Moneda: <%= rs.currency %></th>
            </tr>
            <tr>
              <th>Concepto</th>
              <th>Cantidad</th>
              <th>Precio Unitario</th>
              <th>importe</th>
            </tr>
          </thead>
                <% rs_subtotal = 0 %>
                <% rs.real_quote_lines.each do |rql| %>
                  <tr>
                    <td><%= rql.quote_concept %></td>
                    <td><%= rql.quantity %></td>
                    <td><%= rql.unit_price %></td>
                    <td><%= number_to_currency(rql.quantity * rql.unit_price) %></td>
                    <% rs_subtotal += rql.quantity * rql.unit_price %>
                  </tr>
                <% end %>
                <tr style="background-color: darkgrey;">
                  <td colspan="3" class="text-end">Subtotal:</td>
                  <td><%= number_to_currency(rs_subtotal) %></td>
                </tr>
                <% servicio_total += rs_subtotal %>
          <% end %>
          <tr style="background-color: cadetblue;">
            <td colspan="3" class="text-end">Total:</td>
            <td><%= number_to_currency(servicio_total) %></td>
          </tr>
        <% end %>
      </table>
      <div class="container text-center">
        <p class="signature">Dirección</p>
        <p class="signature">Mtro.  – Dirección logística</p>
        <div style="margin-left:220px;padding-left:100px;">
          <p><%= image_tag wicked_pdf_asset_base64('signature.jpg'), class: 'img-signature signature' %></p>
        </div>
        <p class="signature">HARD BOX COMPRA Y VENDE EN EL MUNDO S. de R. L de C.V</p>
        <p class="signature">Director de logística</p>
        <p class="signature">Tel: </p>
        <p class="signature">Cel.: </p>
        <p class="signature">somethingr.com.mx</p>
      </div>
  <% end %>
  <div>
    <% if @quotation.notes.present? %>
      <h3>Términos y Condiciones:</h3>
      <% @quotation.notes.shift %>
        <% @quotation.notes.each_with_index do |note, index| %>
          <p style="border:2px solid rgb(234, 236, 236);border-radius:10px;padding:10px;text-align:left;"><%= index+1 %> ) <%= note %></p>
        <% end %>
    <% end %>
  </div>

This is app/views/layouts/pdf_header.html.erb

<div>
  <header>
    <div class="show-division-head">
      <div style="float:left;margin-left:-100px;">
        <%= image_tag wicked_pdf_asset_base64('HBlogoFull.png'), class: 'img-fluid', size: '150x150' %>
      </div>
      <div style="float:right;">
        <%= image_tag wicked_pdf_asset_base64('hardboxAsegura.jpg'), class: 'img-fluid', size: '500x100' %>
      <div>
        <h4 style="float:right;">
          Aguascalientes, Ags. <%= Date.today.to_formatted_s(:long)%>
        </h4>
      </div>
    </div>
  </header>
</div>

And this is app/views/layouts/pdf_footer.html.erb

<footer>
  <div class="footer-text">
    <p>Av. Las Americas 65436. 100, neighborhood, 20239 Aguascalientes, Ags.</p>
    <p>TEL: 449 343 45643, 449 6437</p>
    <p>www.mexicanclient.com.mx</p>
  </div>
</footer>

I have tried several stackOverflow postings on this subject with no success, I've also tried several options, like if I put the header and footer code within the app/views/layouts/pdf.html.erb directly I get both header and foother AND the template content but header and footer only once and not in every page as it should be as in a stationery paper. I hope you can help me, thank you.

Upvotes: 0

Views: 188

Answers (0)

Related Questions