Skip to content

Commit

Permalink
[ADD] brand_report_qweb_pdf_watermark: base Module to use brand speci…
Browse files Browse the repository at this point in the history
…fic watermark on reports
  • Loading branch information
bosd committed Feb 10, 2025
1 parent 0c0f4e7 commit 97ed7da
Show file tree
Hide file tree
Showing 22 changed files with 902 additions and 0 deletions.
118 changes: 118 additions & 0 deletions brand_report_qweb_pdf_watermark/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
============================
Brand Specific PDF watermark
============================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:a8c72309a1abf484472b7477f932aee5d490d101edd647734cf15936dc6ce24a
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
:target: https://odoo-community.org/page/development-status
:alt: Production/Stable
.. |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%2Fbrand-lightgray.png?logo=github
:target: https://github.com/OCA/brand/tree/18.0/brand_report_qweb_pdf_watermark
:alt: OCA/brand
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/brand-18-0/brand-18-0-brand_report_qweb_pdf_watermark
: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/brand&target_branch=18.0
:alt: Try me on Runboat

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

This module was adds the functionality to use brand watermarks
(backgrounds) to PDF reports. This module will not add any reports on
itself. It rather provides a "Use Brand Watermark" check box on reports.
And allows you to define a brand specific pdf watermark. You will need
to use it in combination with a module which adds branding possibilities
to an model such as ``account_brand``. If the box is checked and pdf
watermark is added to a brand. Then the watermark is added to the pdf.

**Table of contents**

.. contents::
:local:

Installation
============

This module depends on ``brand_external_report_layout`` and
``report_qweb_pdf_watermark`` The dependenices are the same as with
those modules, You need to install PyPDF2:

::

$ pip install pypdf2

Usage
=====

To use this module, you need to:

Define a pdf watermark on brand.

1. go to settings --> User & Companies --> Brands
2. create or edit an brand
3. upload an pdf watermark. Note that resolutions and size must match,
otherwise you'll have funny results

To use the Brand watermark, on for example your invoices with the
``account_brand`` module you need to:

1. go to your report settings --> Technical --> Reports (e.g.
``account.report_invoice``)
2. Under Advanced Properties, Check the box "Use Brand Watermark".
3. Create a report e.g. an invoice, Set the brand and generate the
report. The brand watermark is added there.

If no brand information is available. No watermark is added. It is
possible to stack the pdf watermarks. E.g. use the "Company watermark"
and "Brand Watermark" on top of each other.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/brand/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 <https://github.com/OCA/brand/issues/new?body=module:%20brand_report_qweb_pdf_watermark%0Aversion:%2018.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
-------

* OBS Solutions BV

Contributors
------------

- Emiel van Bokhoven <emiel.vanbokhoven@obs-solutions.com>

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/brand <https://github.com/OCA/brand/tree/18.0/brand_report_qweb_pdf_watermark>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
3 changes: 3 additions & 0 deletions brand_report_qweb_pdf_watermark/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# © 2025 OBS Solutions BV <http://obs-solutions.com>

from . import models
23 changes: 23 additions & 0 deletions brand_report_qweb_pdf_watermark/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# © 2025 OBS Solutions BV <http://obs-solutions.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{
"name": "Brand Specific PDF watermark",
"version": "18.0.1.0.0",
"author": "OBS Solutions BV, " "Odoo Community Association (OCA)",
"license": "AGPL-3",
"category": "Technical Settings",
"development_status": "Production/Stable",
"summary": "Add brand specific watermarks to your QWEB PDF reports",
"website": "https://github.com/OCA/brand",
"depends": ["web", "brand_external_report_layout", "report_qweb_pdf_watermark"],
"data": [
"views/ir_actions_report_xml.xml",
"views/res_brand.xml",
],
"assets": {
"web.report_assets_pdf": [
"/brand_report_qweb_pdf_watermark/static/src/css/report_qweb_pdf_watermark.css"
],
},
"installable": True,
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Translation of Odoo Server.
# This file contains the translation of the following modules:
# * brand_report_qweb_pdf_watermark
#
msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 18.0+e\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-02-10 11:43+0000\n"
"PO-Revision-Date: 2025-02-10 11:43+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: \n"
"Plural-Forms: \n"

#. module: brand_report_qweb_pdf_watermark
#: model:ir.model,name:brand_report_qweb_pdf_watermark.model_res_brand
msgid "Brand"
msgstr ""

#. module: brand_report_qweb_pdf_watermark
#: model:ir.model,name:brand_report_qweb_pdf_watermark.model_ir_actions_report
msgid "Report Action"
msgstr ""

#. module: brand_report_qweb_pdf_watermark
#: model_terms:ir.ui.view,arch_db:brand_report_qweb_pdf_watermark.view_brand_form
msgid ""
"Upload an pdf file to use as an brand specific watermark. You need to "
"activate the setting 'use brand watermark' on the reports where you want to "
"use it."
msgstr ""

#. module: brand_report_qweb_pdf_watermark
#: model:ir.model.fields,field_description:brand_report_qweb_pdf_watermark.field_ir_actions_report__use_brand_watermark
msgid "Use Brand Watermark"
msgstr ""

