Hi Jason
I was struggling with this issue too for a long time, and have not found a really clean solution. Currently I am using the following workaround:
- Create a helper report with exactly the same content (just duplicate the existing one)
- Write a method on the report model, which renders the helper report, and then analyzes the pdf and returns the page count
- Use the method in the final report, it will output the page count of the report, with this you can then dynamically set the position absolute top of the footer.
Below are the code examples.
Relevant Parts of the XML in the final report (only copied the relevant lines here)
<t t-set="page_count_page_2" t-value="o.get_report_page_count('custom_module.pdf_quote_report_helper_template')"/>
<t t-set="footer_top" t-value="1048.3 if page_count_page_2 == 2 else 670.6"/>
<!-- Footer is positioned dynamically according to page content using footer_top -->
<div t-att-style="'position: absolute; left: 0mm; width: 292mm; height: 85mm; background: #122439; top: %.1fmm;' % footer_top">
Render Report Page Count:
def get_report_page_count(self, report_name):
"""
This method is used to render a helper report inside the sale quotation report and returns
its page count. This is needed for the dynamic positioning of page 2 footer, because the
footer should only be shown at the bottom of the last page of the budget and payment details
"""
self.ensure_one()
report_action = self.env.ref(report_name)
pdf_content, content_type = self.env['ir.actions.report']._render_qweb_pdf(report_action.id, [self.id])
pdf_stream = io.BytesIO(pdf_content)
pdf_reader = PdfReader(pdf_stream)
return len(pdf_reader.pages)