Skip to Content
Menú
This question has been flagged
1 Respondre
4763 Vistes

Hello,


I am trying to select multiple pickings and download into a single pdf they're attachments

 ( shipping labels )


Bellow is my code which will download only one attachment.


Working on Odoo 13 CE.


def generate_awb_pdf(self):
sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', []))
pickings = self.env['stock.picking'].search([('origin', 'in', [o.name for o in sale_orders])])
attachment_ids = self.env['ir.attachment'].search(
[('res_model', '=', 'stock.picking'), ('res_id', 'in', [p.id for p in pickings])])
result = b''
for rec in attachment_ids:
result += rec.datas
base_url = self.env['ir.config_parameter'].get_param('web.base.url')
attachment_obj = self.env['ir.attachment']
attachment_id = attachment_obj.sudo().create(
{'name': "name", 'store_fname': 'awb.pdf', 'datas': result})
download_url = '/web/content/' + str(attachment_id.id) + '?download=true'
return {
'name': 'Report',
'type': 'ir.actions.act_url',
'url': str(base_url) + str(download_url),
'target': 'self',
}



Thank you !


Avatar
Descartar
Autor

Hello Jainesh, 

I've changed the code as you suggested (makes sense..) , however, nothing is getting downloaded.


def generate_awb_pdf(self):

sale_orders = self.env['sale.order'].browse(self._context.get('active_ids', []))
pickings = self.env['stock.picking'].search([('origin', 'in', [o.name for o in sale_orders])])
attachment_ids = self.env['ir.attachment'].search(
[('res_model', '=', 'stock.picking'), ('res_id', 'in', [p.id for p in pickings])])
result = b''

for rec in attachment_ids:
result += rec.datas
attachment_obj = self.env['ir.attachment']
attachment_id = attachment_obj.sudo().create(
{'name': "name", 'store_fname': 'awb.pdf', 'datas': rec.datas})
download_url = '/web/content/' + str(attachment_id.id) + '?download=true'
self.get_report(download_url)

def get_report(self, download_url):
base_url = self.env['ir.config_parameter'].get_param('web.base.url')
return {
'name': 'Report',
'type': 'ir.actions.act_url',
'url': str(base_url) + str(download_url),
'target': 'new',
}

Best Answer

Hello Alexandru,

The problem in your solution is that the data is being returned inside a loop, so once the data is returned the execution of loop will stop.

Please keep the return part in a separate method and call that method inside the loop so even after something is returned the main loop continues.

eg:

for rec in attachment_ids:
    #code to generate url
    self.get_report(url_of_report)

def get_report(self, url_of_report)
    return {
        'name': 'Report',
        'type': 'ir.actions.act_url',
        'url': url_of_report,
        'target': 'self',
}

Hope this solves your issue.

Thanks & Regards,
Email: odoo@aktivsoftware.com
Skype: kalpeshmaheshwari

Avatar
Descartar

Hello @Alexandru

Based on your comment:

For the point:
I am trying to select multiple pickings and download into a single pdf they're attachments ( shipping labels )

- I understand you want all attachments as a single pdf file, Here you have made a few mistakes in code as:
1. In attachments you might have different file types like image, pdf, zip so creating a new attachment of pdf type directly by combining binary data is not a valid solution.

2. Also if you have only pdf attachment then you might also have user uploaded pdf or any other pdf documents related to picking.

I have made a server action which will download all pdf attachments in to a single attachment.

Please check below link for code.
-------------------------------------
https://drive.google.com/drive/folders/1gRGCd9Aet2oTxWMH8Wpsz1wFLLLMUVJi?usp=sharing
-------------------------------------

Note: Please install PyPDF2 lib if you do not have it in your system.

Please make required changes to the above code, I have tested this and it gives me a common pdf for all pdf type attachments in selected pickings.

Although the page numbers and details belong to individual pdfs you might need to find a work around for it if necessary.

Related Posts Respostes Vistes Activitat
0
de febr. 25
696
2
de gen. 24
14345
0
de maig 24
2056
0
de des. 15
3509
0
de març 15
4629