服务器动作生成产品价格标签 - xiaohao0576/odoo-doc GitHub Wiki

价格标签的尺寸是 40mm x 30mm, DPI是203,所以对应的图像分辨率是 320 x 240 像素

html_body  = '''
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>产品标签 - 热敏打印优化</title>
    <style>
        /* 导入 Google Fonts */
        @import url('https://fonts.googleapis.com/css2?family=Hanuman:wght@700&display=swap'); /* 柬埔寨语,使用粗体确保清晰 */
        @import url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@700&display=swap'); /* 中文简体,使用粗体确保清晰 */

        body {
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 240px; /* 确保内容足够高度 */
            background-color: #FFFFFF; /* 强制背景为纯白色 */
        }
        .label-container {
            width: 320px;
            height: 240px;
            /* border: 1px solid #000000; */ /* 移除或改为纯黑边框,如果需要 */
            display: flex;
            flex-direction: column;
            align-items: center; /* 默认居中 */
            justify-content: space-between; /* 使内容均匀分布 */
            padding: 10px; /* 顶部和底部内边距,左右内边距将由 .product-name 控制 */
            box-sizing: border-box; /* 确保 padding 和 border 不会增加容器的实际尺寸 */
            font-family: 'Noto Sans SC', 'Hanuman', sans-serif;
            color: #000000; /* 强制所有文本为纯黑色 */
            background-color: #FFFFFF; /* 强制容器背景为纯白色 */
        }
        .product-name {
            width: 100%; /* **让产品名称占据父容器的全部宽度** */
            font-size: 1.1em; /* 稍微缩小字体以节省空间 */
            text-align: center; /* **文本居中** */
            margin-bottom: 5px; /* 减小底部边距 */
            color: #000000; /* 确保产品名称为纯黑色 */
            font-weight: bold; /* 增加字体粗细以提高清晰度 */
            max-height: 50px; /* 控制产品名称的最大高度 */
            overflow: hidden; /* 超出部分隐藏 */
            line-height: 1.2em; /* 调整行高以适应空间 */
            display: -webkit-box; /* 兼容性:使多行文本溢出显示省略号 */
            -webkit-line-clamp: 3; /* 最多显示3行 */
            -webkit-box-orient: vertical;
            padding: 0 5px; /* **新增:左右内边距 5px,上下内边距 0** */
            box-sizing: border-box; /* 确保 padding 不会增加宽度 */
        }
        .barcode-container {
            width: 260px;
            margin-bottom: 5px;
            text-align: center; /* **新增:文本居中** */
            min-height: 100px;
            /* 对于垂直居中,如果还需要,可能需要配合 line-height 或 padding-top/bottom */
            padding-top: calc((100px - 80px) / 2); /* 假设图片高80px,容器高100px */
            padding-bottom: calc((100px - 80px) / 2);
        }
        .price {
            font-size: 1.5em; /* 字体大小 */
            font-weight: bold; /* 粗体 */
            color: #000000; /* 纯黑色 */
            margin-top: 5px; /* 顶部边距 */
            width: 100%; /* 占据父容器的全部宽度 */
            text-align: center; /* **价格居中显示** */
            padding: 0 5px; /* 左右内边距 5px */
            box-sizing: border-box; /* 确保 padding 不增加宽度 */
        }
    </style>
</head>
<body>
    <div class="label-container">
        <div class="product-name">
            <NAME/>
        </div>
        <div class="barcode-container">
            <img src="https://minime-test03.odoo.com/report/barcode/EAN13/6974304613539?humanreadable=1&width=250&height=80"></img>
        </div>
        <div class="price">
            <PRICE_USD/>
        </div>
        <div class="price">
            <PRICE_KHR/>
        </div>
    </div>
</body>
</html>
'''
toimage = env['ir.actions.report']._run_wkhtmltoimage

bodies = []
exchange_rate = 4100
for r in records:
    html_body = html_body.replace('<NAME/>', r.name)
    html_body = html_body.replace('<BARCODE/>', r.barcode)
    html_body = html_body.replace('<PRICE_USD/>', f'${r.lst_price:,.2f}')
    
    price_khr = r.lst_price * exchange_rate
    rounded_price_khr = round(price_khr / 100) * 100
    html_body = html_body.replace('<PRICE_KHR/>', f'KHR {rounded_price_khr:,.0f}៛')
    
    bodies.append(html_body)

images = toimage(bodies, width=320, height=240, image_format='png')

for image, r in zip(images,records):
    img = b64encode(image).decode("utf-8")
    r['x_price_label'] = img

⚠️ **GitHub.com Fallback** ⚠️