如何通过 XML-RPC 从 odoo/openERP 打印

How to print from odoo/openERP via XML-RPC

是否有人使用 openERP/odoo 通过 XML-RPC 打印发票。我一直在尝试创建一个 xml rpc 打印方法,但没有成功。

 function printInvoice($values,$model){

        $print = new xmlrpc_client($this->server."report");
        $print->return_type = 'phpvals';

        foreach($values as $k=>$v){
            $nval[$k] = new xmlrpcval( $v, xmlrpc_get_type($v) );
        }

        $msg = new xmlrpcmsg('report');

        $msg->addParam(new xmlrpcval($this->database, "string")); 
        $msg->addParam(new xmlrpcval($this->uid, "int")); 
        $msg->addParam(new xmlrpcval($this->password, "string"));
        $msg->addParam(new xmlrpcval($model, "string"));
        $msg->addParam(new xmlrpcval("report", "string"));
        $msg->addParam(new xmlrpcval(87, "int"));
        $msg->addParam(new xmlrpcval($nval,"struct"));

        $resp = $print->send($msg);



        if ($resp->faultCode())
            return $resp->faultString(); 
        else
            return $resp->value();  

    } 

这是我到目前为止的代码,首先我想生成一个报告然后打印它。

我想出了一个简单的方法,您只需在链接中传递发票或订单的 ID,这会为报告动态创建一个 pdf,或者您可以使用 'html' 生成一个 html 准备打印的发票,如下所示:

http://serverurl:port/report/html/account.report_invoice/(发票编号);

如果对某人有帮助,请提供代码。

function printInvoice($id,$type){


            if($type == 'invoice')
            {
                return "http://serverurl:port/report/pdf/account.report_invoice/".$id;
            }
            else if($type == 'order')
            {
                return "http://serverurl:port/report/pdf/sale.report_saleorder/".$id;
            }
            else
            {
                return false;
            }
        }

在python...

import time
import base64

printsock = xmlrpclib.ServerProxy('http://server:8069/xmlrpc/report')
model = 'account.invoice'
id_report = printsock.report(dbname, uid, pwd, model, ids, {'model':    model, 'id': ids[0], 'report_type':'pdf'})
time.sleep(5)
state = False
attempt = 0
while not state:
    report = printsock.report_get(dbname, uid, pwd, id_report)
    state = report['state']
    if not state:
        time.sleep(1)
    attempt += 1
    if attempt>200:
        print 'Printing aborted, too long delay !'

    string_pdf = base64.decodestring(report['result'])
    file_pdf = open('/tmp/file.pdf','w')
    file_pdf.write(string_pdf)
    file_pdf.close()

即使缺少 session_id,还有另一种方法仍然有效。您应该在服务器端添加一个函数,它将 return 一个 pdf:

from openerp import models, api
from openerp.http import request


class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    @api.multi
    def json_pdf(self):
        request.website_multilang = False

        pdf = self.env['report'].get_pdf(self, 'account.report_invoice')
        if pdf:
            return {'data': pdf.encode('base64'), 'name': self.number}
        else:
            return {'error': 'Attachment not found', 'name': self.number}