是否有人使用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();
}
这是我到目前为止的代码,首先我想生成一个报告,然后打印出来。
答案 0 :(得分:3)
我想出了一个简单的方法,你只需要在链接中传递发票或订单的ID,这会动态地为报告创建一个pdf,或者你可以使用'html'代替pdf来生成一个html准备打印发票如下:
http://serverurl:port/report/ html /account.report_invoice/(发票ID);
以下是帮助某人的代码。
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;
}
}
答案 1 :(得分:2)
即使缺少session_id,还有另一种方法可行。您应该在服务器端添加一个函数,它将返回一个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}
答案 2 :(得分:0)
在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()