Everything was fine until I decided to add a new argument –last_index, after which the parameters began to be passed arbitrarily, if the argument –last_index is removed from the code, everything is fine.
import os
import sys
import argparse
from treepoem import generate_barcode
from reportlab.lib.pagesizes import mm, landscape, portrait
from reportlab.pdfgen import canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase import pdfmetrics
from PyPDF2 import PdfWriter, PdfReader
def register_fonts(font_name):
available_fonts = {
"Arial Narrow": "ARIALN.ttf",
"Bahnschrift": "Bahnschrift.ttf",
"Bahnschrift Light SemiCondensed": "Bahnschrift.ttf",
"Helvetica": "Helvetica.ttf" # Add paths to other font files as needed
}
if font_name in available_fonts:
font_path = available_fonts[font_name]
try:
pdfmetrics.registerFont(TTFont(font_name, font_path))
print(f"Font {font_name} registered successfully.")
except Exception as e:
print(f"Error registering font {font_name}: {e}")
sys.exit(1)
else:
print(f"Font {font_name} is not available.")
sys.exit(1)
def create_barcode(output_path, logo_paths, page_size, orientation, font_name, btext_align,
btext_size, index, generate_index, index_font_size, index_offset,
barcode_data, barcode_type, barcode_width,
barcode_height, barcode_align, custom_texts, text_align, text_size,
logo_text_interval, text_intervals, barcode_interval,
index_align):
try:
page_width, page_height = [x * mm for x in page_size]
if orientation == 'landscape':
page_width, page_height = landscape((page_width, page_height))
else:
page_width, page_height = portrait((page_width, page_height))
c = canvas.Canvas(output_path, pagesize=(page_width, page_height))
# Starting y position for the first element
img_y = page_height - barcode_interval
# Barcode
if barcode_data:
barcode = generate_barcode(
barcode_type=barcode_type,
data=barcode_data
)
barcode_img = barcode.convert("RGB")
barcode_temp_path = "temp_barcode.png"
barcode_img.save(barcode_temp_path)
if barcode_align == 'center':
barcode_x = (page_width - barcode_width * mm) / 2
elif barcode_align == 'right':
barcode_x = page_width - barcode_width * mm - 5 * mm
else:
barcode_x = 1 * mm
barcode_y = img_y - barcode_height * mm
c.drawImage(barcode_temp_path, barcode_x, barcode_y, width=barcode_width * mm,
height=barcode_height * mm)
os.remove(barcode_temp_path)
# Adjust y position for DATA element after barcode
#img_y -= barcode_height * mm + barcode_text_interval * mm
img_y -= barcode_height * mm + 5 * mm
# Draw barcode data below the barcode
c.setFont(font_name, btext_size)
barcode_data_width = pdfmetrics.stringWidth(barcode_data, font_name, btext_size)
if btext_align == 'center':
barcode_data_x = (page_width - barcode_data_width) / 2
elif btext_align == 'right':
barcode_data_x = page_width - barcode_data_width - 5 * mm
else:
barcode_data_x = 1 * mm
c.drawString(barcode_data_x, img_y, barcode_data)
# Adjust position for next element
img_y -= btext_size + text_intervals
# Custom Texts
c.setFont(font_name, text_size)
for custom_text in custom_texts:
lines = custom_text.split('\n')
for line in lines:
words = line.split()
current_line = words[0]
for word in words[1:]:
width = pdfmetrics.stringWidth(current_line + ' ' + word, font_name, text_size)
if width < page_width - 2 * mm:
current_line += ' ' + word
else:
line_width = pdfmetrics.stringWidth(current_line, font_name, text_size)
if text_align == 'center':
text_x = (page_width - line_width) / 2
elif text_align == 'right':
text_x = page_width - line_width - 1 * mm
else:
text_x = 1 * mm
c.drawString(text_x, img_y, current_line)
img_y -= text_size + 1 * mm # Adjust position for next line
current_line = word
if current_line:
line_width = pdfmetrics.stringWidth(current_line, font_name, text_size)
if text_align == 'center':
text_x = (page_width - line_width) / 2
elif text_align == 'right':
text_x = page_width - line_width - 1 * mm
else:
text_x = 1 * mm
c.drawString(text_x, img_y, current_line)
# Adjust position for next line
img_y -= text_size + 1 * mm
if generate_index:
c.setFont(font_name, index_font_size)
index_text = f"page: {index}"
index_text_width = pdfmetrics.stringWidth(index_text, font_name, index_font_size)
if index_align == 'center':
index_text_x = (page_width - index_text_width) / 2
elif index_align == 'right':
index_text_x = page_width - index_text_width - 1 * mm
else:
index_text_x = 1 * mm
# c.drawString(index_text_x * mm, index_offset * mm, index_text)
c.drawString(index_text_x, img_y - index_offset * mm, index_text)
# Adjust position for next element
img_y -= index_offset * mm + index_font_size
# if logo_path:
# l ogo_x = (page_width - 9 * mm)
# logo_y = 1 * mm
# c.drawImage(logo_path, logo_x, logo_text_interval * mm, width=6 * mm, height=6 * mm)
if logo_paths:
logo_y = logo_text_interval * mm
if len(logo_paths) == 1:
logo_x = (page_width - 9 * mm) # Centering the single logo
elif len(logo_paths) == 2:
logo_x = (page_width - 14 * mm) # Centering the two logos
elif len(logo_paths) == 3:
logo_x = (page_width - 21 * mm) # Centering the three logos
else:
logo_x = 1 * mm # Default left-aligned start for more than three logos
for logo_path in logo_paths:
if os.path.exists(logo_path):
c.drawImage(logo_path, logo_x, logo_y, width=6 * mm, height=6 * mm)
# Adjust X position for the next logo with a small interval
logo_x += 7 * mm
c.showPage()
c.save()
print(f"Barcode PDF saved as {output_path}")
except Exception as e:
print(f"Error creating Barcode: {e}")
sys.exit(1)
def generate_pdfs(logo_paths, page_size, orientation, font_name, output_mode, start_index,
generate_index, index_font_size, index_offset, barcode_data, barcode_type,
barcode_width, barcode_height, barcode_align, custom_texts,
text_size, text_align, btext_size, btext_align, logo_text_interval,
text_intervals, barcode_interval, index_align, last_index):
register_fonts(font_name)
# Define your output directory here
output_dir = 'output_pdfs'
os.makedirs(output_dir, exist_ok=True)
start_index = int(start_index)
last_index = int(last_index)
# Debugging prints
#print(f"start_index: {start_index}, last_index: {last_index}")
print(
f"page_size: {page_size}, orientation: {orientation}, barcode_data: {barcode_data}, barcode_type: {barcode_type}, start_index: {start_index}, last_index: {last_index}, font_name: {font_name}, output_mode: {output_mode}, generate_index: {generate_index}, index_font_size: {index_font_size}, index_offset: {index_offset}, barcode_width: {barcode_width}, barcode_height: {barcode_height}, barcode_align: {barcode_align}, text_align: {text_align}, text_size: {text_size}, btext_size: {btext_size}, btext_align: {btext_align}, logo_paths: {logo_paths}, logo_text_interval: {logo_text_interval}, text_intervals: {text_intervals}, index_align: {index_align}, barcode_interval: {barcode_interval}, custom_texts: {custom_texts}")
if output_mode == "individual":
output_pdf = os.path.join(output_dir, f"barcode_{start_index}.pdf")
create_barcode(output_pdf, logo_paths, page_size, orientation, font_name, btext_align, btext_size, start_index,
generate_index, index_font_size, index_offset, barcode_data, barcode_type, barcode_width,
barcode_height, barcode_align, custom_texts, text_align, text_size, logo_text_interval,
text_intervals, barcode_interval, index_align)
print(f"Individual barcode PDFs saved in {output_dir}")
elif output_mode == "combined":
writer = PdfWriter()
for i in range(start_index, last_index + 1):
temp_pdf = os.path.join(output_dir, f"barcode_{i}.pdf")
create_barcode(temp_pdf, logo_paths, page_size, orientation, font_name, btext_align, btext_size, i,
generate_index, index_font_size, index_offset, barcode_data, barcode_type, barcode_width,
barcode_height, barcode_align, custom_texts, text_align, text_size, logo_text_interval,
text_intervals, barcode_interval, index_align)
writer.add_page(PdfReader(temp_pdf).pages[0])
os.remove(temp_pdf)
combined_output_pdf = os.path.join(output_dir, "combined_barcode.pdf")
with open(combined_output_pdf, "wb") as f:
writer.write(f)
print(f"Combined barcode PDF saved as {combined_output_pdf}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate barcode PDFs with custom configurations.")
parser.add_argument("--page_size", type=float, nargs=2, required=True, help="Page size in mm (width height).")
parser.add_argument("--orientation", type=str, choices=["portrait", "landscape"], default="portrait",
help="Page orientation.")
parser.add_argument("--barcode_data", type=str, required=True, help="Data for the barcode.")
parser.add_argument("--barcode_type", type=str, required=True, help="Type of the barcode.")
parser.add_argument("--barcode_width", type=float, default=38, help="Width of the barcode in mm.")
parser.add_argument("--barcode_height", type=float, default=20, help="Height of the barcode in mm.")
parser.add_argument("--barcode_align", type=str, choices=["left", "center", "right"], default="center",
help="Alignment of the barcode.")
parser.add_argument("--font_name", type=str, default="Helvetica", help="Font name for text.")
parser.add_argument("--btext_size", type=float, default=10, help="Font size for the custom texts.")
parser.add_argument("--btext_align", type=str, choices=["left", "center", "right"], default="left",
help="Alignment of the custom texts.")
parser.add_argument("--custom_texts", type=str, nargs="+", default=[], help="Custom texts to be added to the PDF.")
parser.add_argument("--text_size", type=float, default=10, help="Font size for the custom texts.")
parser.add_argument("--text_align", type=str, choices=["left", "center", "right"], default="center",
help="Alignment of the custom texts.")
parser.add_argument("--generate_index", action="store_true", help="Generate page index.")
parser.add_argument("--start_index", type=int, default=1, help="Starting index for page numbering.")
parser.add_argument("--last_index", type=int, default=18, help="End index for page numbering.")
parser.add_argument("--index_font_size", type=float, default=10, help="Font size for page index.")
parser.add_argument("--index_align", type=str, choices=["left", "center", "right"], default="right",
help="Alignment of the custom texts.")
parser.add_argument("--index_offset", type=float, default=5, help="Offset for the page index in mm.")
parser.add_argument("--logo_paths", type=str, default=None, nargs='+', help="Path to the logo image.")
parser.add_argument("--logo_text_interval", type=float, default=1, help="Interval between logo and text in mm.")
#parser.add_argument("--barcode_text_interval", type=float, default=12, help="Interval between barcode and btext in mm.")
parser.add_argument("--text_intervals", type=float, default=5,
help="Intervals between btext and text of text in mm.")
parser.add_argument("--barcode_interval", type=float, default=5,
help="Intervals between btext and text of text in mm.")
parser.add_argument("--output_mode", type=str, choices=["individual", "combined"], default="individual",
help="Output mode: individual or combined PDFs.")
args = parser.parse_args()
generate_pdfs(
args.logo_paths,
args.page_size,
args.orientation,
args.font_name,
args.output_mode,
args.start_index,
args.last_index,
args.generate_index,
args.index_font_size,
args.index_offset,
args.barcode_data,
args.barcode_type,
args.barcode_width,
args.barcode_height,
args.barcode_align,
args.custom_texts,
args.text_size,
args.text_align,
args.btext_size,
args.btext_align,
args.logo_text_interval,
#args.barcode_text_interval,
args.text_intervals,
args.index_align,
args.barcode_interval
)
C:Usersuser.virtualenvspythonProject1_3-mr3WyfgDScriptspython.exe
C:/Users/user/PycharmProjects/pythonProject1_3/main5_test.py
--page_size 55 40
--orientation "landscape"
--barcode_data "2040609891546"
--barcode_type "code128"
--barcode_width 38
--barcode_height 10
--barcode_align "center"
--font_name "Arial Narrow"
--btext_size 10
--btext_align "center"
--custom_texts "Art: 12345" "My text"
--text_size 9
--text_align "center"
--generate_index
--index_font_size 9
--logo_paths logo2.png logo1.png
--logo_text_interval 2
--text_intervals 4
--barcode_interval 15
--output_mode "combined"
Font Arial Narrow registered successfully.
page_size: [55.0, 40.0],
orientation: "landscape",
*barcode_data: 5,
barcode_type: "2040609891546", *
start_index: 1,
*last_index: 15,*
font_name: "Arial Narrow",
output_mode: "combined",
*generate_index: 18,*
*index_font_size: True,*
index_offset: 9.0,
*barcode_width: "code128",*
barcode_height: 38.0,
barcode_align: 10.0,
*text_align: 9.0, *
*text_size: ['Art: 12345', 'My text'], *
*btext_size: "center", *
*btext_align: 10.0, *
logo_paths: ['logo2.png', 'logo1.png'],
logo_text_interval: "center",
text_intervals: 2.0,
index_align: "right",
barcode_interval: 4.0,
custom_texts: "center"
Error creating Barcode: unsupported barcode type '2040609891546'
Process finished with exit code 1
If you remove any other argument instead, it does not help.
I checked that the arguments were received using the print function, but I still don’t understand why the arguments began to get confused
New contributor
Valentin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4