如何为Brother RJ-4040打印机创建动态高度纸张格式记录?
Let's tackle this roll paper printing issue step by step. The core problem here is that your fixed page height is forcing unwanted pagination (and repeated headers), while roll paper needs to print as a single continuous sheet with only one header. Here's the best approach to solve this:
1. Adjust the Paper Format for Continuous Printing
First, forget about setting page_height to -1 or 0—Odoo doesn't handle those values correctly for custom paper formats. Instead, use an extremely large height value (like 9999) to ensure Odoo treats the entire report as a single "page" that can accommodate all your content. This mimics how POS receipts work behind the scenes.
Update your paperformat record like this:
<record id="paperformat_stock_picking_rj4040brother" model="report.paperformat"> <field name="name">RJ-4040 Brother</field> <field name="default" eval="True"/> <field name="format">custom</field> <field name="page_height">9999</field> <!-- Large enough to fit any length of picking --> <field name="page_width">102</field> <field name="orientation">Portrait</field> <field name="margin_top">40</field> <field name="margin_bottom">23</field> <field name="margin_left">3</field> <field name="margin_right">3</field> <field name="header_line" eval="False"/> <field name="header_spacing">0</field> <!-- Disable default header spacing since we're using a custom header --> <field name="dpi">80</field> </record>
2. Modify Your Report Template to Avoid Pagination & Repeated Headers
The reason your header is repeating is that Odoo's default report rendering repeats the header section on every page. Since you're using a custom header, you need to embed it directly in the report body (not in the Odoo-defined header area) and ensure the entire report content is contained in a single, non-paginated container.
Example Template Structure:
<template id="report_stock_picking_rj4040"> <t t-call="report.html_container"> <t t-foreach="docs" t-as="doc"> <!-- Single container for the entire receipt --> <div class="continuous-report"> <!-- Custom Header (only prints once) --> <div class="custom-header"> <!-- Add your header content here: company logo, picking number, date, etc. --> <h1>Picking #<t t-esc="doc.name"/></h1> <p>Date: <t t-esc="doc.scheduled_date"/></p> </div> <!-- Picking Lines (continuous, no page breaks) --> <div class="picking-lines"> <t t-foreach="doc.move_line_ids" t-as="line"> <div class="line-item"> <span><t t-esc="line.product_id.name"/></span> <span><t t-esc="line.product_uom_qty"/> <t t-esc="line.product_uom.name"/></span> </div> </t> </div> <!-- Custom Footer (optional) --> <div class="custom-footer"> <p>Thank you!</p> </div> </div> </t> </t> </template>
Add CSS to Prevent Unwanted Page Breaks
Add this CSS to your report to ensure no accidental pagination within content sections:
.continuous-report { page-break-inside: avoid; page-break-after: avoid; } .custom-header, .picking-lines, .custom-footer { page-break-inside: avoid; } .line-item { margin: 2px 0; }
3. Why This Works
- The large
page_heighttells Odoo that the entire report can fit on one page, so it won't split your content into multiple pages. - By placing your custom header in the report body (not the Odoo header), it only renders once at the top of the continuous sheet.
- The CSS prevents any unintended page breaks within your header, line items, or footer.
Optional: Dynamic Height Adjustment (If Needed)
If for some reason the large height approach doesn't work (e.g., specific printer constraints), you could extend the get_pdf method to calculate the actual content height and adjust the paper format dynamically. However, this is overkill for most roll paper scenarios—above steps should solve your issue without needing custom Python code.
内容的提问来源于stack exchange,提问作者forvas

