Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview mode #39

Open
yelhouti opened this issue Nov 5, 2022 · 1 comment
Open

Preview mode #39

yelhouti opened this issue Nov 5, 2022 · 1 comment

Comments

@yelhouti
Copy link

yelhouti commented Nov 5, 2022

Hey there, great library, thanks for the work.

I can see that for now you have two connection types, do you plan to add others for network, bluetooth ...?
Also I was wondering how faisable it is to have a preview of the receipt. ie: connection=Image/PDF Do you know of any code able to do that ?
That would make the app much more attractive I think and would allow to print to other non escpos printers as a fallback

@ma-zal
Copy link

ma-zal commented Jun 12, 2024

Preview into PDF is the most probably absolutely out of the scope, because the PDF is totally different format than ESC/POS. Therefore the effort of developing something like PDF export could be enormous.

But anybody can implement it on own side by extending existing classes/interfaces. By same aproach I have implemented plaintext preview. The idea is that you can define new own printer, which will transform and store the incoming printing data and which provides additional own function like getPlaintext().

Example:

file plaintext-printer-profile.ts

export class PlaintextProfile extends Profile {

  feed(lines: number): Promise<void> {
    return this.connection.write(Buffer.from(
      '\n'.repeat(lines)
    ));
  }

  async write(text: string, styles: number): Promise<void> { 
    await this.connection.write(Buffer.from(text, 'utf-8'));
  }

  // ... other code
}

export const pos58CapabilityMock: Capability = {
  columns: 32,
  model: 'mock',
  codepage: 'utf-8',
  codepages: [
    { code: 'utf-8', command: '' },
  ],
  fonts: [
    {
      name: 'Font A',
      columns: 32,
    },
  ],
  brand: '',
  initialize: '',
  profile: 'Epson',
};

And the usage

import { Printer, InMemory, Model } from 'escpos-buffer';
import { PlaintextProfile, pos58CapabilityMock } from './plaintext-printer-profile';

/**
 * Generates `outData` in plaintext.
 * It means without any escape sequences commonly used in case of POS printer.
 */
async function getPreviewPrinter(): Promise<{ printerFormatter: Printer, outData: InMemory }> {
  const connection = new InMemory();
  const plaintextProfile = new PlaintextProfile(pos58CapabilityMock);
  const plaintextModel = new Model(plaintextProfile);
  const printer = await Printer.CONNECT(plaintextModel, connection);
  return { printerFormatter: printer, outData: connection };
}

const { printerFormatter, outData } = await getPreviewPrinter();
printerFormatter.write('Something to print ...');
printerFormatter.write('Another tex to print ...');
// Now, you can use the `printerFormatter` functions by the same approach as you are printing to the actual printer,
// because the interface is the same. So you can just switch in your code from the actual printer to this one.
// ...and you will get the printed content in any format as you will implement in `PlaintextProfile` class.

const printerPlainText = outData
  .buffer()
  .toString('utf8');

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

No branches or pull requests

2 participants