javascriptpythonwidgetodoopdf-viewer

Remove download button from odoo's pdf_viewer widget


I'm trying to disable the download button located into the attachment preview widget of Odoo (Pdf_viewer) as you can see into the code below:

 <field name="preview" attrs="{'readonly': [('preview', '=', True)]}" widget="pdf_viewer" />

PS: I tried to set the field to readonly but still give for the reader the entire file.

enter image description here

I had a little help from a friend that suggested of changing the JS file containing the template of this button, but I don't know the steps to do that! Thank you for your helps.


Solution

  • You can override the _disableButtons function and hide the download button.

    Example:

    var basic_fields = require('web.basic_fields');
    
    basic_fields.FieldPdfViewer.include({
        _disableButtons: function (iframe) {
            $(iframe).contents().find('button#download').hide();
            // $(iframe).contents().find('button#secondaryDownload').hide();
            this._super(iframe);
        },
    });
    

    If you need to control the download button visibility using the context attribute, try the following code:

    var basic_fields = require('web.basic_fields');
    var Context = require('web.Context');
    
    basic_fields.FieldPdfViewer.include({
        _disableButtons: function (iframe) {
            var self = this;
            if (self.attrs.context) {
                var context = new Context(self.attrs.context).eval();
                if(!context.download) {
                    $(iframe).contents().find('button#download').hide();
                    // $(iframe).contents().find('button#secondaryDownload').hide();
                }
            }
            this._super(iframe);
        },
    });
    

    Edit:

    Create an XML file with the following content and add it to the data entry in the manifest file:

    <?xml version="1.0" encoding="utf-8"?>
    <odoo>
        <data>
            <template id="assets_backend" inherit_id="web.assets_backend" name="assets_backend">
                <xpath expr="." position="inside">
                    <script type="text/javascript"
                            src="/module_name/static/src/js/pdf_viewser.js"></script>
                </xpath>
            </template>
        </data>
    </odoo>
    

    Create pdf_viewser.js under static/src/js and add the above code:

    odoo.define('module_name.PDFViewer', function (require) {
        "use strict";
        
        var basic_fields = require('web.basic_fields');
    
        basic_fields.FieldPdfViewer.include({
            _disableButtons: function (iframe) {
                $(iframe).contents().find('button#download').hide();
                // $(iframe).contents().find('button#secondaryDownload').hide();
                this._super(iframe);
            },
        });
    
    });
    

    The steps are listed in the Adding files in an asset bundle section. For more details check the Assets Management documentation.