3

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...

chickahoona
  • 1,914
  • 14
  • 23
  • You should add your edit as an answer. – Kenly Oct 27 '20 at 11:25
  • I thought so too, yet as its not an answer to the actual question (the "solution" is jsonrpc and not xmlrpc) I had ambivalent thoughts :( If you say that you would do it, Ill change it. You have far more reputation in comparison to me so I follow your guidance :D – chickahoona Oct 27 '20 at 12:02
  • 1
    It is an alternative solution, you can read more at [Can an alternative solution be an-answer](https://meta.stackoverflow.com/questions/300254/can-an-alternative-solution-be-an-answer) – Kenly Oct 27 '20 at 12:21
  • Thanks for the feedback, has been implemented. – chickahoona Oct 27 '20 at 12:42

3 Answers3

2

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"]
chickahoona
  • 1,914
  • 14
  • 23
1

Thanks for your answer!

I have found the following solution which works with XML RPC:

Code in Odoo 16:

class InvoiceXMLRPC(models.Model):
    _inherit = "ir.actions.report"

    def render_qweb_pdf_inherit(self, report_ref, res_ids=None, data=None):
        pdf_content = self._render_qweb_pdf(report_ref, res_ids=res_ids, data=data)
        pdf_content_encoded = base64.b64encode(pdf_content[0]) # needs to be encoded to be able to access with xmlrpc
        return pdf_content_encoded

Code on the Raspberry Pi:

result = models.execute_kw(db, uid, password,
    'ir.actions.report', 'render_qweb_pdf_inherit',[REPORT_ID, REPORT_ID],{'res_ids': invoice_ids})

invoice_content = base64.b64decode(result)


with open('/home/pi/InvoicePrintO16/invoices.pdf', 'wb') as report_file:
        report_file.write(invoice_content)
    
os.system('lp invoices.pdf')
Nate
  • 11
  • 2
0

Thanks for your response. I am also trying to get a PDF report from Odoo 16 with the external API. After failing with XMLRPC and OdooRPC I tried your approach. I was able to get PDF content, but when I try to open the PDF it's only a white page. The file has 444 kb size, and it contains data like the following:

Type /XObject
/Subtype /Image
/Width 2962
/Height 886
/BitsPerComponent 8
/ColorSpace /DeviceRGB
/Length 7 0 R
/Filter /DCTDecode
>>
stream
ÿØÿàJFIFÿÛCÿÛCÿv
                ’ÿÄ

        ÿÄg
          
!"1?AQX—#Ö×?2BVa‘–˜ð3Wq¡±ÔÕ$4RT•ÁÑ%6CYb”¨Øáñ&'DU57GrFHS(9Ec‚†’¢¥ÿÄ?ÿÄ,
1QÑ!2Aq‘¡±ðaÁáñ"RÿÚ
                  ?ŸÀÿêþ·ýh^\È#¹.|¨Ð¢²žoI–ûq£´ún<òÛiûÍj"/¿ÐPÖµ§«lÖWxÃ4o~éV²…J6øÿ™N®°iáëË‘—·qËc?ë¬'ëïBt¥­ºžµ4•Nq¾<Ñ?QØr?ÑÈöO4?y6iä~‰å·#ô-‚ØÆ:Àãr:–ô䆮µÿ¢hÊ%)¾2uW‚™WqÂA“·âOšŒŸá'í ¶1ް=ˆýHºwLÛÊkÛE²y#ºŸ/ª\÷&ωw
                                                                                                                                                                                      µ}(Í?~"ø~$|[¨‚ØÆ:ÀæTmnè¾ãáìö®ôÃ]¿¡³î)ªþF#vHäG·Í;ì_p[?ÇXÕld¬s{™Ù7q(¹$틪…_#OÞ_EO–[>ûïè:9©ü¾ïö—õ°·Üöûyã¿ü?óÛó3þŒ¿§×þÿM¶ùŸ§Þ@?¿ý%þ¯ôüÿœ¾ð@|ºÕb›nÑê×jctú5
:qí9Ö$èÙøÓ¢~Nê+öB?tãÚr¬Iѳñ§DüÔWì„4éÇ´äX“£gãN‰ù;¨¯iÓiÈ>±'FÏÆòwQ_²Ó§Ó}bNŸ:'äd!§N=§ úÄ?tOÉÝE~ÈCNœ{NCaÚQÖ>›µÁŽ*ywKy-Œ«Ž¨÷…JÁ¨\±­‹ÖÔe‹¶H ×jt”À¾­Ë^­'ÊÒîj•O‹Oz˜µM8¬Ír\Y¬GÔMºàd؇q\ÖåŸFqÝ×Ö·©ŒœŠ•vã«@¡Ñ©ÌÍùÕJœˆ°¢2_k’i~›ŸÌ-ˆ¾l?˜Ï]zúOéíÙ´û—W¶-÷_‡Í    a?òîDn¿)uÛ&”)
BÑWºi­¶âTÓŽ%Ý3§N=§!§Óã

Do you know what might be the problem? FYI: The method render_qweb_pdf was changed to a private method _render_qweb_pdf in Odoo 16, I needed to change that to be able to call it over the external API.

The code:

import json
import random
import urllib.request
import config
import sys
import os

HOST = config.url
DB = config.db
USER = config.username
PASS = config.password

def json_rpc(url, method, params):
    data = {"jsonrpc": "2.0", 
           "method": method,
           "params": params,
           "id": random.randint(0, 1000000000),
            }
    req = urllib.request.Request(url=url, 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"]

def call(url, service, method, *args):
    return json_rpc(url, "call", {"service": service, "method": method, "args": args})

url = "http://%s/jsonrpc" % (HOST)
uid = call(url, "common", "login", DB, USER, PASS)

model_name = 'ir.actions.report'
method_name = 'render_qweb_pdf'
report_id = 784
invoice_id = [1827]
method = "call"
params = {
    "service": "object",
    "method": "execute",
    "args": [DB, uid, PASS, model_name, method_name, report_id, report_id, invoice_id],
}
data = {
    "jsonrpc": "2.0",
    "method": method,
    "params": params,
    "id": random.randint(0, 1000000000),
}
req = urllib.request.Request(url="URL", data=json.dumps(data).encode(), headers={
    "Content-Type": "application/json",
})

reply = json.loads(urllib.request.urlopen(req).read().decode('UTF-8'))

#print(reply)

if reply.get("error"):
    raise Exception(reply["error"])

#print(reply["result"])

report_data = (reply['result'])[0]

#print(report_data)

#print(sys.getsizeof(reply))

with open('/home/pi/InvoicePrintO16/invoices.pdf', 'w') as report_file:
        report_file.write(report_data)
    
os.system('lp invoices.pdf')
Makhele Sabata
  • 582
  • 6
  • 16
Nate
  • 11
  • 2
  • Hm hard to say. Most probably an encoding issue. I recall having quite some headaches there in Odoo 15 again. I don't know if that's some internal tool or so and if you have full access to the Odoo installation, but I created an own Odoo plugin that adds a custom render function to ir_action which base64 encodes things so that I could decode them later on the other end. – chickahoona Jun 23 '23 at 09:38