1.webkit 打印需要安装的支持模块

请首先安装 Webkit 报表引擎(report_webkit),再安装 Webkit 报表的支持库(report_webkit_lib),该模块讲自动安装和设置 wkhtml2pdf 的路径。

另外,安装 oecn_base_fonts 模块可解决打印中文乱码问题,安装 web_pdf_preview 模块可以实现在浏览器中直接预览打印 pdf 文件。

2.webkit 打印模块文件结构

其中:hr_expense_report.xml 定义了报表名称和菜单,hr_expense_report.py 定义了报表解析类,hr_expense_report.mako 定义了报表模版。

3.报表定义文件(hr_expense_report.xml)

<?xml version="1.0" encoding="utf-8"?>
<openerp>
<data>
<report id="report_hr_expense_webkit"
name="hr.expense.webkit"
model="hr.expense.expense"
file="hr_expense_webkit\\report\\hr_expense_report.mako"
string="员工费用申请(html)"
report_type="webkit"
menu="True"
auto="False"
header="False" />
</data>
</openerp>

说明:

  id: 报表id,任意字符串,不重复即可。
  name: 报表名称,为报表解析类(hr_expense_report.py)中 webkit_report.WebKitParser() 函数第一个参数去掉 report. 。
  string: 打印菜单中显示的名字。
  model: 报表渲染对象,打印菜单创建在该对象上。
  file: 报表模版(.mako)文件路径,路径以本模块的路径开始,注意7.0早期版本webkit类型的报表中报表模版的路径必须用\\分割,7.0升级后的版本已经修正该Bug,使用/分割路径即可。
  report_type: 报表类型,此处为 webkit 。
    menu: True or False,是否在客户端上显示菜单, False 一般用语Wizard 向导时,不需要菜单。 

  auto: 是否利用缺省方法分析 .RML文件。


4.报表解析类定义文件(hr_expense_report.py)

# -*- coding: utf-8 -*-
import time
from report import report_sxw
from report_webkit import webkit_report class hr_expense_report_webkit(report_sxw.rml_parse):
def __init__(self, cr, uid, name, context):
super(hr_expense_report_webkit, self).__init__(cr, uid, name, context=context)
self.localcontext.update({
'time': time,
'cr':cr,
'uid': uid,
}) webkit_report.WebKitParser('report.hr.expense.webkit',
'hr.expense.expense',
'addons/hr_expense_webkit/report/hr_expense_report.mako',
parser=hr_expense_report_webkit, header=False)

说明:
  解释器继承自 report_sxw.rml_parse 对象 并且增加了 time、cr、uid 变量,因此在报表中可以调用。
  webkit_report.WebKitParser 实例生成之后,需要如下参数:
    报表的名字
    report关联的 object 名字
    mako文件的路径
    parser,默认的 mako 解释器(默认为 rml_parse)
    header=False,使用报表自己定义的报表头


5.hr_expense_report.mako

<html>
<head>
</head>
% for expense in objects:
<body>
<table width="1369" height="363" border="3" bordercolordark="#000000" frame="border" rules="groups">
<tr>
<td height="42" colspan="8"><h2 align="center">员工费用申请单</h2></td>
</tr>
<tr>
<td width="144" height="32">员工:</td>
<td width="146" height="32">${ expense.employee_id.name or '' }</td>
<td width="221" height="32">日期:</td>
<td height="32" colspan="2">${ expense.date or '' }</td>
<td width="150" height="32">部门:</td>
<td height="32" colspan="2">${ expense.department_id.name or '' }</td>
</tr>
<tbody>
<tr>
<td height="32">产品</td>
<td height="32">费用日期</td>
<td height="32">费用备注</td>
<td width="154" height="32">单号</td>
<td width="137" height="32"><div align="center">计量单位</div></td>
<td height="32"><div align="right">单价</div></td>
<td width="159" height="32"><div align="center">数量</div></td>
<td width="206" height="32"><div align="right">合计</div></td>
</tr>
% for line in expense.line_ids:
<tr>
<td width="144" height="32">${ line.product_id.name or '' }</td>
<td width="146" height="32">${ line.date_value or '' }</td>
<td width="221" height="32">${ line.name or '' }</td>
<td width="154" height="32">${ line.ref or '' }</td>
<td width="137" height="32"><div align="center">${ line.uom_id.name or '' }</div></td>
<td width="150" height="32"><div align="right">${ line.unit_amount or '' }</div></td>
<td width="159" height="32"><div align="center">${ line.unit_quantity or '' }</div></td>
<td width="206" height="32"><div align="right">${ line.total_amount or '' }</div></td>
</tr>
% endfor
<tr>
<td height="32" colspan="7"><div align="right">合计:</div></td>
<td height="32"><div align="right">${ expense.amount or '' }</div></td>
</tr>
</tbody>
<tr>
<td height="32">说明:</td>
<td height="32" colspan="7">${ expense.name or '' }</td>
</tr>
<tr>
<td height="32">备注:</td>
<td height="32" colspan="7">${ expense.note or '' }</td>
</tr>
</table>
</body>
% endfor
</html>

说明,以下关于Mako模版的介绍引用自:http://www.cnblogs.com/alangwansui/archive/2013/02/05/2892839.html

Mako的是用Python编写一个模板库。它提供了一个熟悉的,非XML的语法,编译成Python模块以获得最佳性能.

Mako 模板

语法

Mako模板可以解析 XML, HTML, email text, 等文字流(parsed from a text stream) .

