欢迎转载,但需标注出处,谢谢!

背景

近期在客户的项目中发现在自定义报表样式的时候,存在渲染为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
  1. 我们可以看到在base_url = IrConfig.get_param('report.url') or IrConfig.get_param('web.base.url')中取了系统参数中的report.urlweb.base.url中的地址。
  2. 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(填写本机的地址)

【odoo】【知识点】生成pdf文件时缺少样式的问题的更多相关文章

  1. 在js内生成PDF文件并下载的功能实现(不调用后端),以及生成pdf时换行的格式不被渲染,word-break:break-all

    在js内生成PDF文件并下载的功能实现(不调用后端),以及生成pdf时换行的格式不被渲染,word-break:break-all 前天来了个新需求, 有一个授权书的文件要点击下载, 需要在前端生成, ...

  2. Itext生成pdf文件

    来源:https://my.oschina.net/lujianing/blog/894365 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等. ...

  3. Java 动态生成 PDF 文件

    每片文章前来首小诗:   今日夕阳伴薄雾,印着雪墙笑开颜.我心仿佛出窗前,浮在半腰望西天.  --泥沙砖瓦浆木匠 需求: 项目里面有需要java动态生成 PDF 文件,提供下载.今天我找了下有关了,系 ...

  4. JavaWeb项目生成PDF文件添加水印图片并导出

    一.前言 首先需要在Maven中添加相应的jar包依赖,若项目没用到Maven,也可自行下载相应所需的jar包(itextpdf.jar 与 itext-asian.jar),如下图所示.点此下载 M ...

  5. Java生成PDF文件(转)

    原文地址:https://www.cnblogs.com/shuilangyizu/p/5760928.html 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iT ...

  6. [itext]Java生成PDF文件

    一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...

  7. springMVC生成pdf文件

    pom.xml文件配置=== <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf --> <dependenc ...

  8. [轉載]史上最强php生成pdf文件,html转pdf文件方法

    之前有个客户需要把一些html页面生成pdf文件,然后我就找一些用php把html页面围成pdf文件的类.方法是可谓是找了很多很多,什么html2pdf,pdflib,FPDF这些都试过了,但是都没有 ...

  9. 怎么用PHP在HTML中生成PDF文件

    原文:Generate PDF from html using PHP 译文:使用PHP在html中生成PDF 译者:dwqs 利用PHP编码生成PDF文件是一个非常耗时的工作.在早期,开发者使用PH ...

随机推荐

  1. IDEA详细配置+优秀插件

    目录 IDEA破解 Settings配置 配置 settings 字体 关闭IDEA更新 设置IDEA打开为项目选择界面 自动导入包配置 显示方法的分割线 滚轮设置字体大小 智能提示忽略大小写 Tab ...

  2. IDEA部署了项目,其他页面可以正常访问,但访问tomcat的localhost:8080却出现404

    解决方案 点击Edit Configurations 点击右边的加号, 把目录放在你tomcat目录下的ROOT目录.之后就可以正常运行了

  3. php微信jsapi支付 支付宝支付 两码合一

    产品开会提出了这样的需求:一个二维码可以微信支付也可以支付宝支付 经过自己的钻研以及询问技术高人(本人代码一般般)和网上搜索 最终实现其功能  我用微信jsapi 和 支付宝网页支付 其实并不怎么难: ...

  4. mongodb oplog详解和格式分析

    1. 基本概念 oplog使用固定大小集合记录了数据库中所有修改操作的操作日志(新增.修改和删除,无查询),mongodb收到修改请求后,先在主节点(Primary)执行请求,再把操作日志保存到opl ...

  5. 动态路由协议、RIP

    动态路由协议.RIP      一.动态路由协议        1)动态路由协议概述        2)度量值        3)收敛        4)静态路由与动态路由的比较        5)动 ...

  6. C语言:2.1

    int main() { char zi='A'; short bla=10; int blb=20; long blc=30; float bld=340.56; double ble=34.324 ...

  7. python 得到字典的所有键 和值

    a={} a={"a":1,"b":2,"c":3,"d":4} print(a) print(a.items()) p ...

  8. C语言:指针

    #include <stdio.h> #include <stdlib.h> int sum(int a,int b) { int c; c=a+b; printf(" ...

  9. Java中的基本数据类型和引用数据类型的区别

    一.数据类型 Java中的数据类型分为两大类,基本数据类型和引用数据类型. 1.基本数据类型 基本数据类型只有8种,可按照如下分类 ①整数类型:long.int.short.byte ②浮点类型:fl ...

  10. [刘阳Java]_MySQL数据优化总结_查询备忘录

    数据库优化是在后端开发中必备技能,今天写一篇MySQL数据优化的总结,供大家看看 一.MySQL数据库优化分类 我们通过一个图片形式来看看数据优化一些策略问题 不难看出,优化有两条路可以选择:硬件与技 ...