[MIG] report_qweb_pdf_watermark: Migration to 15.0
[MIG] report_qweb_pdf_watermark: Migration to 15.0pull/734/head
parent
07a55136df
commit
fad7b49b2e
|
@ -2,18 +2,24 @@
|
|||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
{
|
||||
"name": "Pdf watermark",
|
||||
"version": "14.0.1.0.1",
|
||||
"version": "15.0.1.0.0",
|
||||
"author": "Therp BV, " "Odoo Community Association (OCA)",
|
||||
"license": "AGPL-3",
|
||||
"category": "Technical Settings",
|
||||
"development_status": "Production/Stable",
|
||||
"summary": "Add watermarks to your QWEB PDF reports",
|
||||
"website": "https://github.com/OCA/reporting-engine",
|
||||
"depends": ["web"],
|
||||
"data": [
|
||||
"demo/report.xml",
|
||||
"views/ir_actions_report_xml.xml",
|
||||
"views/layout_templates.xml",
|
||||
"views/res_company.xml",
|
||||
],
|
||||
"assets": {
|
||||
"web.report_assets_pdf": [
|
||||
"/report_qweb_pdf_watermark/static/src/css/report_qweb_pdf_watermark.css"
|
||||
],
|
||||
},
|
||||
"demo": ["demo/report.xml"],
|
||||
"installable": True,
|
||||
}
|
||||
|
|
|
@ -1,22 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<odoo>
|
||||
<data>
|
||||
<record id="demo_reports" model="ir.actions.report">
|
||||
<field name="name">Demo report</field>
|
||||
<record id="demo_report" model="ir.actions.report">
|
||||
<field name="name">Watermark Demo report</field>
|
||||
<field name="model">res.users</field>
|
||||
<field name="report_type">qweb-pdf</field>
|
||||
<field name="report_name">report_qweb_pdf_watermark.demo_report_view</field>
|
||||
<field name="report_file">report_qweb_pdf_watermark.demo_report_view</field>
|
||||
<field name="paperformat_id" ref="base.paperformat_euro" />
|
||||
<field name="pdf_watermark_expression">docs[:1].company_id.logo</field>
|
||||
<field name="binding_model_id" ref="base.model_res_users" />
|
||||
<field name="binding_type">report</field>
|
||||
</record>
|
||||
<record id="demo_report" model="ir.actions.report">
|
||||
<field name="name">Demo report</field>
|
||||
<field name="model">res.users</field>
|
||||
<field name="report_name">report_qweb_pdf_watermark.demo_report_view</field>
|
||||
<field name="pdf_watermark_expression">docs[:1].company_id.logo</field>
|
||||
</record>
|
||||
<template id="demo_report_view">
|
||||
<t t-call="web.html_container">
|
||||
<t t-call="web.external_layout">
|
||||
|
@ -30,5 +24,4 @@
|
|||
</t>
|
||||
</t>
|
||||
</template>
|
||||
</data>
|
||||
</odoo>
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
# © 2016 Therp BV <http://therp.nl>
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
from . import res_company
|
||||
from . import report
|
||||
|
|
|
@ -9,13 +9,13 @@ from PIL import Image
|
|||
from odoo import api, fields, models
|
||||
from odoo.tools.safe_eval import safe_eval
|
||||
|
||||
logger = getLogger(__name__)
|
||||
|
||||
try:
|
||||
# we need this to be sure PIL has loaded PDF support
|
||||
from PIL import PdfImagePlugin # noqa: F401
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
logger = getLogger(__name__)
|
||||
logger.error("ImportError: The PdfImagePlugin could not be imported")
|
||||
|
||||
try:
|
||||
from PyPDF2 import PdfFileReader, PdfFileWriter # pylint: disable=W0404
|
||||
|
@ -27,7 +27,13 @@ except ImportError:
|
|||
class Report(models.Model):
|
||||
_inherit = "ir.actions.report"
|
||||
|
||||
pdf_watermark = fields.Binary("Watermark")
|
||||
use_company_watermark = fields.Boolean(
|
||||
default=False,
|
||||
help="Use the pdf watermark defined globally in the company settings.",
|
||||
)
|
||||
pdf_watermark = fields.Binary(
|
||||
"Watermark", help="Upload an pdf file to use as an watermark on this report."
|
||||
)
|
||||
pdf_watermark_expression = fields.Char(
|
||||
"Watermark expression",
|
||||
help="An expression yielding the base64 "
|
||||
|
@ -42,6 +48,17 @@ class Report(models.Model):
|
|||
)
|
||||
return super(Report, self)._render_qweb_pdf(res_ids=res_ids, data=data)
|
||||
|
||||
def pdf_has_usable_pages(self, numpages):
|
||||
if numpages < 1:
|
||||
logger.error("Your watermark pdf does not contain any pages")
|
||||
return False
|
||||
if numpages > 1:
|
||||
logger.debug(
|
||||
"Your watermark pdf contains more than one page, "
|
||||
"all but the first one will be ignored"
|
||||
)
|
||||
return True
|
||||
|
||||
@api.model
|
||||
def _run_wkhtmltopdf(
|
||||
self,
|
||||
|
@ -65,6 +82,8 @@ class Report(models.Model):
|
|||
watermark = None
|
||||
if self.pdf_watermark:
|
||||
watermark = b64decode(self.pdf_watermark)
|
||||
elif self.use_company_watermark and self.env.company.pdf_watermark:
|
||||
watermark = b64decode(self.env.company.pdf_watermark)
|
||||
elif docids:
|
||||
watermark = safe_eval(
|
||||
self.pdf_watermark_expression or "None",
|
||||
|
@ -100,14 +119,8 @@ class Report(models.Model):
|
|||
logger.error("No usable watermark found, got %s...", watermark[:100])
|
||||
return result
|
||||
|
||||
if pdf_watermark.numPages < 1:
|
||||
logger.error("Your watermark pdf does not contain any pages")
|
||||
if not self.pdf_has_usable_pages(pdf_watermark.numPages):
|
||||
return result
|
||||
if pdf_watermark.numPages > 1:
|
||||
logger.debug(
|
||||
"Your watermark pdf contains more than one page, "
|
||||
"all but the first one will be ignored"
|
||||
)
|
||||
|
||||
for page in PdfFileReader(BytesIO(result)).pages:
|
||||
watermark_page = pdf.addBlankPage(
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
# Copyright 2022 360 ERP (<https://www.360erp.nl>)
|
||||
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
|
||||
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class ResCompany(models.Model):
|
||||
_inherit = "res.company"
|
||||
|
||||
pdf_watermark = fields.Binary("Watermark")
|
|
@ -3,3 +3,4 @@
|
|||
* Rod Schouteden <rod.schouteden@dynapps.be>
|
||||
* Robin Goots <robin.goots@dynapps.be>
|
||||
* Foram Shah <foram.shah@initos.com>
|
||||
* Emiel van Bokhoven <emiel@360erp.nl>
|
||||
|
|
|
@ -14,3 +14,10 @@
|
|||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
* [MIG] Migration to V14.
|
||||
|
||||
|
||||
15.0.1.0.0 (2022-01-11)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
* [MIG] Migration to V15.
|
||||
* Define pdf watermark in company settings
|
||||
|
|
|
@ -3,3 +3,18 @@ To use this module, you need to:
|
|||
#. go to your report
|
||||
#. select a PDF or image to use as watermark. Note that resolutions and size must match, otherwise you'll have funny results
|
||||
#. You can also fill in an expression that returns the data (base64 encoded) to be used as watermark
|
||||
|
||||
To use the Company watermark, you need to:
|
||||
|
||||
#. go to settings --> company --> update info
|
||||
#. upload an pdf watermark
|
||||
#. go to settings --> technical --> reporting --> reports
|
||||
#. Select the report where you want to use it.
|
||||
#. On the 'Advanced Properties' tab of the notebook check 'use company watermark'
|
||||
|
||||
### Demo
|
||||
And demo report is available (if you have demo data installed) on the users form view.
|
||||
|
||||
#. go to Configuration --> users
|
||||
#. Select an users
|
||||
#. Click the print button --> Watermark Demo report.
|
||||
|
|
|
@ -23,11 +23,33 @@ class TestReportQwebPdfWatermark(HttpCase):
|
|||
# and now we should have three again
|
||||
self._test_report_images(3)
|
||||
|
||||
def _test_report_images(self, number):
|
||||
report = self.env["ir.model.data"].xmlid_to_object(
|
||||
"report_qweb_pdf_watermark.demo_report"
|
||||
# test use company watermark
|
||||
self.env.ref("report_qweb_pdf_watermark.demo_report").write(
|
||||
{"pdf_watermark": False}
|
||||
)
|
||||
self.env.ref("report_qweb_pdf_watermark.demo_report").write(
|
||||
{"use_company_watermark": True}
|
||||
)
|
||||
self.env.ref("base.main_company").write(
|
||||
{"pdf_watermark": self.env.user.company_id.logo}
|
||||
)
|
||||
self._test_report_images(3)
|
||||
|
||||
def _test_report_images(self, number):
|
||||
report = self.env.ref("report_qweb_pdf_watermark.demo_report")
|
||||
pdf, _ = report.with_context(force_report_rendering=True)._render_qweb_pdf(
|
||||
self.env["res.users"].search([]).ids
|
||||
)
|
||||
self.assertEqual(pdf.count(b"/Subtype /Image"), number)
|
||||
|
||||
def test_pdf_has_usable_pages(self):
|
||||
# test 0
|
||||
numpages = 0
|
||||
# pdf_has_usable_pages(self, pdf_watermark)
|
||||
self.assertFalse(self.env["ir.actions.report"].pdf_has_usable_pages(numpages))
|
||||
# test 1
|
||||
numpages = 1
|
||||
self.assertTrue(self.env["ir.actions.report"].pdf_has_usable_pages(numpages))
|
||||
# test 2
|
||||
numpages = 2
|
||||
self.assertTrue(self.env["ir.actions.report"].pdf_has_usable_pages(numpages))
|
||||
|
|
|
@ -6,14 +6,19 @@
|
|||
<field name="arch" type="xml">
|
||||
<field name="attachment" position="after">
|
||||
<field
|
||||
name="pdf_watermark"
|
||||
name="use_company_watermark"
|
||||
attrs="{'invisible': [('report_type', '!=', 'qweb-pdf')]}"
|
||||
/>
|
||||
<field
|
||||
name="pdf_watermark_expression"
|
||||
attrs="{'invisible': [('report_type', '!=', 'qweb-pdf')]}"
|
||||
attrs="{'invisible': ['|',('report_type', '!=', 'qweb-pdf'),('use_company_watermark', '=', True)]}"
|
||||
groups="base.group_no_one"
|
||||
/>
|
||||
<field
|
||||
name="pdf_watermark"
|
||||
attrs="{'invisible': ['|',('report_type', '!=', 'qweb-pdf'),('use_company_watermark', '=', True)]}"
|
||||
widget="pdf_viewer"
|
||||
/>
|
||||
</field>
|
||||
</field>
|
||||
</record>
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<odoo>
|
||||
<!-- Include fix for https://github.com/odoo/odoo/issues/16610 -->
|
||||
<template id="assets_pdf" inherit_id="web.report_assets_pdf">
|
||||
<link position="after">
|
||||
<link
|
||||
href="/report_qweb_pdf_watermark/static/src/css/report_qweb_pdf_watermark.css"
|
||||
rel="stylesheet"
|
||||
/>
|
||||
</link>
|
||||
</template>
|
||||
</odoo>
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<odoo>
|
||||
|
||||
<record id="view_company_form" model="ir.ui.view">
|
||||
<field name="model">res.company</field>
|
||||
<field name="inherit_id" ref="base.view_company_form" />
|
||||
<field name="arch" type="xml">
|
||||
|
||||
<xpath expr="//field[@name='currency_id']" position="after">
|
||||
<field
|
||||
name="pdf_watermark"
|
||||
widget="pdf_viewer"
|
||||
help="Upload an pdf file to use as an Company watermark. You need to activate the setting 'use company watermark' on the reports where you want to use it."
|
||||
/>
|
||||
</xpath>
|
||||
|
||||
</field>
|
||||
</record>
|
||||
|
||||
</odoo>
|
Loading…
Reference in New Issue