diff --git a/report_wkhtmltopdf_paperformat_from_template/README.rst b/report_wkhtmltopdf_paperformat_from_template/README.rst
new file mode 100644
index 000000000..e534719a4
--- /dev/null
+++ b/report_wkhtmltopdf_paperformat_from_template/README.rst
@@ -0,0 +1,111 @@
+============================================
+Report Wkhtmltopdf Paperformat from Template
+============================================
+
+..
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+ !! This file is generated by oca-gen-addon-readme !!
+ !! changes will be overwritten. !!
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+ !! source digest: sha256:f51a84a5c23884b0f3e2f1eea0f66515c07344324f3dab70449ea0b437b66c17
+ !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+.. |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/16.0/report_wkhtmltopdf_paperformat_from_template
+ :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-16-0/reporting-engine-16-0-report_wkhtmltopdf_paperformat_from_template
+ :alt: Translate me on Weblate
+.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
+ :target: https://runboat.odoo-community.org/builds?repo=OCA/reporting-engine&target_branch=16.0
+ :alt: Try me on Runboat
+
+|badge1| |badge2| |badge3| |badge4| |badge5|
+
+This is a technical module that allows to force a paperformat directly from the
+report QWeb template definition.
+
+This is useful in situations where the report content is dynamically chosen, depending
+on some record conditions, and a different paperformat needs to be used for each.
+
+In core, Odoo already allows to overwrite some paperformat values like the ``margin-top``,
+``header-spacing``, etc.. through special ``data-report-*`` attributes in the root ``html``
+tag of the report QWeb template. This module extends this feature to allow to overwrite
+the paperformat itself.
+
+**Table of contents**
+
+.. contents::
+ :local:
+
+Usage
+=====
+
+In a report QWeb template, set the ``data_report_paperformat`` variable to the desired
+paperformat ``xmlid``.
+
+For example:
+
+.. code-block:: xml
+
+
+
+ my_module.paperformat_custom
+
+
+
+Bug Tracker
+===========
+
+Bugs are tracked on `GitHub Issues `_.
+In case of trouble, please check there if your issue has already been reported.
+If you spotted it first, help us to smash it by providing a detailed and welcomed
+`feedback `_.
+
+Do not contact contributors directly about support or help with technical issues.
+
+Credits
+=======
+
+Authors
+~~~~~~~
+
+* Camptocamp
+
+Contributors
+~~~~~~~~~~~~
+
+* `Camptocamp `_
+
+ * Iván Todorovich
+
+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.
+
+.. |maintainer-ivantodorovich| image:: https://github.com/ivantodorovich.png?size=40px
+ :target: https://github.com/ivantodorovich
+ :alt: ivantodorovich
+
+Current `maintainer `__:
+
+|maintainer-ivantodorovich|
+
+This module is part of the `OCA/reporting-engine `_ project on GitHub.
+
+You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
diff --git a/report_wkhtmltopdf_paperformat_from_template/__init__.py b/report_wkhtmltopdf_paperformat_from_template/__init__.py
new file mode 100644
index 000000000..0650744f6
--- /dev/null
+++ b/report_wkhtmltopdf_paperformat_from_template/__init__.py
@@ -0,0 +1 @@
+from . import models
diff --git a/report_wkhtmltopdf_paperformat_from_template/__manifest__.py b/report_wkhtmltopdf_paperformat_from_template/__manifest__.py
new file mode 100644
index 000000000..bd429435d
--- /dev/null
+++ b/report_wkhtmltopdf_paperformat_from_template/__manifest__.py
@@ -0,0 +1,15 @@
+# Copyright 2024 Camptocamp SA (https://www.camptocamp.com).
+# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
+
+{
+ "name": "Report Wkhtmltopdf Paperformat from Template",
+ "summary": "Force the paperformat directly in the specific paperformat args",
+ "version": "16.0.1.0.0",
+ "author": "Camptocamp, Odoo Community Association (OCA)",
+ "maintainers": ["ivantodorovich"],
+ "website": "https://github.com/OCA/reporting-engine",
+ "license": "AGPL-3",
+ "category": "Technical",
+ "depends": ["web"],
+ "data": ["views/report_layout.xml"],
+}
diff --git a/report_wkhtmltopdf_paperformat_from_template/models/__init__.py b/report_wkhtmltopdf_paperformat_from_template/models/__init__.py
new file mode 100644
index 000000000..a248cf216
--- /dev/null
+++ b/report_wkhtmltopdf_paperformat_from_template/models/__init__.py
@@ -0,0 +1 @@
+from . import ir_actions_report
diff --git a/report_wkhtmltopdf_paperformat_from_template/models/ir_actions_report.py b/report_wkhtmltopdf_paperformat_from_template/models/ir_actions_report.py
new file mode 100644
index 000000000..f583a20cf
--- /dev/null
+++ b/report_wkhtmltopdf_paperformat_from_template/models/ir_actions_report.py
@@ -0,0 +1,44 @@
+# Copyright 2024 Camptocamp SA (https://www.camptocamp.com).
+# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
+
+from odoo import api, models
+
+
+class IrActionsReport(models.Model):
+ _inherit = "ir.actions.report"
+
+ @api.model
+ def get_paperformat(self):
+ # OVERRIDE to allow to define paperformat via context
+ if paperformat_id := self.env.context.get("paperformat_id"):
+ return self.env["report.paperformat"].browse(paperformat_id)
+ return super().get_paperformat()
+
+ @api.model
+ def _run_wkhtmltopdf(
+ self,
+ bodies,
+ report_ref=False,
+ header=None,
+ footer=None,
+ landscape=False,
+ specific_paperformat_args=None,
+ set_viewport_size=False,
+ ):
+ # OVERRIDE to allow to define paperformat via context
+ if specific_paperformat_args is None:
+ specific_paperformat_args = {}
+ if paperformat_xml_id := specific_paperformat_args.get(
+ "data-report-paperformat"
+ ):
+ paperformat_id = self.env.ref(paperformat_xml_id).id
+ self = self.with_context(paperformat_id=paperformat_id)
+ return super(IrActionsReport, self)._run_wkhtmltopdf(
+ bodies,
+ report_ref=report_ref,
+ header=header,
+ footer=footer,
+ landscape=landscape,
+ specific_paperformat_args=specific_paperformat_args,
+ set_viewport_size=set_viewport_size,
+ )
diff --git a/report_wkhtmltopdf_paperformat_from_template/readme/CONTRIBUTORS.rst b/report_wkhtmltopdf_paperformat_from_template/readme/CONTRIBUTORS.rst
new file mode 100644
index 000000000..a1e0a8395
--- /dev/null
+++ b/report_wkhtmltopdf_paperformat_from_template/readme/CONTRIBUTORS.rst
@@ -0,0 +1,3 @@
+* `Camptocamp `_
+
+ * Iván Todorovich
diff --git a/report_wkhtmltopdf_paperformat_from_template/readme/DESCRIPTION.rst b/report_wkhtmltopdf_paperformat_from_template/readme/DESCRIPTION.rst
new file mode 100644
index 000000000..c0c3e70cc
--- /dev/null
+++ b/report_wkhtmltopdf_paperformat_from_template/readme/DESCRIPTION.rst
@@ -0,0 +1,10 @@
+This is a technical module that allows to force a paperformat directly from the
+report QWeb template definition.
+
+This is useful in situations where the report content is dynamically chosen, depending
+on some record conditions, and a different paperformat needs to be used for each.
+
+In core, Odoo already allows to overwrite some paperformat values like the ``margin-top``,
+``header-spacing``, etc.. through special ``data-report-*`` attributes in the root ``html``
+tag of the report QWeb template. This module extends this feature to allow to overwrite
+the paperformat itself.
diff --git a/report_wkhtmltopdf_paperformat_from_template/readme/USAGE.rst b/report_wkhtmltopdf_paperformat_from_template/readme/USAGE.rst
new file mode 100644
index 000000000..cc028def3
--- /dev/null
+++ b/report_wkhtmltopdf_paperformat_from_template/readme/USAGE.rst
@@ -0,0 +1,12 @@
+In a report QWeb template, set the ``data_report_paperformat`` variable to the desired
+paperformat ``xmlid``.
+
+For example:
+
+.. code-block:: xml
+
+
+
+ my_module.paperformat_custom
+
+
diff --git a/report_wkhtmltopdf_paperformat_from_template/views/report_layout.xml b/report_wkhtmltopdf_paperformat_from_template/views/report_layout.xml
new file mode 100644
index 000000000..58afdf7b0
--- /dev/null
+++ b/report_wkhtmltopdf_paperformat_from_template/views/report_layout.xml
@@ -0,0 +1,24 @@
+
+
+
+
+
+
+ data_report_paperformat
+
+
+
+
+
+ data_report_paperformat
+
+
+
+
diff --git a/setup/report_wkhtmltopdf_paperformat_from_template/odoo/addons/report_wkhtmltopdf_paperformat_from_template b/setup/report_wkhtmltopdf_paperformat_from_template/odoo/addons/report_wkhtmltopdf_paperformat_from_template
new file mode 120000
index 000000000..317c296f1
--- /dev/null
+++ b/setup/report_wkhtmltopdf_paperformat_from_template/odoo/addons/report_wkhtmltopdf_paperformat_from_template
@@ -0,0 +1 @@
+../../../../report_wkhtmltopdf_paperformat_from_template
\ No newline at end of file
diff --git a/setup/report_wkhtmltopdf_paperformat_from_template/setup.py b/setup/report_wkhtmltopdf_paperformat_from_template/setup.py
new file mode 100644
index 000000000..28c57bb64
--- /dev/null
+++ b/setup/report_wkhtmltopdf_paperformat_from_template/setup.py
@@ -0,0 +1,6 @@
+import setuptools
+
+setuptools.setup(
+ setup_requires=['setuptools-odoo'],
+ odoo_addon=True,
+)