[FIX]FIX issue in barcode text img

This commit is contained in:
zainab8585 2024-07-22 03:49:44 +02:00
parent 0ac1e45c08
commit 5a74818a73
1 changed files with 23 additions and 68 deletions

View File

@ -8,6 +8,7 @@ import base64
import os
from odoo.exceptions import ValidationError
# import barcode as barcode
from PIL import Image
from PIL import ImageDraw
@ -28,6 +29,7 @@ from odoo.tools.translate import _
# from odoo.osv.orm import setup_modifiers
class Transaction(models.Model):
_inherit = 'transaction.transaction'
@ -35,89 +37,42 @@ class Transaction(models.Model):
@api.constrains('ean13', 'name', 'transaction_date', 'type')
def binary_compute_constraint(self):
fonts = [os.path.dirname(__file__) + '/img/KacstOffice.ttf',
os.path.dirname(__file__) + '/img/amiri-regular.ttf']
font_path = os.path.join(os.path.dirname(__file__), 'img', 'amiri-regular.ttf')
font_size = 22
img = Image.new("RGBA", (500, 420), "white")
draw = ImageDraw.Draw(img)
number_word = "الرقم : "
number_word_reshaped = arabic_reshaper.reshape(
u'' + number_word)
number_word_artext = get_display(number_word_reshaped)
draw.text((220, 20),
number_word_artext, "black",
font=ImageFont.truetype(fonts[1], 18))
font = ImageFont.truetype(font_path, font_size)
number_value = self.name
number_value_reshaped = arabic_reshaper.reshape(
u'' + number_value if number_value else '')
number_value_artext = get_display(number_value_reshaped)
draw.text((80, 20),
number_value_artext, "black",
font=ImageFont.truetype(fonts[1], 18))
#
date_hijri = "التاريخ : "
date_hijri_reshaped = arabic_reshaper.reshape(
u'' + date_hijri)
date_hijri_artext = get_display(date_hijri_reshaped)
draw.text((211, 40),
date_hijri_artext, "black",
font=ImageFont.truetype(fonts[1], 18))
def draw_text(draw, text, position, font, alignment="left"):
text_size = draw.textsize(text, font=font)
date_hijri_value = self.transaction_date_hijri
date_hijri_value_reshaped = arabic_reshaper.reshape(
u'' + date_hijri_value if date_hijri_value else '')
date_hijri_artext = get_display(date_hijri_value_reshaped)
draw.text((120, 40),
date_hijri_artext.replace('-', '/'), "black",
font=ImageFont.truetype(fonts[1], 18))
if alignment == "right":
position = (position[0] - text_size[0], position[1])
elif alignment == "center":
position = (position[0] - text_size[0] / 2, position[1])
date_m = "الموافق : "
date_m_reshaped = arabic_reshaper.reshape(
u'' + date_m)
date_m_artext = get_display(date_m_reshaped)
draw.text((210, 65),
date_m_artext, "black",
font=ImageFont.truetype(fonts[1], 18))
draw.text(position, text, "black", font=font)
date_m_value = self.transaction_date
date_m_value_reshaped = arabic_reshaper.reshape(
u'' + str(date_m_value) if date_m_value else '')
date_m_value_artext = get_display(date_m_value_reshaped)
draw.text((120, 65),
date_m_value_artext.replace('-', '/'), "black",
font=ImageFont.truetype(fonts[1], 18))
# Draw text elements
draw_text(draw, (self.name or '')+' :رقم المعاملة ', (10, 20), font)
draw_text(draw, (self.transaction_date_hijri or '').replace('-', '/')+" :تاريخ المعاملة الهجري ", (10, 60), font)
draw_text(draw,(str(self.transaction_date) if self.transaction_date else '').replace('-', '/')+" :تاريخ المعاملة الميلادي ", (10, 100), font)
draw_text(draw,(str(self.attachment_num) if self.attachment_num else '0')+ " :رتبة المعاملة " , (10, 140), font)
attach_m = "المرفقات : "
attach_m_reshaped = arabic_reshaper.reshape(
u'' + attach_m)
date_m_artext = get_display(attach_m_reshaped)
draw.text((200, 85),
date_m_artext, "black",
font=ImageFont.truetype(fonts[1], 18))
attach_m_value = str(self.attachment_num) if self.attachment_num else '0'
attach_m_value_reshaped = arabic_reshaper.reshape(
u'' + attach_m_value)
attach_mvalue_artext = get_display(attach_m_value_reshaped)
draw.text((180, 85),
attach_mvalue_artext, "black",
font=ImageFont.truetype(fonts[1], 18))
# barcode_symbology = options.get('symbology', 'Code128')
barcode = self.env['ir.actions.report'].barcode('Code11', self.name, width=250, height=100,
humanreadable=0)
# Generate barcode
barcode = self.env['ir.actions.report'].barcode('Code11', self.name, width=250, height=100, humanreadable=0)
barcode_buffer = BytesIO(barcode)
barcode_image_file = Image.open(barcode_buffer)
ImageDraw.Draw(img)
img.paste(barcode_image_file, (20, 180))
# Save image to binary field
buffered = BytesIO()
img.paste(barcode_image_file, (20, 110))
img.save(buffered, format="png")
img_str = base64.b64encode(buffered.getvalue())
self.binary_barcode = img_str
class AttachmentInherit(models.Model):
_inherit = 'ir.attachment'