Mako模板含有Mako特有的指令(Mako-specific directives), 包括变量、表达式替换(expression substitution),控制结构(比如条件和循环,conditionals and loops),服务器端命令,完整的Python代码块,这些就像不同功能的标签(tag)一样易用。所有这些指令都解析为Python代码.

这意味着在Mako模板中,你可以最大化发挥Python的优势.

表达式替换

最简单的表达式是变量替换。 Mako模板中使用 ${} 结构,而不是rml中的 [[ ]] .

eg:

this is x: ${x}

    上式被模板输出流解析(template output stream),从本地上下文(localcontext)传递给模板生成函数(template rendering function).

    标签 ${} 中的代码直接被Python解析.
控制结构:

在Mako中,控制结构 (i.e. if/else, 循环 (像 while 和 for), 包括 try/except) 都使用 % 标记,之后接上普通的Python控制表达式即可。在控制结构结束时,使用 "end<name>" 标记,"<name>" 是控制结构的关键字:

eg:

% if x==5:
this is some output
% endif

Python 块

在 <% %> 标记中, 你可以加入普通的Python代码块。虽然之中的代码可以加入任意的空格,但是还是注意下格式比较好。Mako的编译器会根据周围生成的 Python代码结构,调整Python代码块中的格式.

openerp学习笔记 webkit 打印的更多相关文章

  1. openerp学习笔记 跟踪状态,记录日志,发送消息

    跟踪状态基础数据: kl_qingjd/kl_qingjd_data.xml <?xml version="1.0"?><openerp>    <d ...

  2. openerp学习笔记 对象间关系【多对一(一对一)、一对多(主细结构)、多对多关系、自关联关系(树状结构)】

    1.多对一(一对一)关系:采购单与供应商之间的关系 'partner_id':fields.many2one('res.partner', 'Supplier', required=True, sta ...

  3. openerp学习笔记 模块结构分析

    以OpenERP7.0中的 hr_expense 模块为例: 如图中代码所示: __init__.py :和普通 Python 模块中的__init__.py 作用相同,主要用于引用模块根目录下的.p ...

  4. openerp学习笔记 计算字段支持搜索

    示例1: # -*- encoding: utf-8 -*-import poolerimport loggingimport netsvcimport toolslogger = netsvc.Lo ...

  5. openerp学习笔记 domain 增加扩展支持,例如支持 <field name="domain">[('type','=','get_user_ht_type()')]</field>

    示例代码1,ir_action_window.read : # -*- coding: utf-8 -*-from openerp.osv import fields,osv class res_us ...

  6. openerp学习笔记 自定义小数精度(小数位数)

    小数位数标识定义: lx_purchase/data/lx_purchase_data.xml <?xml version="1.0" encoding="utf- ...

  7. openerp学习笔记 视图样式(表格行颜色、按钮,字段只读、隐藏,按钮状态、类型、图标、权限,group边距,聚合[合计、平均],样式)

    表格行颜色:             <tree string="请假单列表" colors="red:state == 'refuse';blue:state = ...

  8. openerp学习笔记 domain 的应用

    1.在Action中定义,domain用于对象默认的搜索条件: 示例: <record id="action_orders" model="ir.actions.a ...

  9. openerp学习笔记 日期时间相关操作

    日期格式化字符串:DATE_FORMAT = "%Y-%m-%d" 日期时间格式字符串:DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" ...

随机推荐

  1. CNZZ每天百亿条日志写入,SLS+ODPS轻松拆招

    如果你是一个站长,想要提交一个查询,从一亿多条日志中找出从湖南省发出.使用ISP电信.通过百度搜索跳转到达的访问日志.该怎么做? 别急,在接收到您的查询条件后,CNZZ可以快速通过SLS(简单日志服务 ...

  2. 在虚拟环境中安装pygame

    http://www.pygame.org/wiki/CompileUbuntu#Python%203.x%20into%20virtual%20environment 先安装依赖: ᐅ sudo a ...

  3. 实用技术——Eclipse 常用快捷键

    Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率. 和编辑相关的快捷键 Eclipse中有如下一些和编辑相关的快捷键.  1. [ALT+/]  此快捷键为用户编 ...

  4. 文本替换sed+字段处理cut,join+awk重新编排字段

    [1]sed工具(Stream Editor)--流编辑器 sed 本身也是一个管线(管道)命令,可以分析 standard input 的啦! 而且 sed 还可以将数据进行取代.删除.新增.截取特 ...

  5. spring--事务原理

    Spring支持以下7种事务传播行为. 传播行为 XML文件 propagation值 含义 PROPAGATION_REQUIRED REQUIRED 表示当前方法必须在一个具有事务的上下文中运行. ...

  6. Flume与Kafka集成

    一.Flume介绍 Flume是一个分布式.可靠.和高可用的海量日志聚合的系统,支持在系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并写到各种数据接受方(可定制)的能 ...

  7. Wordpress 运行缓慢的解决方法

    原因为wordpress 3.8之后采用google字体所致.   修改 functions.php    //禁用Open Sans class Disable_Google_Fonts { pub ...

  8. C#操作ini

    /// <summary> /// 读写INI文件的类. /// </summary> public class INIHelper { // 读写INI文件相关. [DllI ...

  9. C#的Process类的一些用法

    c#之process类相关整理 一.根据进程名获取进程的用户名? 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using Sys ...

  10. 通过Scrapy抓取QQ空间

    毕业设计题目就是用Scrapy抓取QQ空间的数据,最近毕业设计弄完了,来总结以下: 首先是模拟登录的问题: 由于Tencent对模拟登录比较讨厌,各个防备,而本人能力有限,所以做的最简单的,手动登录后 ...