#. module: brand_report_qweb_pdf_watermark
#: model:ir.model.fields,help:brand_report_qweb_pdf_watermark.field_ir_actions_report__use_brand_watermark
msgid "Use the pdf watermark defined globally in the brand settings."
msgstr ""

#. module: brand_report_qweb_pdf_watermark
#: model:ir.model.fields,field_description:brand_report_qweb_pdf_watermark.field_res_brand__pdf_watermark
msgid "Watermark"
msgstr ""
4 changes: 4 additions & 0 deletions brand_report_qweb_pdf_watermark/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# © 2025 OBS Solutions BV <http://obs-solutions.com>

from . import res_brand
from . import report
64 changes: 64 additions & 0 deletions brand_report_qweb_pdf_watermark/models/report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# © 2016 Therp BV <http://therp.nl>
# Copyright 2023 Onestein - Anjeel Haria
# © 2025 OBS Solutions BV <http://obs-solutions.com>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from base64 import b64decode
from io import BytesIO
from logging import getLogger

from odoo import fields, models

logger = getLogger(__name__)

try:
# we need this to be sure PIL has loaded PDF support
from PIL import PdfImagePlugin # noqa: F401
except ImportError:
logger.error("ImportError: The PdfImagePlugin could not be imported")

try:
from PyPDF2 import PdfFileReader, PdfFileWriter # pylint: disable=W0404
except ImportError:
logger.debug("Can not import PyPDF2")


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

use_brand_watermark = fields.Boolean(
default=False,
help="Use the pdf watermark defined globally in the brand settings.",
)

def _postprocess_wkhtmltopdf(self, result, **kwargs):
docids = self.env.context.get("res_ids", False)
if docids:
model_name = (
self.model or self._get_report(kwargs.get("report_ref", False)).model
)
first_record_id = docids[0]
record = self.env[model_name].browse(first_record_id)
if hasattr(record, "brand_id"):
brand = record.brand_id
if brand and brand.pdf_watermark:
watermark = b64decode(brand.pdf_watermark)

# Apply the watermark to the PDF
pdf = PdfFileWriter()
pdf_watermark = PdfFileReader(BytesIO(watermark))
for page in PdfFileReader(BytesIO(result)).pages:
watermark_page = pdf.addBlankPage(
page.mediaBox.getWidth(), page.mediaBox.getHeight()
)
watermark_page.mergePage(pdf_watermark.getPage(0))
watermark_page.mergePage(page)

pdf_content = BytesIO()
pdf.write(pdf_content)
result = pdf_content.getvalue()

return result

def _run_wkhtmltopdf(self, *args, **kwargs):
result = super()._run_wkhtmltopdf(*args, **kwargs)
return self._postprocess_wkhtmltopdf(result, **kwargs)
9 changes: 9 additions & 0 deletions brand_report_qweb_pdf_watermark/models/res_brand.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# © 2025 OBS Solutions BV <http://obs-solutions.com>

from odoo import fields, models


class ResBrand(models.Model):
_inherit = "res.brand"

pdf_watermark = fields.Binary("Watermark")
3 changes: 3 additions & 0 deletions brand_report_qweb_pdf_watermark/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
1 change: 1 addition & 0 deletions brand_report_qweb_pdf_watermark/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Emiel van Bokhoven \<<emiel.vanbokhoven@obs-solutions.com>\>
5 changes: 5 additions & 0 deletions brand_report_qweb_pdf_watermark/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This module was adds the functionality to use brand watermarks (backgrounds) to PDF reports.
This module will not add any reports on itself. It rather provides a "Use Brand Watermark" check box on reports.
And allows you to define a brand specific pdf watermark.
You will need to use it in combination with a module which adds branding possibilities to an model such as `account_brand`.
If the box is checked and pdf watermark is added to a brand. Then the watermark is added to the pdf.
4 changes: 4 additions & 0 deletions brand_report_qweb_pdf_watermark/readme/INSTALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This module depends on `brand_external_report_layout` and `report_qweb_pdf_watermark`
The dependenices are the same as with those modules, You need to install PyPDF2:

$ pip install pypdf2
18 changes: 18 additions & 0 deletions brand_report_qweb_pdf_watermark/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
To use this module, you need to:

Define a pdf watermark on brand.
1. go to settings --\> User & Companies --\> Brands
2. create or edit an brand
3. upload an pdf watermark. Note that resolutions and
size must match, otherwise you'll have funny results

To use the Brand watermark, on for example your invoices with the `account_brand` module you need to:

1. go to your report settings --\> Technical --\> Reports
(e.g. `account.report_invoice`)
2. Under Advanced Properties, Check the box "Use Brand Watermark".
3. Create a report e.g. an invoice, Set the brand and generate the report.
The brand watermark is added there.

If no brand information is available. No watermark is added.
It is possible to stack the pdf watermarks. E.g. use the "Company watermark" and "Brand Watermark" on top of each other.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 97ed7da

Please sign in to comment.