Odoo 13 PDF报表打印:如何设置表格行级分页符
Odoo 13 PDF报表表格分页优化方案
问题描述
Odoo 13打印PDF报表时,若表格记录超出单页容量,表格会在触达页边距的任意位置拆分,导致整行内容跨两页显示,严重影响报表可读性。
解决方案
通过添加CSS样式控制表格行的分页行为,确保分页仅在行与行(记录之间)触发,同时保证分页后每页自动显示表头。
修改步骤
- 更新报表模板的CSS样式
在报表的<style>标签内新增以下规则:
/* 禁止表格行内部被分页拆分 */ .table-black-border tr { page-break-inside: avoid; } /* 强制表头在每页顶部重复显示 */ .table-black-border thead { display: table-header-group; }
修改后的完整样式代码如下:
.table-black-border { border: 3px solid black; border-collapse: collapse; width: 100%; } .table-black-border th, .table-black-border td { border: 1px solid black; padding: 10px; text-align: center; font-size: 16px; } /* 新增分页控制样式 */ .table-black-border tr { page-break-inside: avoid; } .table-black-border thead { display: table-header-group; }
- 验证效果
重新加载Odoo报表模板,打印包含大量记录的报表,确认表格仅在行之间分页,且每页都显示完整的表头与行内容。
修改后的完整报表模板代码
<template id="outgoing_report_template"> <t t-call="web.html_container"> <t t-call="web.external_layout"> <div class="page"> <style> .table-black-border { border: 3px solid black; border-collapse: collapse; width: 100%; } .table-black-border th, .table-black-border td { border: 1px solid black; padding: 10px; text-align: center; font-size: 16px; } /* 禁止行内分页 */ .table-black-border tr { page-break-inside: avoid; } /* 表头每页重复显示 */ .table-black-border thead { display: table-header-group; } </style> <img src="/custom_inventory/static/src/images/logo.png" alt="logo" width="150px" height="150px" style="float:left;margin-left:600px"/> <table dir="rtl" style="text-align:center;font-size:20px;margin-left:auto;" width="30%"> <tr> <td> <span>الوزارة/الدائرة ________________________</span> </td> </tr> <tr> <td> <span><![CDATA[ ]]></span> </td> </tr> <tr> <td> <span><![CDATA[ ]]></span> </td> </tr> <tr> <td> <pre style="border:none;">رقم ل</pre> </td> </tr> </table> <!-- Main report content --> <table dir="rtl" style="text-align:center;" width="100%"> <tr> <th colspan="4" style="font-size:48px;"> <span><u>مستند الاخراجات</u></span> </th> </tr> <tr> <td> <span><![CDATA[ ]]></span> </td> </tr> <tr style="font-size:16px;"> <td> <span>صرف الى _______________________</span> </td> <td> <span>من قبل ________________________</span> </td> <td> <span>للسيد ________________________</span> </td> <td> <span>التاريخ ________________________</span> </td> </tr> <tr> <td><span><![CDATA[ ]]></span></td> </tr> <tr style="font-size:16px;"> <td><span>بموجب طلب اللوازم رقم ________________________</span></td> <td><span>تاريخ ________________________</span></td> <td><span>موقع من ________________________</span></td> <td><span>توقيع مأمور المستودع ________________________</span></td> </tr> </table> <br/><br/> <!--main report table start--> <table class="table-black-border" style="float:right;" dir="RTL" width="100%"> <thead> <tr> <th rowspan="2" width="15%"><span>رقم التسجيل</span></th> <th rowspan="2"><span>نوع اللوازم و وصفها</span></th> <th rowspan="2"><span>الوحدة</span></th> <th rowspan="2"><span>الكمية</span></th> <th colspan="2"><span>سعر الوحدة</span></th> <th colspan="2"><span>سعر الاجمالي</span></th> <th rowspan="2"><span>الرصيد</span></th> <th rowspan="2"><span>خانة التخزين</span></th> <th rowspan="2"><span>الكمية المصروفة كتابة</span></th> </tr> <tr> <th><span>فلس</span></th> <th><span>دينار</span></th> <th><span>فلس</span></th> <th><span>دينار</span></th> </tr> </thead> <tr t-foreach="docs.move_ids_without_package" t-as="move_line"> <td><span t-field="move_line.reference"/></td> <td><span t-field="move_line.product_id"/></td> <td><span t-field="move_line.product_uom"/></td> <td><span t-esc="('%d' %(move_line.product_uom_qty))"/></td> <td> <div t-if="docs.origin"> <span t-esc="('%d' %(docs.get_report_price_unit(docs.origin, move_line.product_id.name) % 1 * 1000))"/> </div> <div t-if="not docs.origin"> <span t-esc="('%d' %(move_line.product_id.product_tmpl_id.list_price % 1 * 1000))"/> </div> </td> <td> <div t-if="docs.origin"> <span t-esc="('%d' %(docs.get_report_price_unit(docs.origin, move_line.product_id.name)))"/> </div> <div t-if="not docs.origin"> <span t-esc="('%d' %(move_line.product_id.product_tmpl_id.list_price))"/> </div> </td> <td> <div t-if="docs.origin"> <span t-esc="('%d' %(docs.get_report_price_subtotal(docs.origin, move_line.product_id.name) % 1 * 1000))"/> </div> <div t-if="not docs.origin"> <span t-esc="('%d' %(move_line.product_id.product_tmpl_id.list_price * move_line.product_uom_qty % 1 * 1000))"/> </div> </td> <td> <div t-if="docs.origin"> <span t-esc="('%d' %(docs.get_report_price_subtotal(docs.origin, move_line.product_id.name)))"/> </div> <div t-if="not docs.origin"> <span t-esc="('%d' %(move_line.product_id.product_tmpl_id.list_price * move_line.product_uom_qty))"/> </div> </td> <td><span t-field="move_line.location_id.name"/></td> <td><span t-field="move_line.location_dest_id.name"/></td> <td><span t-esc="docs.number2words(move_line.quantity_done)"/></td> </tr> </table> <!--main report table end--> <br/><span><![CDATA[ ]]></span><br/> <table dir="rtl" style="font-size:20px;text-align:center;" width="100%"> <tr> <td>تاريخ ________________________</td> <td>الوظيفة ________________________</td> <td>إسم المستلم ________________________</td> </tr> <tr> <td><span><![CDATA[ ]]></span></td> </tr> <tr> <td>نسخة المحاسب ________________________</td> <td></td> <td>توقيع المستلم ________________________</td> </tr> <tr> <td><span><![CDATA[ ]]></span></td> </tr> <tr> <td>قرار رقم ________________________</td> <td></td> <td>تاريخ ________________________</td> </tr> </table> </div> </t> </t> </template> </odoo>
原理说明
page-break-inside: avoid:指定PDF渲染引擎不要在表格行内部插入分页符,确保整行内容始终在同一页显示。display: table-header-group:强制表头元素以表格页眉组的形式渲染,实现分页后每页自动重复显示表头。
内容的提问来源于stack exchange,提问作者Evan
相关产品推荐
相关产品推荐

