歡迎轉載,但需標註出處,謝謝!javascript
近期在客戶的項目中發如今自定義報表樣式的時候,存在渲染爲html正常,可是在生成pdf的時候,缺乏樣式的狀況。html
涉及到的odoo源碼中的ir_actions_report.py文件中的代碼java
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(填寫本機的地址)
node