How can I download an invoice from Odoo (v13) via xml rpc in Python?

Viewed 766

I struggle currently a bit to download an invoice as PDF from Odoo 13 with xml rpc.

The closest that I could get is this:

model_name = 'ir.actions.report'
model_method = 'render_qweb_pdf'
report_id = 282
invoice_id = 4
args = [[report_id]]
kwargs = {'res_ids': [invoice_id]}

models = ServerProxy('{}/xmlrpc/2/object'.format(url))
return models.execute_kw(db, uid, password,
                         model_name, method_name,
                         args, kwargs)

Yet I always end up with this error:

  ...py", line 46, in execute_kw
    args, kwargs)
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1112, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1452, in __request
    verbose=self.__verbose
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1154, in request
    return self.single_request(host, handler, request_body, verbose)
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1170, in single_request
    return self.parse_response(resp)
  File "/usr/lib/python3.6/xmlrpc/client.py", line 1336, in parse_response
    p.feed(data)
  File "/usr/lib/python3.6/xmlrpc/client.py", line 439, in feed
    self._parser.Parse(data, 0)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 64, column 9

The data that its trying to parse there in this self._parser.Parse(data, 0)-line is

b"<?xml version='1.0'?>\n<methodResponse>\n<params>\n<param>\n<value><array><data>\n<value><string>%PDF-1.3\n1 0 obj\n&lt;&lt;\n/Type /Pages\n/Count 0\n/Kids [ ]\n&gt;&gt;\nendobj\n2 0 obj\n&lt;&lt;\n/Producer (PyPDF2)\n&gt;&gt;\nendobj\n3 0 obj\n&lt;&lt;\n/Type /Catalog\n/Pages 4 0 R\n/Outlines 23 0 R\n/PageMode /UseOutlines\n/Dests 25 0 R\n/Names &lt;&lt;\n/EmbeddedFiles &lt;&lt;\n/Names [ (factur\\055x\\056xml) &lt;&lt;\n/Type /Filespec\n/F (factur\\055x\\056xml)\n/EF &lt;&lt;\n/F 27 0 R\n&gt;&gt;\n&gt;&gt; ]\n&gt;&gt;\n&gt;&gt;\n&gt;&gt;\nendobj\n4 0 obj\n&lt;&lt;\n/Type /Pages\n/Kids [ 5 0 R ]\n/Count 1\n/ProcSet [ /PDF /Text /ImageB /ImageC ]\n&gt;&gt;\nendobj\n5 0 obj\n&lt;&lt;\n/Type /Page\n/Parent 4 0 R\n/Contents 6 0 R\n/Resources 7 0 R\n/Annots 22 0 R\n/MediaBox [ 0 0 595 842 ]\n&gt;&gt;\nendobj\n6 0 obj\n&lt;&lt;\n/Filter /FlateDecode\n/Length 2705\n&gt;&gt;\nstream\nx\xc2\x9c\xc3\xad]K\xc2\x8f\xc3\xa4\xc2\xb8\r\xc2\xbe\xc3\x97\xc2\xaf\xc3\xb09@\xc2\xbb\xc2\xad\xc2\xb7\x0c\x04\x0bL\xc2\xbf\xc2\x82\xc3\xa4\x10`0\r\xc3\xac!\xc3\x88!\xc2\x98\xc3\x9dM\xc2\xb0\xc2\x98\xc3\x9ed\xc2\xb2\xc2\x87\xc3\xbc\xc3\xbdH\xc2\xb2\xc3\xbc\xc2\x92\xc3\xab\xc2\x93m\xc2\xb5\xc3\xad\xc2\xb2\xc2\xabk\x1a\xc2\x98z\xc2\xb0$Q\x14I\xc2\x91\x14)\xc3\x9f\xc3\xbf\xc3\xa9\xc3\x8b?\xc2\xb2\x7f\xc3\xbe\xc2\x9e\xc3\x9d?~\xc3\xb9O\xc3\xb6\xc3\x95\xc2\xbf&gt;~9\x15\xc2\xb9.\xc3\xbc\xc2\xbf\xc3\x8c\xc3\xbe\xc3\x9d\xc3\xb5\xc2\xbfP\xc2\x84\xc3\xa7\xc2\xaa\xc2\xb4\xc3\xbf\xc2\xb2\xc2\xafo\xc2\xa7\xc3\xaf\xc3\x99\xc3\xb7\xc3\x93\xc3\xa7\xc3\x93g\xc3\xb3\xc2\xbf}\xc3\xbd~\xc2\xaa;"

So it actually looks quite good and so promising ... :(

Is there a better approach in Odoo 13 now? I checked and all the info for Odoo 12 and so on seems outdated as models / reports / functions / xrpc calls ... all don't exist anymore...

1 Answers

After discovering jsonrpc I was finally able to download the invoice ... Hope this helps someone in the absence of a solution with xmlrpc. (I am still looking for an xml rpc solution.)

import urllib.request
import json
import random

model_name = 'ir.actions.report'
method_name = 'render_qweb_pdf'
report_id = 282
invoice_id = 4
method = "call"
params = {
    "service": "object",
    "method": "execute",
    "args": [db, uid, password, model_name, method_name, report_id, invoice_id],
}
data = {
    "jsonrpc": "2.0",
    "method": method,
    "params": params,
    "id": random.randint(0, 1000000000),
}
req = urllib.request.Request(url=f"{self.url}/jsonrpc", data=json.dumps(data).encode(), headers={
    "Content-Type": "application/json",
})
reply = json.loads(urllib.request.urlopen(req).read().decode('UTF-8'))
if reply.get("error"):
    raise Exception(reply["error"])
return reply["result"]
Related