问题描述
我目前有点难以从odoo 13下载带有XML rpc的PDF发票。
我能得到的最接近的是:
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)
但是我总是会遇到此错误:
...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
它在此self._parser.Parse(data,0)
行中尝试解析的数据是
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<<\n/Type /Pages\n/Count 0\n/Kids [ ]\n>>\nendobj\n2 0 obj\n<<\n/Producer (PyPDF2)\n>>\nendobj\n3 0 obj\n<<\n/Type /Catalog\n/Pages 4 0 R\n/Outlines 23 0 R\n/PageMode /USEOutlines\n/Dests 25 0 R\n/Names <<\n/EmbeddedFiles <<\n/Names [ (factur\\055x\\056xml) <<\n/Type /Filespec\n/F (factur\\055x\\056xml)\n/EF <<\n/F 27 0 R\n>>\n>> ]\n>>\n>>\n>>\nendobj\n4 0 obj\n<<\n/Type /Pages\n/Kids [ 5 0 R ]\n/Count 1\n/ProcSet [ /PDF /Text /ImageB /ImageC ]\n>>\nendobj\n5 0 obj\n<<\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>>\nendobj\n6 0 obj\n<<\n/Filter /FlateDecode\n/Length 2705\n>>\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>~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;"
所以它实际上看起来很好,而且很有希望...:(
odoo 13现在是否有更好的方法?我检查了一下,关于odoo 12的所有信息似乎过时了,因为模型/报告/函数/ xrpc调用...都不再存在...
解决方法
发现jsonrpc之后,我终于可以下载发票了……希望这对没有xmlrpc解决方案的人有所帮助。 (我仍在寻找xml rpc解决方案。)
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"]