diff --git a/report_wkhtmltopdf_param/README.rst b/report_wkhtmltopdf_param/README.rst new file mode 100644 index 00000000..567acaed --- /dev/null +++ b/report_wkhtmltopdf_param/README.rst @@ -0,0 +1,70 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +======================== +Report Wkhtmltopdf Param +======================== + +This module allows you to add new parameters for a paper format which are +then forwarded to wkhtmltopdf command as arguments. To display the arguments +that wkhtmltopdf accepts go to your command line and type 'wkhtmltopdf -H'. + +A commonly used parameter in Odoo is *--disable-smart-shrinking*, that will +disable the automatic resizing of the PDF when converting. This is +important when you intend to have a layout that conforms to certain alignment. +It is very common whenever you need to conform the PDF to a predefined +layoyut (e.g. checks, official forms,...). + + +Usage +===== + +#. Go to *Settings* and press 'Activate the developer mode (with assets)' +#. Go to *Settings - Technical - Reports - Paper Format* +#. Add additional parameters indicating the command argument name (remember to + add prefix -- or -) and value. + + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/143/10.0 + + +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 smashing it by providing a detailed and welcomed feedback. + +Credits +======= + +Images +------ + +* Odoo Community Association: `Icon `_. + +Contributors +------------ + +* Miku Laitinen +* Jordi Ballester + + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +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. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/report_wkhtmltopdf_param/__init__.py b/report_wkhtmltopdf_param/__init__.py new file mode 100644 index 00000000..056ac84d --- /dev/null +++ b/report_wkhtmltopdf_param/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Avoin.Systems +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import models diff --git a/report_wkhtmltopdf_param/__manifest__.py b/report_wkhtmltopdf_param/__manifest__.py new file mode 100644 index 00000000..d45d1903 --- /dev/null +++ b/report_wkhtmltopdf_param/__manifest__.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Avoin.Systems +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +# noinspection PyStatementEffect +{ + "name": "Report Wkhtmltopdf Param", + "version": "10.0.1.0.1", + "license": "AGPL-3", + "summary": """ + Add new parameters for a paper format to be used by wkhtmltopdf + command as arguments. + """, + "author": "Avoin.Systems," + "Eficent," + "Odoo Community Association (OCA)", + "website": "https://avoin.systems", + "category": "Technical Settings", + "depends": [ + "report", + ], + "data": [ + "security/ir.model.access.csv", + "views/paperformat.xml", + ], + "installable": True, + "auto_install": False, + "application": False, + "active": False, +} diff --git a/report_wkhtmltopdf_param/models/__init__.py b/report_wkhtmltopdf_param/models/__init__.py new file mode 100644 index 00000000..ebdbde57 --- /dev/null +++ b/report_wkhtmltopdf_param/models/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Avoin.Systems +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import report_paperformat_parameter +from . import report_paperformat +from . import report diff --git a/report_wkhtmltopdf_param/models/report.py b/report_wkhtmltopdf_param/models/report.py new file mode 100644 index 00000000..fc14c983 --- /dev/null +++ b/report_wkhtmltopdf_param/models/report.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Avoin.Systems +# Copyright 2017 Eficent Business and IT Consulting Services, S.L. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models + + +class Report(models.Model): + _inherit = 'report' + + def _build_wkhtmltopdf_args(self, paperformat, + specific_paperformat_args=None): + # noinspection PyUnresolvedReferences,PyProtectedMember + command_args = super(Report, self)._build_wkhtmltopdf_args( + paperformat, + specific_paperformat_args + ) + + for param in paperformat.custom_params: + command_args.extend([param.name]) + if param.value: + command_args.extend([param.value]) + + return command_args diff --git a/report_wkhtmltopdf_param/models/report_paperformat.py b/report_wkhtmltopdf_param/models/report_paperformat.py new file mode 100644 index 00000000..6b2582ce --- /dev/null +++ b/report_wkhtmltopdf_param/models/report_paperformat.py @@ -0,0 +1,42 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Avoin.Systems +# Copyright 2017 Eficent Business and IT Consulting Services, S.L. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import api, fields, models, _ +from odoo.exceptions import ValidationError +import logging + +_logger = logging.getLogger(__name__) + + +class Paper(models.Model): + _inherit = 'report.paperformat' + + custom_params = fields.One2many( + 'report.paperformat.parameter', + 'paperformat_id', + 'Custom Parameters', + help='Custom Parameters passed forward as wkhtmltopdf ' + 'command arguments' + ) + + @api.constrains('custom_params') + def _check_recursion(self): + for paperformat in self: + sample_html = """ + + + +
+ Hello World! +
+ + + """ + contenthtml = [tuple([1, sample_html])] + content = self.env['report']._run_wkhtmltopdf( + [], [], contenthtml, False, paperformat, False, False, False) + if not content: + raise ValidationError(_( + "Failed to create a PDF using the provided parameters.")) diff --git a/report_wkhtmltopdf_param/models/report_paperformat_parameter.py b/report_wkhtmltopdf_param/models/report_paperformat_parameter.py new file mode 100644 index 00000000..c044a6aa --- /dev/null +++ b/report_wkhtmltopdf_param/models/report_paperformat_parameter.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Avoin.Systems +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import models, fields + + +class ReportPaperformatParameter(models.Model): + _name = 'report.paperformat.parameter' + _description = 'wkhtmltopdf parameters' + + paperformat_id = fields.Many2one( + 'report.paperformat', + 'Paper Format', + required=True, + ) + + name = fields.Char( + 'Name', + required=True, + help='The command argument name. Remember to add prefix -- or -' + ) + + value = fields.Char( + 'Value', + ) diff --git a/report_wkhtmltopdf_param/security/ir.model.access.csv b/report_wkhtmltopdf_param/security/ir.model.access.csv new file mode 100644 index 00000000..204dd15f --- /dev/null +++ b/report_wkhtmltopdf_param/security/ir.model.access.csv @@ -0,0 +1,3 @@ +"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink" +"paperformat_parameter_access_portal","ir_actions_report_paperformat_parameter group_portal","model_report_paperformat_parameter","base.group_portal",1,0,0,0 +"paperformat_parameter_access_employee","ir_actions_report_paperformat_parameter group_hr_user","model_report_paperformat_parameter",,1,0,1,0 diff --git a/report_wkhtmltopdf_param/static/description/icon.png b/report_wkhtmltopdf_param/static/description/icon.png new file mode 100644 index 00000000..3a0328b5 Binary files /dev/null and b/report_wkhtmltopdf_param/static/description/icon.png differ diff --git a/report_wkhtmltopdf_param/tests/__init__.py b/report_wkhtmltopdf_param/tests/__init__.py new file mode 100644 index 00000000..17a83ad6 --- /dev/null +++ b/report_wkhtmltopdf_param/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Avoin.Systems +# Copyright 2017 Eficent Business and IT Consulting Services, S.L. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +from . import test_report_paperformat diff --git a/report_wkhtmltopdf_param/tests/test_report_paperformat.py b/report_wkhtmltopdf_param/tests/test_report_paperformat.py new file mode 100644 index 00000000..2307a588 --- /dev/null +++ b/report_wkhtmltopdf_param/tests/test_report_paperformat.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# Copyright 2017 Avoin.Systems +# Copyright 2017 Eficent Business and IT Consulting Services, S.L. +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +import odoo.tests +from odoo.exceptions import ValidationError + + +@odoo.tests.common.at_install(False) +@odoo.tests.common.post_install(True) +class TestWkhtmltopdf(odoo.tests.TransactionCase): + def test_wkhtmltopdf_incorrect_parameter(self): + for report_paperformat in self.env['report.paperformat'].search([]): + with self.assertRaises(ValidationError): + report_paperformat.update({ + 'custom_params': [(0, 0, { + 'name': 'bad-parameter' + })]}) + + def test_wkhtmltopdf_valid_parameter(self): + for report_paperformat in self.env['report.paperformat'].search([]): + error = False + try: + report_paperformat.update({ + 'custom_params': [(0, 0, { + 'name': '--disable-smart-shrinking' + })]}) + except ValidationError: + error = True + self.assertEquals(error, False, + "There was an error adding wkhtmltopdf " + "parameter --disable-smart-shrinking") diff --git a/report_wkhtmltopdf_param/views/paperformat.xml b/report_wkhtmltopdf_param/views/paperformat.xml new file mode 100644 index 00000000..57d6777a --- /dev/null +++ b/report_wkhtmltopdf_param/views/paperformat.xml @@ -0,0 +1,21 @@ + + + + + paperformat with custom parameters + report.paperformat + + + + + + + + + + + + + + +