feat: new barcode generator

This commit is contained in:
2024-06-11 15:15:48 +03:00
parent dce52cd883
commit ba73d5cb09

View File

@@ -82,10 +82,10 @@ class PDFGenerator:
buffer.seek(0)
return buffer
def generate_large_text(self, barcode_value, text, num_duplicates=1):
def generate(self, barcode_value, text, num_duplicates=1):
buffer = BytesIO()
# Create document with specified page size
# Создаем документ с указанным размером страницы
doc = SimpleDocTemplate(buffer,
pagesize=(self.page_width, self.page_height),
rightMargin=1 * mm,
@@ -93,35 +93,47 @@ class PDFGenerator:
topMargin=1 * mm,
bottomMargin=1 * mm)
# Create paragraph with new style
# Создаем абзац с новым стилем
paragraph = Paragraph(text, self.small_style)
# Create barcode
barcode = code128.Code128(barcode_value, humanReadable=True)
# Получаем ширину и высоту абзаца
paragraph_width, paragraph_height = paragraph.wrap(self.page_width - 2 * mm, self.page_height)
# Function to draw barcode on canvas
# Рассчитываем доступное пространство для штрихкода
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 # Учитываем поля и отступы
# Создаем штрихкод
available_width = self.page_width - 2 * mm # Учитываем поля
# Приблизительное количество элементов в штрихкоде Code 128 для средней длины
num_elements = 11 * len(barcode_value) # Примерная оценка: 11 элементов на символ
# Рассчитываем ширину штриха
bar_width = available_width / num_elements
barcode = code128.Code128(barcode_value,
barWidth=bar_width,
barHeight=barcode_height,
humanReadable=True)
# Функция для отрисовки штрихкода на canvas
def add_barcode(canvas, doc):
barcode_width = barcode.width
barcode_x = (self.page_width - barcode_width) / 2 # Center the barcode
barcode.drawOn(canvas, barcode_x, self.page_height - barcode.height - 5)
barcode_x = (self.page_width - barcode_width) / 2 # Центрируем штрихкод
barcode_y = human_readable_height + 2 * mm # Размещаем штрихкод снизу с учетом отступа
barcode.drawOn(canvas, barcode_x, barcode_y)
# Create list of elements to add to the document
# Создаем список элементов для добавления в документ
elements = []
for _ in range(num_duplicates):
elements.append(Spacer(1, 8 * mm))
elements.append(paragraph)
elements.append(Spacer(1, space_between_text_and_barcode)) # Отступ между текстом и штрихкодом
elements.append(PageBreak())
# Build document
doc.build(elements[:-1], onFirstPage=add_barcode, onLaterPages=add_barcode) # Remove the last PageBreak
# Создаем документ
doc.build(elements[:-1], onFirstPage=add_barcode, onLaterPages=add_barcode) # Убираем последний PageBreak
buffer.seek(0)
return buffer
def generate(self, barcode_value, text, num_duplicates=1):
paragraph = Paragraph(text, self.small_style)
# Get the width and height of the paragraph
paragraph_width, paragraph_height = paragraph.wrap(self.page_width - 2 * mm, self.page_height)
if paragraph_height > 30:
return self.generate_large_text(barcode_value, text, num_duplicates)
return self.generate_small_text(barcode_value, text, num_duplicates)