How to print from odoo / openERP via XML-RPC
Has anyone used openERP / odoo to print invoices via XML-RPC. I am trying to create an xml rpc method for printing with no success.
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();
}
this is the code i have so far, first of all i want to generate a report and then print it.
+3
source to share
4 answers
I figured it was an easy way to do it, you just pass the invoice or order id in the links and this dynamically generates a pdf for the report, or instead of pdf you can use "html" which generates printable html for invoice as follows way:
http: // serverurl: port / report / html /account.report_invoice/(invoice id);
Here is the code in case it helps anyone.
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;
}
}
+3
source to share
There is another way that works even if the session_id is missing. You have to add a server side function that will return the 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
source to share
In 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()
0
source to share