如何在Odoo中移除Sale Order Report页眉页脚,生成空白头尾自定义报表
Hey there! I’ve tackled this exact requirement before, so here’s a practical, step-by-step solution to build a custom report that mirrors the sale.order report’s structure but has completely empty headers and footers:
1. Create a Custom QWeb Report Template (Inherit from Sale Order Report)
The easiest way to match the sale.order report’s look while stripping headers/footers is to inherit the existing sale order report template and override its header/footer sections. Add this XML to your custom module’s report file:
<odoo> <template id="custom_sale_order_no_header_footer" inherit_id="sale.report_saleorder"> <!-- Replace existing header with empty content --> <xpath expr="//div[@class='header']" position="replace"> <div class="header"></div> </xpath> <!-- Replace existing footer with empty content --> <xpath expr="//div[@class='footer']" position="replace"> <div class="footer"></div> </xpath> </template> </odoo>
This keeps all the core sale order report logic intact while removing any header/footer content.
2. Define the Report Action
Next, link your custom template to the sale.order model by adding a report action in your module’s XML:
<odoo> <report id="report_custom_sale_order" model="sale.order" string="Custom Sale Order (No Header/Footer)" report_type="qweb-pdf" name="your_module_name.custom_sale_order_no_header_footer" file="your_module_name.custom_sale_order_no_header_footer" attachment_use="False" /> </odoo>
Replace your_module_name with the actual name of your custom module. This adds a new print option to the sale order form view.
3. Fix PDF Margins (Critical for Clean Output)
Even with empty headers/footers, wkhtmltopdf (Odoo’s default PDF renderer) might leave default margins or placeholder content. Add this style template to eliminate those:
<odoo> <template id="custom_sale_order_report_styles" inherit_id="web.report_assets_common"> <xpath expr="." position="inside"> <style> @page { /* Remove default header/footer content */ @top-left { content: none; } @top-right { content: none; } @bottom-left { content: none; } @bottom-right { content: none; } /* Adjust page margins to remove empty header/footer space */ margin-top: 0.5cm; margin-bottom: 0.5cm; } .page { padding: 0; } </style> </xpath> </template> </odoo>
This ensures no leftover header/footer elements appear in the final PDF.
4. Activate Your Custom Report
- Install or update your custom module in Odoo.
- Navigate to any sale order, click the Print button, and select your new report option ("Custom Sale Order (No Header/Footer)").
- The generated PDF will match the standard sale order report’s layout but have completely empty headers and footers.
Alternative: Build a Full Custom Template (If Inheritance Isn’t Ideal)
If you need full control over the report structure, you can copy the entire sale.report_saleorder template code into your custom module, then manually delete all header and footer sections. Just make sure to update the report action to point to your full template instead of the inherited one.
内容的提问来源于stack exchange,提问作者Djamel-eddine Bouzegzeg

