Skip to content

Commit

Permalink
[ADD] report_pdf_zip_download
Browse files Browse the repository at this point in the history
  • Loading branch information
AungKoKoLin1997 committed Oct 23, 2024
1 parent e685488 commit 67b5e44
Show file tree
Hide file tree
Showing 16 changed files with 725 additions and 0 deletions.
80 changes: 80 additions & 0 deletions report_pdf_zip_download/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
=======================
Report PDF ZIP Download
=======================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Freporting--engine-lightgray.png?logo=github
:target: https://github.com/OCA/reporting-engine/tree/12.0/report_pdf_zip_download
:alt: OCA/reporting-engine
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/reporting-engine-12-0/reporting-engine-12-0-report_pdf_zip_download
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/143/12.0
:alt: Try me on Runbot

|badge1| |badge2| |badge3| |badge4| |badge5|

This module generates one PDF report per record, and the reports are compiled into a ZIP file if the user prints multiple records at once. If the user prints a single record, it will follow Odoo's standard behavior and will generate just a single PDF file.

**Table of contents**

.. contents::
:local:

Configuration
=============

To enable the ZIP file feature, please check the 'Zip Download' option under the 'Advanced Properties' tab for the specific report.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/reporting-engine/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/reporting-engine/issues/new?body=module:%20report_pdf_zip_download%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* Quartile

Contributors
~~~~~~~~~~~~

* `Quartile <https://www.quartile.co>`__:

* Aung Ko Ko Lin

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/reporting-engine <https://github.com/OCA/reporting-engine/tree/12.0/report_pdf_zip_download>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions report_pdf_zip_download/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import controllers
from . import models
17 changes: 17 additions & 0 deletions report_pdf_zip_download/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# © 2017 Creu Blanca
# Copyright 2024 Quartile (https://www.quartile.co)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
{
"name": "Report PDF ZIP Download",
"category": "Report",
"version": "12.0.1.0.0",
"author": "Quartile, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/reporting-engine",
"license": "AGPL-3",
"depends": ["base"],
"data": [
"views/ir_actions_report_views.xml",
"views/web_client_templates.xml",
],
"installable": True,
}
1 change: 1 addition & 0 deletions report_pdf_zip_download/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import main
63 changes: 63 additions & 0 deletions report_pdf_zip_download/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright 2024 Quartile (https://www.quartile.co)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import zipfile
from odoo.http import content_disposition
from odoo import http
from odoo.http import request
from odoo.addons.web.controllers.main import ReportController
from io import BytesIO
from datetime import datetime
import json
from odoo.tools.safe_eval import safe_eval
import time


class ExtendedReportController(ReportController):

@http.route()
def report_routes(self, reportname, docids=None, converter=None, **data):
report = request.env['ir.actions.report']._get_report_from_name(reportname)
report_name = report.report_file
doc_ids = []
if converter == "zip":
if docids:
doc_ids = [int(i) for i in docids.split(',')]
context = dict(request.env.context)
if data.get('options'):
data.update(json.loads(data.pop('options')))
if data.get('context'):
data['context'] = json.loads(data['context'])
if data['context'].get('lang'):
del data['context']['lang']
context.update(data['context'])
attachments = []
for doc_id in doc_ids:
pdf_content, _ = report.with_context(context).render_qweb_pdf(
[doc_id], data=data
)
if report.print_report_name:
obj = request.env[report.model].browse(doc_id)
report_name = safe_eval(report.print_report_name,
{'object': obj, 'time': time})
report_name = report_name.replace('/', '_')
pdf_name = f'{report_name}.pdf'
attachments.append((pdf_name, pdf_content))
# Generate the ZIP file
zip_filename = f"{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip"
bitIO = BytesIO()
with zipfile.ZipFile(bitIO, "w", zipfile.ZIP_DEFLATED) as zip_file:
for pdf_name, pdf_content in attachments:
zip_file.writestr(pdf_name, pdf_content)
zip_content = bitIO.getvalue()
headers = [
('Content-Type', 'application/zip'),
('Content-Disposition', content_disposition(zip_filename))
]
return request.make_response(
zip_content,
headers=headers
)
return super(ExtendedReportController, self).report_routes(
reportname, docids, converter, **data
)
1 change: 1 addition & 0 deletions report_pdf_zip_download/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import ir_actions_report
13 changes: 13 additions & 0 deletions report_pdf_zip_download/models/ir_actions_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 Quartile (https://www.quartile.co)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from odoo import models, fields


class IrActionsReport(models.Model):
_inherit = "ir.actions.report"

zip_download = fields.Boolean(
help="If enabled, the report will be downloaded as a zip "
"file for multiple records."
)
1 change: 1 addition & 0 deletions report_pdf_zip_download/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
To enable the ZIP file feature, please check the 'Zip Download' option under the 'Advanced Properties' tab for the specific report.
3 changes: 3 additions & 0 deletions report_pdf_zip_download/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `Quartile <https://www.quartile.co>`__:

* Aung Ko Ko Lin
1 change: 1 addition & 0 deletions report_pdf_zip_download/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module generates one PDF report per record, and the reports are compiled into a ZIP file if the user prints multiple records at once. If the user prints a single record, it will follow Odoo's standard behavior and will generate just a single PDF file.
Loading

0 comments on commit 67b5e44

Please sign in to comment.