2

我有一个带按钮的向导。在按钮操作上,我想运行报告并将 PDF 保留在服务器上。我有上面的代码片段,它使用 Web 服务创建报告。但是在向导上下文中,我通常只有 uid(我认为)。

在向导中将报告写入磁盘的等效方法是什么?

def reportToDisk(self, cr, uid, ids, context=None):

    dbname = 'db'
    username = 'user'
    pwd = 'pass'
    model = 'sale.order'
    report_name = 'doc.sale'

    sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
    uid = sock_common.login(dbname, username, pwd)

    sock = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/object')
    ids = sock.execute(dbname, uid, pwd, model, 'search',[])[0:1]

    sock_report = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/report')
    id_report = sock_report.report(
            dbname, uid, pwd, report_name, ids,{'model': model, 'id': ids[0], 'report_type':'pdf'}
    )

    cont = True
    while cont:
            report = sock_report.report_get(dbname, uid, pwd, id_report)
            cont = not report['state']

    string_pdf = base64.decodestring(report['result'])
    file_pdf = open('/home/arch-in/file.pdf','w')
    file_pdf.write(string_pdf)
    file_pdf.close()
4

1 回答 1

2

在您的按钮单击时返回报告操作(它可以是向导按钮或查看按钮,它仅适用于按钮单击返回),如下所示:

  def btn_clik_action(self, cr, uid, ids, context=None):
        if context == None:
            context = {}
        value =  {
        'type': 'ir.actions.report.xml', 
        'report_name':'report.name.(servicename)',
        'datas': {
                  'model':'model.name',
                  'id': ids and ids[0] or False,
                  'ids': ids and ids or [],
                  'report_type': 'pdf'
                 },
           'nodestroy': True
       }

只需返回报告操作将为您提供报告文件,基本上您不需要编写或任何内容。

谢谢你

于 2012-12-26T12:23:21.833 回答