[FIX] account: print image in pdf

To Reproduce
============
- create a vendor bill and add an image as attachment
- print Original Bill -> the generated pdf can't be loaded

Problem
=======
when trying to add an image attachment to pdf report, the image needs
a special process.

Solution
========
add the image processing when printing pdf
fix inspired from 15 [commit](https://github.com/odoo/odoo/commit/b82a6cdea21a66588ddcafcdfb5026ec22546745)

opw-3127535

closes odoo/odoo#112596

X-original-commit: 41c79f66cfb5cdb558110d32842198157932397e
Signed-off-by: William André (wan) <wan@odoo.com>
This commit is contained in:
Abdelouahab (abla)
2023-02-13 23:42:06 +01:00
committed by william-andre
parent 03aa01aff7
commit 82e17cff03
2 changed files with 18 additions and 4 deletions
+2 -3
View File
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
import io
import textwrap
from collections import OrderedDict
from PyPDF2.utils import PdfStreamError
@@ -29,8 +28,8 @@ class IrActionsReport(models.Model):
for invoice in invoices:
attachment = invoice.message_main_attachment_id
if attachment:
stream = io.BytesIO(attachment.raw)
if attachment.mimetype == 'application/pdf':
stream = pdf.to_pdf_stream(attachment)
if stream:
record = self.env[attachment.res_model].browse(attachment.res_id)
try:
stream = pdf.add_banner(stream, record.name, logo=True)
+16 -1
View File
@@ -9,7 +9,7 @@ from logging import getLogger
from PyPDF2 import PdfFileWriter, PdfFileReader
from PyPDF2.generic import DictionaryObject, NameObject, ArrayObject, DecodedStreamObject, NumberObject, createStringObject, ByteStringObject
from zlib import compress, decompress
from PIL import Image
from PIL import Image, PdfImagePlugin
from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.utils import ImageReader
@@ -28,6 +28,9 @@ REGEX_SUBTYPE_UNFORMATED = re.compile(r'^\w+/[\w-]+$')
REGEX_SUBTYPE_FORMATED = re.compile(r'^/\w+#2F[\w-]+$')
# Disable linter warning: this import is needed to make sure a PDF stream can be saved in Image.
PdfImagePlugin.__name__
# make sure values are unwrapped by calling the specialized __getitem__
def _unwrapping_get(self, key, default=None):
try:
@@ -84,6 +87,18 @@ def rotate_pdf(pdf):
return _buffer.getvalue()
def to_pdf_stream(attachment) -> io.BytesIO:
"""Get the byte stream of the attachment as a PDF."""
stream = io.BytesIO(attachment.raw)
if attachment.mimetype == 'application/pdf':
return stream
elif attachment.mimetype.startswith('image'):
output_stream = io.BytesIO()
Image.open(stream).convert("RGB").save(output_stream, format="pdf")
return output_stream
_logger.warning("mimetype (%s) not recognized for %s", attachment.mimetype, attachment)
def add_banner(pdf_stream, text=None, logo=False, thickness=2 * cm):
""" Add a banner on a PDF in the upper right corner, with Odoo's logo (optionally).