fix: card creation fix

This commit is contained in:
2025-05-12 17:56:25 +04:00
parent 4f08019d85
commit 329616bcd3
6 changed files with 62 additions and 18 deletions

View File

@@ -15,10 +15,15 @@ class BasePdfCardGenerator:
self._session = session
assets_folder = os.path.join(APP_PATH, 'assets')
fonts_folder = os.path.join(assets_folder, 'fonts')
font_file_path = os.path.join(fonts_folder, 'DejaVuSans.ttf')
dejavu_font_file_path = os.path.join(fonts_folder, 'DejaVuSans.ttf')
pdfmetrics.registerFont(TTFont('DejaVuSans', dejavu_font_file_path))
arial_font_file_path = os.path.join(fonts_folder, 'Arial Nova Cond.ttf')
pdfmetrics.registerFont(TTFont('Arial Nova Cond', arial_font_file_path))
self.page_width = 58 * mm
self.page_height = 40 * mm
pdfmetrics.registerFont(TTFont('DejaVuSans', font_file_path))
self.styles = getSampleStyleSheet()
self._set_small_paragraph_styles()
@@ -64,11 +69,9 @@ class BasePdfCardGenerator:
bottomMargin=1
)
def _get_paragraph_style(self, font_size: int):
def _get_paragraph_style(self, font_size: int, font_name: str = "Arial Nova Cond"):
common_paragraph_style = {
"parent": self.styles['Normal'],
"fontName": "DejaVuSans",
"spaceAfter": 4,
"fontName": font_name,
"fontSize": font_size,
}
return ParagraphStyle(

View File

@@ -1,16 +1,50 @@
import os
from io import BytesIO
from reportlab.graphics import renderPDF
from reportlab.lib.units import mm
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Paragraph, SimpleDocTemplate, Frame
from reportlab_qrcode import QRCodeImage
from svglib.svglib import svg2rlg
from constants import STATIC_PATH
from generators.base_pdf_card_generator.base_pdf_card_generator import BasePdfCardGenerator
from services.warehouse_management import WmsService
class WarehousePlaceQRCodeGenerator(BasePdfCardGenerator):
@staticmethod
def _get_big_font_size_from_str(value: str | int) -> int:
value = str(value)
dots = value.count(".")
string = value.replace(".", "")
length = len(string) + dots / 3
if length > 8:
return 30
elif length > 5:
return 37
return 48
@staticmethod
def _get_mid_font_size_from_str(value: str | int) -> int:
value = str(value)
dots = value.count(".")
string = value.replace(".", "")
length = len(string) + dots / 3
if length > 8:
return 22
elif length > 6:
return 26
elif length > 4:
return 30
return 34
async def generate(self, place_id: int, is_short: bool) -> BytesIO:
icon_path = os.path.join(STATIC_PATH, "icons", "denco.svg")
service = WmsService(self._session)
place_code = await service.get_code_of_place(place_id)
@@ -18,25 +52,28 @@ class WarehousePlaceQRCodeGenerator(BasePdfCardGenerator):
doc: SimpleDocTemplate = self._create_doc(buffer)
def on_first_page(canvas: Canvas, doc):
denco_paragraph = Paragraph('DENCO', self.small_centered_style)
svg_icon_scale = 0.04
drawing = svg2rlg(icon_path)
drawing.scale(svg_icon_scale, svg_icon_scale)
if is_short:
qr = QRCodeImage(place_code, size=20 * mm)
qr.drawOn(canvas, 39 * mm, -4)
qr = QRCodeImage(place_code, size=17 * mm)
qr.drawOn(canvas, 42 * mm, -4)
number_paragraph = Paragraph(str(place_code), self._get_paragraph_style(26))
number_frame = Frame(x1=0 * mm, y1=-15 * mm, width=42 * mm, height=30 * mm)
denco_frame = Frame(x1=0 * mm, y1=-2 * mm, width=42 * mm, height=20 * mm)
font_size = self._get_mid_font_size_from_str(place_code)
number_paragraph = Paragraph(str(place_code), self._get_paragraph_style(font_size))
number_frame = Frame(x1=0 * mm, y1=-15 * mm, width=45 * mm, height=30 * mm)
renderPDF.draw(drawing, canvas, 3, 35)
else:
qr = QRCodeImage(place_code, size=25 * mm)
qr.drawOn(canvas, 34 * mm, -4)
qr = QRCodeImage(place_code, size=20 * mm)
qr.drawOn(canvas, 39 * mm, -2 * mm)
number_paragraph = Paragraph(str(place_code), self._get_paragraph_style(34))
font_size = self._get_big_font_size_from_str(place_code)
number_paragraph = Paragraph(str(place_code), self._get_paragraph_style(font_size))
number_frame = Frame(x1=0 * mm, y1=9 * mm, width=60 * mm, height=30 * mm)
denco_frame = Frame(x1=0 * mm, y1=-13 * mm, width=58 * mm, height=20 * mm)
renderPDF.draw(drawing, canvas, 67, 3)
number_frame.addFromList([number_paragraph], canvas)
denco_frame.addFromList([denco_paragraph], canvas)
empty_paragraph = Paragraph("", self.small_centered_style)
elements = [empty_paragraph]