Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odgovori
5038 Prikazi

Hello Guys,

I've designed reports for my custom modules using openoffice and RML. But while printing/generating the PDF file of the report, it's opening or saving as the report name.pdf,as listed in settings>actions>reports.

My objective is to add some id or number to the file name of the corresponding report, Ex: "My Report - 123.pdf", "My Report - 234.pdf" etc.

To achieve the above, i've tried 2 approaches as mentioned below : 1. Edit report from front end and add python code to Add attachment box and select the Reload from Attachment checkbox.

  1. Added attributes like attachment, use_attachment, multi etc. under report tag in the module_view.xml file.

But no luck. Still the report PDF is opening/saving with the old name. Pls help me with this.

Any help would be appreciated. :)

Cheers

ASP :)

Avatar
Opusti
Best Answer

Hi. I had the same problem. The only solution for me was to modify the OPENERP Kernel with a patch. Then you can control the name from you xml report configuration. Example:

<report
    id="sale_order_cyls_label_report"
    model="sale.order"        
    name="sale_order_cyls_label_report"
    file="OpenJobs/report/sale_order_cyls_label_report.rml"
    string="Cylinders Labels 2x2" 
    auto="True" 
    report_type="pdf"
    attachment="('CYL'+(object.job_id.job or ''))"
        usage="default"
        keyword="client_print_multi"
        menu="True"
        header="False"
    />

Then the file name was made from the attribute 'attachment' ex. 'CYL10024.pdf'. To patch, change the file 'openerp7/openerp-web/addons/web/controllers/main.py'. Search and find at the end of file the statement:

    file_name = '%s.%s' % (file_name, report_struct['format'])

Then insert next code just before:

 #Start code added
 ''' Code added by Raul Paz to set the default file_name like the attach_name
    The attach_name mach with the attachment expresion from your report_xml
    <report
        id="report_webkit.YourModule_report_id"
        model="YourModule.model"        
        name="Your_report_name"
        file="YOUR_MODULE/report/YOUR_MAKO_FILE.mako"
        string="Your Text in the print button" 
        auto="False" 
        report_type="webkit"
        attachment="'Valid_filename_expresion"
        usage="default"
/>
'''
try:
    if action['attachment']:
        model = context['active_model']
        cr = openerp.pooler.get_db(req.session._db).cursor()
        uid = context['uid']
        ids = context['active_ids']
        objects=openerp.pooler.get_pool(req.session._db).get(model).browse(cr,uid,ids,context=context)
        file_name=[eval(action['attachment'],{'object':x, 'time':time}) for x in objects][0]
        ''' Next code force to remake the report file on each request. 
            Remove if your want to send always the first report file.
            If attachment_use is set to false the report files are always generated and stored
        '''
        if action['attachment_use']:
            aids = openerp.pooler.get_pool(req.session._db).get('ir.attachment').search(cr, uid, [('datas_fname','=',file_name+'.pdf'),('res_model','=',model),('res_id','in',ids)])
            atts = openerp.pooler.get_pool(req.session._db).get('ir.attachment')
            res = atts.unlink(cr, uid, aids, context) 
            cr.commit()
            openerp.osv.orm.except_orm('Attach doc deleted',file_name)
except:
    openerp.osv.orm.except_orm('Attach doc not deleted',file_name)
finally:
    cr.close()
#end code added

This work for all report styles...

Avatar
Opusti
Avtor

Hi Raul,

Good to see your response. The issue has already been fixed at my end.

Please check this : http://help.openerp.com/question/42119/customize-file-name-for-rml-reports/

Cheers

ASP :)