How to use 'saveas' function to send a pdf file to client side? Openerp | Odoo
<p>I wrote a module to consume a .wsdl webservice, using python suds library, this service returns a PDF file in base64, I'm able to save this file in a binary fild, so I want a button to download this file on client side, How can I do that? I was reading that I can use 'saveas' method available on '/web/controllers/main.py' but It's a contoller method, How can I call it by a button action? I couldn't figure out! I would appreciate any help you can give me! Thanks!</p>

<p>If you are adding a button in the backend that you wish to download your pdf. You won't have access to template structures. You can get around this using a technique like this example.</p>

<p>You need a link, which you can wrap around a button (which does nothing) and you can use javascript if you need to manipulate the href (to add a query_string or to pass any data to your controller. Because you define your link as <code>target=_blank</code> and provide a download name whatever you get as a response from your controller function your browser will try to download. </p>

<pre><code>&lt;a id="pdf_download_link" href="/controller/url/path" target="_blank" download='my_pdf_file.pdf'&gt;
    &lt;button class="oe_stat_button" 
            type="action" 
            icon="fa-print" 
            name="print_pdf_function" 
            string="Download PDF" 
            help="Download PDF File"/&gt;
&lt;/a&gt;

&lt;script&gt;
    $(document).ready(function(){
        var controller_url = ...
        $("#pdf_download_link").attr("href", controller_url);    
    });
&lt;/script&gt;
</code></pre>

<p>Your model does not need an action function <code>print_pdf</code>_function. In Odoo8 (not sure about Odoo9) the button will click but it just will not find the action function however the url will trigger the response from your controller.</p>

<p>Your controller.py file</p>

<pre><code>@http.route('/controller/url/path', auth='user', website=True)
def download_pdf_method(self, **kwargs):
    your_method = ...
    return file
</code></pre>

<p>In your controller you will need to access your binary file from your model and return it in the response to the link's request.</p>

<p>I use this in a module of my own. I use javascript to manipulate my link href so that I am passing the appropriate record it to my controllers. However you may be able to interrogate the request object and determine the record id or any other relevant data that you need. Also wrapping the button in the link is not really necessary however the buttons look much nicer than a plain text link.</p>

<p>I know this is not a conventional approach. Your other option is to just save it as a binary file and make the file available on your form. When a binary field has data the normal function of Odoo's backend form is to provide a download link.</p>

python pdf openerp odoo-8 openerp-7