3

Has anybody used openERP/ odoo for printing invoices via XML-RPC. I've been 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 that I have so far, first of all i want to generate a report and then print it.

Rinor Dreshaj
  • 1,032
  • 12
  • 24

3 Answers3

3

I figured it out a simple way to do it, you just pass the id of invoice or order in the links and this dynamically creates a pdf for the report, or instead of the pdf you can use 'html' which generates an html ready for print for the invoice like this:

http://serverurl:port/report/html/account.report_invoice/(id of invoice);

Here is the code if it helps someone.

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;
            }
        }
Rinor Dreshaj
  • 1,032
  • 12
  • 24
  • 1
    This way is easy for the javascript client, because it has already made a session. But when made from outside the Cookie with: session_id is missing. And the server rejects the request. Any idea how to get it ? – yucer Mar 14 '16 at 15:22
2

There is another way that works even when session_id is missing. You should add a function on the server side which will return a 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}
Andrei
  • 117
  • 8
0

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()
Cœur
  • 37,241
  • 25
  • 195
  • 267
Clovis
  • 1
  • 2