Introduction
report_webkit is an odoo (formerly OpenERP) module that uses the wkhtmltopdf tool to render mako templates into PDF reports.
It includes multiple headers, footers and logos, javascript support, CSS support and other features.
Sometimes you want to print an element of the report at the bottom of the last page of the report. Take for instance the customer’s signature on a quotation. You need to print the 'Signature:'
string once, but if you put it at the end of the mako template, it could be printed in the centre of the page: it depends on the number of lines of the quotation.
The code
Let’s see how to solve this using the standard footer of a webkit report.
<head> <meta content="text/html; charset=UTF-8" http-equiv="content-type"/> <script> function subst() { var vars={}; var x=document.location.search.substring(1).split('&'); for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);} var x=['frompage','topage','page','webpage','section','subsection','subsubsection']; for(var i in x) { var y = document.getElementsByClassName(x[i]); var signature_div = document.getElementsByClassName('signature')[0]; if (vars['page'] == vars['topage']) { signature_div.style.visibility="visible"; } for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]]; } } </script> </head> <body style="border:0; margin: 0;" onload="subst()"> <div class="signature" style="visibility:hidden;">Signature:</div> <table style="border-top: 1px solid black; width: 100%"> <tr > <td style="text-align:right;font-size:12;" width="95%"> Page <span class="page"/> </td> <td style="text-align:left;font-size:12;"> of <span class="topage"/></td> </tr> </table> </body>
The interesting part is the element
<div class="signature" style="visibility:hidden;">Signature:</div>
and the javascript
var signature_div = document.getElementsByClassName('signature')[0]; if (vars['page'] == vars['topage']) { signature_div.style.visibility="visible"; }
This allows to use, within the footer, a div
element that is always hidden, except when vars['page'] == vars['topage']
, that is when we are on the last page of the report.
That’s all 🙂
Update March 2016
The module report_qweb_element_page_visibility is available.
From its description:
This module allows you to use 4 classes in QWEB reports:
- not-first-page: shows element in every page but first
- not-last-page: shows element in every page but last
- first-page: shows element only on first page
- last-page: shows element only on last page