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. 新手教程之使用Xib自定义UITableViewCell

    新手教程之使用Xib自定义UITableViewCell 前言 首先:什么是UITableView?看图 其次:什么是cell? 然后:为什么要自定cell,UITableView不是自带的有cell ...

  2. mybatis--MapperProxy事务

    上篇 详细分析了org.mybatis.spring.mapper.MapperScannerConfigurer 和 org.mybatis.spring.SqlSessionFactoryBean ...

  3. FreeBSD 安裝 Tomcat JAVA JDK1.6 筆記

    首先是安裝軟體 cd /usr/ports/java/jdk16/ make 在這一步,需要你手動到sun.com上下載幾個安裝包,按提示下載好後加入到 /usr/ports/distfiles/,再 ...

  4. 【转】对于移动APP测试的一个小技巧

    目标:目前越来越多的应用要支持移动设备,html5的推出,方便了页面对移动app的支持,那么我们该如何有效的去测试同时支持app和web的代码?web的测试可以使用浏览器的一些工具来辅助测试,比如ff ...

  5. 二十二、android中application标签说明

    <application> <applicationandroid:allowClearUserData=["true" | "false"] ...

  6. 九、Android学习笔记_ Android开发中使用软引用和弱引用防止内存溢出

    在<Effective Java 2nd Edition>中,第6条“消除过期的对象引用”提到,虽然Java有 垃圾回收机制,但是只要是自己管理的内存,就应该警惕内存泄露的问题,例如的对象 ...

  7. Part 17 Temporary tables in SQL Server

    Temporary tables in SQL Server

  8. 什么叫wipe,安卓用户如何去wipe?

    一.wipe是什么意思 wipe从英文单词的字面意思来理解就是:揩,擦;揩干,擦净的意思,从刷机爱好者的专业角度来理解可以认为是一种对手机数据擦除的操作.关于wipe是什么意思比较专业的解答为:wip ...

  9. Hadoop集群错误

    1.Hadoop集群所有的DataNode都启动不了解决办法 删除从节点.../usr/hadoop/tmp/dfs/ 下内容,再重新格式化namenode

  10. swift 闭包简写实际参数名$0、$1等理解

    Swift 自动对行内闭包提供简写实际参数名,你也可以通过 $0 , $1 , $2 等名字来引用闭包的实际参数值. 如果你在闭包表达式中使用这些简写实际参数名,那么你可以在闭包的实际参数列表中忽略对 ...