歡迎轉載,但需標註出處,謝謝!
背景
近期在客戶的專案中發現在自定義報表樣式的時候,存在渲染為html正常,但是在生成pdf的時候,缺少樣式的情況。
分析
涉及到的odoo原始碼中的ir_actions_report.py檔案中的程式碼
def _prepare_html(self, html):
'''Divide and recreate the header/footer html by merging all found in html.
The bodies are extracted and added to a list. Then, extract the specific_paperformat_args.
The idea is to put all headers/footers together. Then, we will use a javascript trick
(see minimal_layout template) to set the right header/footer during the processing of wkhtmltopdf.
This allows the computation of multiple reports in a single call to wkhtmltopdf.
:param html: The html rendered by render_qweb_html.
:type: bodies: list of string representing each one a html body.
:type header: string representing the html header.
:type footer: string representing the html footer.
:type specific_paperformat_args: dictionary of prioritized paperformat values.
:return: bodies, header, footer, specific_paperformat_args
'''
IrConfig = self.env['ir.config_parameter'].sudo()
base_url = IrConfig.get_param('report.url') or IrConfig.get_param('web.base.url')
# Return empty dictionary if 'web.minimal_layout' not found.
layout = self.env.ref('web.minimal_layout', False)
if not layout:
return {}
layout = self.env['ir.ui.view'].browse(self.env['ir.ui.view'].get_view_id('web.minimal_layout'))
root = lxml.html.fromstring(html)
match_klass = "//div[contains(concat(' ', normalize-space(@class), ' '), ' {} ')]"
header_node = etree.Element('div', id='minimal_layout_report_headers')
footer_node = etree.Element('div', id='minimal_layout_report_footers')
bodies = []
res_ids = []
body_parent = root.xpath('//main')[0]
# Retrieve headers
for node in root.xpath(match_klass.format('header')):
body_parent = node.getparent()
node.getparent().remove(node)
header_node.append(node)
# Retrieve footers
for node in root.xpath(match_klass.format('footer')):
body_parent = node.getparent()
node.getparent().remove(node)
footer_node.append(node)
# Retrieve bodies
for node in root.xpath(match_klass.format('article')):
layout_with_lang = layout
# set context language to body language
if node.get('data-oe-lang'):
layout_with_lang = layout_with_lang.with_context(lang=node.get('data-oe-lang'))
body = layout_with_lang._render(dict(subst=False, body=lxml.html.tostring(node), base_url=base_url))
bodies.append(body)
if node.get('data-oe-model') == self.model:
res_ids.append(int(node.get('data-oe-id', 0)))
else:
res_ids.append(None)
if not bodies:
body = bytearray().join([lxml.html.tostring(c) for c in body_parent.getchildren()])
bodies.append(body)
# Get paperformat arguments set in the root html tag. They are prioritized over
# paperformat-record arguments.
specific_paperformat_args = {}
for attribute in root.items():
if attribute[0].startswith('data-report-'):
specific_paperformat_args[attribute[0]] = attribute[1]
header = layout._render(dict(subst=True, body=lxml.html.tostring(header_node), base_url=base_url))
footer = layout._render(dict(subst=True, body=lxml.html.tostring(footer_node), base_url=base_url))
return bodies, res_ids, header, footer, specific_paperformat_args
- 我們可以看到在
base_url = IrConfig.get_param('report.url') or IrConfig.get_param('web.base.url')
中取了系統引數中的report.url
或web.base.url
中的地址。 - 在
body = layout_with_lang._render(dict(subst=False, body=lxml.html.tostring(node), base_url=base_url))
中渲染了我們必要的樣式,若這裡的url出錯,那麼這裡我們將缺少樣式內容。當然,若是全部都通過html原生去實現樣式,那麼也是可以避免如上的問題。
解決方案
在系統引數新增report.url
或者web.base.url
的值為http://127.0.0.1:8069(填寫本機的地址)