feat: end-point for pdf of all deal's barcodes, client and marketplace info in bill of payment
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import os
|
||||
from io import BytesIO
|
||||
from typing import List
|
||||
|
||||
from reportlab.graphics.barcode import code128
|
||||
from reportlab.lib.pagesizes import mm
|
||||
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
||||
@@ -8,6 +10,7 @@ from reportlab.pdfbase.ttfonts import TTFont
|
||||
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
|
||||
|
||||
from constants import APP_PATH
|
||||
from schemas.barcode import PdfBarcodeGenData
|
||||
|
||||
|
||||
class PDFGenerator:
|
||||
@@ -82,7 +85,7 @@ class PDFGenerator:
|
||||
buffer.seek(0)
|
||||
return buffer
|
||||
|
||||
def generate(self, barcode_value, text, num_duplicates=1):
|
||||
def generate(self, barcodes_data: List[PdfBarcodeGenData]):
|
||||
buffer = BytesIO()
|
||||
|
||||
# Создаем документ с указанным размером страницы
|
||||
@@ -93,43 +96,58 @@ class PDFGenerator:
|
||||
topMargin=1 * mm,
|
||||
bottomMargin=1 * mm)
|
||||
|
||||
# Создаем абзац с новым стилем
|
||||
paragraph = Paragraph(text, self.small_style)
|
||||
# Список элементов для добавления в документ
|
||||
elements = []
|
||||
product_barcode_canvases = []
|
||||
product_counter = 0
|
||||
product_barcodes_counter = 0
|
||||
|
||||
# Получаем ширину и высоту абзаца
|
||||
paragraph_width, paragraph_height = paragraph.wrap(self.page_width - 2 * mm, self.page_height)
|
||||
for barcode_data in barcodes_data:
|
||||
# Создаем абзац с новым стилем
|
||||
paragraph = Paragraph(barcode_data.text, self.small_style)
|
||||
|
||||
# Рассчитываем доступное пространство для штрихкода
|
||||
human_readable_height = 6 * mm # Высота human-readable текста
|
||||
space_between_text_and_barcode = 4 * mm # Отступ между текстом и штрихкодом
|
||||
barcode_height = self.page_height - paragraph_height - human_readable_height - space_between_text_and_barcode - 4 * mm # Учитываем поля и отступы
|
||||
# Получаем ширину и высоту абзаца
|
||||
paragraph_width, paragraph_height = paragraph.wrap(self.page_width - 2 * mm, self.page_height)
|
||||
|
||||
# Создаем штрихкод
|
||||
available_width = self.page_width - 2 * mm # Учитываем поля
|
||||
# Рассчитываем доступное пространство для штрихкода
|
||||
human_readable_height = 6 * mm # Высота human-readable текста
|
||||
space_between_text_and_barcode = 4 * mm # Отступ между текстом и штрихкодом
|
||||
barcode_height = self.page_height - paragraph_height - human_readable_height - space_between_text_and_barcode - 4 * mm # Учитываем поля и отступы
|
||||
|
||||
# Приблизительное количество элементов в штрихкоде Code 128 для средней длины
|
||||
num_elements = 11 * len(barcode_value) # Примерная оценка: 11 элементов на символ
|
||||
# Создаем штрихкод
|
||||
available_width = self.page_width - 2 * mm # Учитываем поля
|
||||
|
||||
# Рассчитываем ширину штриха
|
||||
bar_width = available_width / num_elements
|
||||
barcode = code128.Code128(barcode_value,
|
||||
barWidth=bar_width,
|
||||
barHeight=barcode_height,
|
||||
humanReadable=True)
|
||||
# Приблизительное количество элементов в штрихкоде Code 128 для средней длины
|
||||
num_elements = 11 * len(barcode_data.barcode_value) # Примерная оценка: 11 элементов на символ
|
||||
|
||||
# Рассчитываем ширину штриха
|
||||
bar_width = available_width / num_elements
|
||||
barcode = code128.Code128(barcode_data.barcode_value,
|
||||
barWidth=bar_width,
|
||||
barHeight=barcode_height,
|
||||
humanReadable=True)
|
||||
product_barcode_canvases.append(barcode)
|
||||
|
||||
# Добавление штрихкодов в список элементов документа
|
||||
for _ in range(barcode_data.num_duplicates):
|
||||
elements.append(paragraph)
|
||||
elements.append(Spacer(1, space_between_text_and_barcode)) # Отступ между текстом и штрихкодом
|
||||
elements.append(PageBreak())
|
||||
|
||||
# Функция для отрисовки штрихкода на canvas
|
||||
def add_barcode(canvas, doc):
|
||||
barcode_width = barcode.width
|
||||
nonlocal product_barcodes_counter, product_counter
|
||||
|
||||
barcode_canvas = product_barcode_canvases[product_counter]
|
||||
barcode_width = barcode_canvas.width
|
||||
barcode_x = (self.page_width - barcode_width) / 2 # Центрируем штрихкод
|
||||
barcode_y = human_readable_height + 2 * mm # Размещаем штрихкод снизу с учетом отступа
|
||||
barcode.drawOn(canvas, barcode_x, barcode_y)
|
||||
barcode_canvas.drawOn(canvas, barcode_x, barcode_y)
|
||||
|
||||
# Создаем список элементов для добавления в документ
|
||||
elements = []
|
||||
for _ in range(num_duplicates):
|
||||
elements.append(paragraph)
|
||||
elements.append(Spacer(1, space_between_text_and_barcode)) # Отступ между текстом и штрихкодом
|
||||
elements.append(PageBreak())
|
||||
product_barcodes_counter += 1
|
||||
if product_barcodes_counter >= barcodes_data[product_counter].num_duplicates:
|
||||
product_barcodes_counter = 0
|
||||
product_counter += 1
|
||||
|
||||
# Создаем документ
|
||||
doc.build(elements[:-1], onFirstPage=add_barcode, onLaterPages=add_barcode) # Убираем последний PageBreak
|
||||
|
||||
Reference in New Issue
Block a user