Hello, I am trying to download an invoice in PDF format in a local folder to be able to modify it but I have not found any solution. I use the XML-RPC client to download but I couldn't, I attach the code. Can anyone help me?
Thanks
from flask import Flask, request, jsonify, send_file
import xmlrpc.client
import requests
import base64
url = 'url'
db = 'db'
username = 'username'
password = 'password'
session_url = f'{url}web/session/authenticate'
data = {
'jsonrpc': '2.0',
'method': 'call',
'params': {
"service": "common",
"method": "login",
'db': db,
'login': username,
'password': password,
}
}
session_response = requests.post(session_url, json=data)
session_data = session_response.json()
session_id = session_response.cookies['session_id']
common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url))
uid = common.authenticate(db, username, password, {})
models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url))
invoice_id = 1
invoice = models.execute_kw(db, uid, password, 'account.move', 'read', [invoice_id], {'fields': ['name']})
print("Invoice details:", invoice)
report_action = models.execute_kw(db, uid, password, 'ir.actions.report', 'search_read', [[['model', '=', 'account.move'], ['report_type', '=', 'qweb-pdf']]], {'fields': ['id', 'name']})
print("Available reports:", report_action)
if report_action:
report_id = report_action[0]['id']
report_data = models.execute_kw(db, uid, password, 'account.move', 'action_invoice_print', [invoice_id])
pdf_content = base64.b64decode(report_data[0])
filename = '{}.pdf'.format(invoice[0]['name'])
with open(filename, 'wb') as f:
f.write(pdf_content)
print(f"PDF saved as {filename}")
else:
print("No suitable report found for invoices.")