Skip to content

Conversation

@agordon
Copy link

@agordon agordon commented Aug 5, 2024

Description

Three usage examples with pictures of printed results - hopefully will help users get started quicker with python-escpos

Tested with

TM-P80 clone, called "MUNBYN 80mm USB Receipt Printer" , kernel device info:

usb 3-4.2: New USB device found, idVendor=04b8, idProduct=0e20, bcdDevice= 1.00
usb 3-4.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
usb 3-4.2: Product: TM-m30-ii
usb 3-4.2: Manufacturer: EPSON  

Copy link
Contributor

@belono belono left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @agordon and welcome to python-escpos!!.

This examples are great, however I left some suggestions and fixes to the code.
I still have to take a second look to the receipt.py but please be patient as I could be a bit slow with the review process.
By the moment we can start discussing and checking this little changes.
Thanks in advance!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could move this resource file to the graphics folder i.e examples/graphics/receipt/creature5.gif to keep the resource images into a separated folder as in other example files

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as in my previous comment: Move the resource file into the graphics/receipt sub-folder

Comment on lines +44 to +52
# Justify-text on left AND right sides by padding spaces,
# code by: Georgina Skibinski https://stackoverflow.com/a/66087666
def justify(txt: str, width: int) -> str:
prev_txt = txt
while (l := width - len(txt)) > 0:
txt = re.sub(r"(\s+)", r"\1 ", txt, count=l)
if txt == prev_txt:
break
return txt.rjust(width)
Copy link
Contributor

@belono belono Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this function.
I think we must add it to python-escpos as a private method so that other methods like block_text, software_columns and the like can make optional use of it.

What do you think @patkan @agordon ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, feel free to create a change request :-)

Copy link
Contributor

@belono belono left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second look.
I think I'm done.

Comment on lines +85 to +97
## Itemized list header (bold with underline)
p.set_with_default(bold=True, underline=True)
p.textln(header_format.format(**header))

## Itemized List
p.set_with_default()
for idx, item in enumerate(items):
txt = item_format.format(**item)
if idx == len(items) - 1:
# If this is the last item, add underline
# to visually "close" the list.
p.set_with_default(underline=True)
p.textln(txt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We recently implemented the new software_columns method which simplifies formatting and printing lines of text as columns from a list of text items, so now users have an easy way to print something like "tables".
It would be great if this example file could include that functionality.

# Font "b" on my TM-P80-clone printer can squeeze 64 characters per line
disclaimer_width = 64

recipt_barcode = "1234567890"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used.
Forgot to add the barcode to the receipt?
It would be great to see a barcode or a QR printed on the receipt to showcase the capabilities of python-escpos.

Comment on lines +129 to +136
txt = re.sub(" +", " ", txt)
# textwrap.wrap() ensures words are not broken (unlike "escpos.block_text()").
txt = textwrap.wrap(txt, width=disclaimer_width)
# Justify each line
txt = [justify(x, disclaimer_width) for x in txt]
p.set_with_default(font="b")
for l in txt:
p.textln(l)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
txt = re.sub(" +", " ", txt)
# textwrap.wrap() ensures words are not broken (unlike "escpos.block_text()").
txt = textwrap.wrap(txt, width=disclaimer_width)
# Justify each line
txt = [justify(x, disclaimer_width) for x in txt]
p.set_with_default(font="b")
for l in txt:
p.textln(l)
p.software_columns(text_list=[txt], widths=disclaimer_width, align='left')

Again, the software_columns method is your friend here.
This will print the text in disclaimer as a single column of left aligned text wrapped to fit within 64 characters max width per line.
The only lack would be the justification of the text that I would like to see be part of the python-escpos tools in the near future.

patkan and others added 5 commits August 24, 2024 23:25
Change import sorting

Co-authored-by: Benito López <belono@users.noreply.github.com>
Co-authored-by: Benito López <belono@users.noreply.github.com>
Co-authored-by: Benito López <belono@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants