Search code examples
apiodooxml-rpcinvoiceodoo-12

Odoo v12 API get invoice PDF


This question got me started with my C# Odoo API implementation. I have working code using CookComputing.XmlRpcV2 to retrieve a list of invoices.

What I would like to implement is the option to retrieve/download a PDF of a selected invoice. Does anybody know what I would need to get this done?

When I search, I find forum posts stating reporting doesn't work since V11, such as this one. Neither do I see it mentioned in the online documentation for V12, although there is mention of it at the bottom of the page for V10.

Update

Someone mentioned to construct an URL:

http://localhost:8069/my/invoices/1?report_type=pdf&download=true&access_token=<ACCESSTOKEN>

Where 1 is the invoice ID. Technically this works, but requires me to be logged in to the portal using the browser. Even if I can log into the portal from my C# service, I do not know where/how to retrieve the correct access token. I can see this is in GUID form. Does anybody know whether this is the same token I could retrieve from the OAuth2 REST API (which is a paid module b.t.w.)?


Solution

  • Correct. You can download the PDF by placing in the access_token.

    This is the only way I managed to figure it out for Odoo v.12. after smashing my head against a brick wall repeatedly. My example programming language is with Python 3 though, not C#, but I'm sure you can adapt it.

    odoo_url_host = "https://company.odoo.com"
    

    The access_token can be found in the invoice's JSON response.

    invoice_id = 1234
    models = xmlrpcclient.ServerProxy('{}/xmlrpc/2/object'.format(odoo_url_host))
    invoice = models.execute_kw(db, uid, password, "account.invoice", read, [[invoice_id]])
    

    which, provided you get back a found invoice, you can use the response like so:

    print(invoice["access_token"])
    
    download_url = "%s/%s/my/invoices/%d?report_type=pdf&download=true&access_token=%s" % (odoo_url_host, invoice_id, invoice["access_token"])
    

    Which if you simply want to automatically download, can be done like so:

    import urllib.request
    
    pdf_destination = "./invoices/invoice-%d.pdf" % invoice_id
    
    urllib.request.urlretrieve(download_url, pdf_destination)
    

    You'd need to change the way this is written for Python 2.7.

    Also, make sure that you click 'share' on the invoice (within odoo) as sometimes the access_token doesn't get generated for that invoice and returns false otherwise.

    Or if you'd like to seamlessly have the access_token generated, execute this before trying to get the access token:

    ctx = {'active_model': 'account.invoice', 'active_id': invoice_id}
    print(models.execute_kw(db, uid, password, 'portal.share', 'default_get',[{}],{'context': ctx}))
    

    That should get you the share_link for the entire document, but all you need is the access_token to be generated. You can extract the access_token from the share_link value in the JSON response, if you'd like to do it that way. Anyway :) Happy coding.