Coder Social home page Coder Social logo

Comments (5)

NielsLeenheer avatar NielsLeenheer commented on June 7, 2024 3

What I've been doing is similar to @mariapaulinar. Set a maximum number of characters for a line and do the formatting yourself. Determine the length of the left string, and the right string and calculate the number of spaces you must insert between them.

It works. But having a table feature would definitely be easier. If I can find some time, I'll look into adding more advanced layout methods.

from escposencoder.

NielsLeenheer avatar NielsLeenheer commented on June 7, 2024 3

The next version of EscPosEncoder will support tables with a syntax similar to this:

    let result = encoder
        .table(
            [
                { width: 36, marginRight: 2, align: 'left' },
                { width: 10, align: 'right' }
            ], 
            [
                [ 'Item 1', '€ 10,00' ],
                [ 'Item 2', '15,00' ],
                [ 'Item 3', '9,95' ],
                [ 'Item 4', '4,75' ],
                [ 'Item 5', '211,05' ],
                [ '', '='.repeat(10) ],
                [ 'Total', (encoder) => encoder.bold().text('€ 250,75').bold() ],
            ]
        )	
        .encode()

from escposencoder.

mariapaulinar avatar mariapaulinar commented on June 7, 2024 1

Maybe this code can help you (It was made with Angular):

    const encoder = new EscPosEncoder();
    let result = encoder
      .codepage('cp852')
      .newline()
      .newline()
      .align('center')
      .line('Recibo de caja')
      .bold(true)
      .line('FACTURA     SALDO        PAGO')
      .bold(false);
    const maxChar = 11;
    for (const item of resp.facturas) {
      const cantFactura = (item.numeroFactura.toString().length <= maxChar) ? maxChar - item.numeroFactura.toString().length : 0;
      const cantSaldo = (formatNumber(item.saldo, 'en-US').length <= maxChar) ? maxChar - formatNumber(item.saldo, 'en-US').length : 0;
      const cantPago = (formatNumber(item.valorConsignar, 'en-US').length <= maxChar) ? maxChar - formatNumber(item.valorConsignar, 'en-US').length : 0;
      const line = item.numeroFactura + ' '.repeat(cantFactura) + ' $' + formatNumber(item.saldo, 'en-US') + ' '.repeat(cantSaldo) + ' $' + formatNumber(item.valorConsignar, 'en-US') + ' '.repeat(cantPago);
      result = result.line(line);
    }
    result = result
      .line('-------------------------------')
      .line('TOTAL PAGADO ' + resp.totalPagado)
      .newline()
      .newline()
      .line('Formas Pago')
      .line('-------------------------------');
    for (const item of resp.formasPago) {
      result = result.line(item.tipo + '   $' + item.valor);
    }
    result = result
      .line('-------------------------------')
      .line('TOTAL PAGADO ' + resp.totalPagado)
      .newline()
      .newline()
      .newline()
      .encode();

from escposencoder.

marquipaul avatar marquipaul commented on June 7, 2024 1

Hi, @mariapaulinar your code example was very helpful. Thank you

Yes, it will be easier. Thank you for this package very helpful. @NielsLeenheer

from escposencoder.

JJ-Alcuran avatar JJ-Alcuran commented on June 7, 2024

Hi @NielsLeenheer.
I used this .table( ... ) syntax and it is not working.
Do you have a demo

This my code using Ionic Angular but an error says .table( ) is not a function.
I think I did a wrong way. Please help me with a working example

const encoder = new EscPosEncoder();
const result = encoder.initialize();

result
  .align('center')
  .newline()
  .line('Transaction Receipt')
  .bold(false)
  .align('left')
  .line('Transaction Number: ' + this.generatedNumber)
  .line('Transaction Date: ' + this.dateTime)
  .newline()
  .newline()
  .line('-------------------------------')
  .table(
    [
        { width: 36, marginRight: 2, align: 'left' },
        { width: 10, align: 'right' }
    ], 
    [
        [ 'Item 1', '50' ],
        [ 'Item 2', '60' ],
        [ 'Item 3', '10' ],
        [ 'Item 4', '40' ],
        [ 'Item 5', '60' ],
        [ 'Total', '220' ],
    ]
  )	
  .newline()
  .newline()
  .newline()
  .encode();

from escposencoder.

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.