Coder Social home page Coder Social logo

Tabbed text about python-escpos HOT 8 OPEN

tmblog avatar tmblog commented on July 18, 2024 1
Tabbed text

from python-escpos.

Comments (8)

belono avatar belono commented on July 18, 2024 1

Hi and thanks for your question!

The way Epson ESC/POS handles horizontal tabs (HT) is:

  1. Inform the printer how to handle the character tab (number of tabs to be handled and length of each tab)
  2. Send text and tabs separately.

In python-escpos we do the above using the methods:

  1. .control("HT", count, tab_size)
  2. .text(...) and .text("\t")

So to get the following output (imagine underscores are spaces):

tabbed________text________8

You write:

p.control("HT", count=2, tab_size=8)  # handle 2 tabs of 8 characters size
p.text("tabbed")
p.text("\t")  # TAB nº1
p.text("text")
p.text("\t")  # TAB nº2
p.text("8")
p.ln()  # carriage return

Hope this helps.
Don't doubt to ask for help if you need it.
Cheers!

from python-escpos.

belono avatar belono commented on July 18, 2024 1

Hello!

Your question reminded me of issue #27, our oldest open issue, a few days ago. This inspired me to write a fix for this fairly common need and I hope to be ready to open a draft pull request in a few days.

My approach is to add a helper method that organizes a list of strings into columns of the same width. The main problem I'm facing is what to do when a string exceeds the column width. I have a few approaches in mind, but I have yet to explore them.

Greetings and stay tuned. ;-)

from python-escpos.

gabri3l0 avatar gabri3l0 commented on July 18, 2024

@tmblog what was the steps to configure and test the printer on windows? could you help me please?

from python-escpos.

Peppershade avatar Peppershade commented on July 18, 2024

I have the same question. I tried with set align, but that doesn't seem to do the trick when applied to the same line

from python-escpos.

tmblog avatar tmblog commented on July 18, 2024

Hi and thanks for your question!

The way Epson ESC/POS handles horizontal tabs (HT) is:

1. Inform the printer how to handle the character tab (number of tabs to be handled and length of each tab)

2. Send text and tabs separately.

In python-escpos we do the above using the methods:

1. `.control("HT", count, tab_size)`

2. `.text(...)` and `.text("\t")`

So to get the following output (imagine underscores are spaces):

tabbed________text________8

You write:

p.control("HT", count=2, tab_size=8)  # handle 2 tabs of 8 characters size
p.text("tabbed")
p.text("\t")  # TAB nº1
p.text("text")
p.text("\t")  # TAB nº2
p.text("8")
p.ln()  # carriage return

Hope this helps. Don't doubt to ask for help if you need it. Cheers!

That's great thank you!
What method would you recommend to programmatically calculate the end of the paper so I can place the price for a bill.

from python-escpos.

tmblog avatar tmblog commented on July 18, 2024

@tmblog what was the steps to configure and test the printer on windows? could you help me please?

Hello I used the default printer without the pid and vid.
See this:

from escpos.printer import Win32Raw

def print_escpos_receipt():
    try:
        # Create a printer object and open connection to default printer
        printer = Win32Raw()
        printer.open()

        printer.set(normal_textsize=1, double_height=1, height=8, bold=1)
        printer.textln("Collection - duplicate #101")
        printer.ln(2)
        printer.set(normal_textsize=1, double_height=1, height=8)
        printer.textln("Processing on Tue 09 Apr 01:25AM")

And so on

from python-escpos.

belono avatar belono commented on July 18, 2024

What method would you recommend to programmatically calculate the end of the paper so I can place the price for a bill.

It depends.
If you just want to place a single line of text containing a price like the typical 'Total' or 'Amount: £XXX,XX' right-aligned text found in sales invoices, then the simplest way is .set(align="right") to set the printer to print right-aligned.

On the other had, the only way to know the "end" of the paper is to know the number of columns of the printer, but that value changes between models and also by the choice of the printer font (A or B), and other settings.
Hopefully, most of this information can be found in the capabilities database as long as the printer is on the list of supported printers and the capabilities database contains the data.

You can access the capabilities data of a printer by passing the profile parameter with the profile name when instantiating a connector, and getting the profile dictionary, or calling the profile methods. For example:

import escpos.printer as printer

p = printer.Dummy(profile="TM-T20II")
print(p.profile.profile_data)
n_cols = p.profile.get_columns(font="a")

You can then use this data to position the text with the help of tabs or other methods.

from python-escpos.

tmblog avatar tmblog commented on July 18, 2024

What method would you recommend to programmatically calculate the end of the paper so I can place the price for a bill.

It depends. If you just want to place a single line of text containing a price like the typical 'Total' or 'Amount: £XXX,XX' right-aligned text found in sales invoices, then the simplest way is .set(align="right") to set the printer to print right-aligned.

On the other had, the only way to know the "end" of the paper is to know the number of columns of the printer, but that value changes between models and also by the choice of the printer font (A or B), and other settings. Hopefully, most of this information can be found in the capabilities database as long as the printer is on the list of supported printers and the capabilities database contains the data.

You can access the capabilities data of a printer by passing the profile parameter with the profile name when instantiating a connector, and getting the profile dictionary, or calling the profile methods. For example:

import escpos.printer as printer

p = printer.Dummy(profile="TM-T20II")
print(p.profile.profile_data)
n_cols = p.profile.get_columns(font="a")

You can then use this data to position the text with the help of tabs or other methods.

Hi thank you for your help!
I was referring to having line items on a bill e.g. item on left and price on the right.
I found that letting python do that makes it much easier than configuring printer tabs etc.

white_space = " ";
text = "Item 1" + 32 * white_space + "£9.99"
# programmatically adjust white space based on paper width.

from python-escpos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.