mike42 / escpos-php

PHP library for printing to ESC/POS-compatible thermal and impact printers
Other
2.57k stars 861 forks source link

Working with toRasterFormat() #681

Closed alanleyva closed 5 years ago

alanleyva commented 5 years ago

Hi Mike.

I manage to make a working connection with the Chrome Bluetooth devices on the Web feature to a Bluetooth Printer.

I'm trying to print the company logo on the recipt using your libray. I do not need to connect to the printer but just to create the image to ESC/POS "ready" I try using the $img -> toRasterFormat() function but the output does not seem to work for me.

It certantly does something, but I'm not sure how to make it work. When I send that data to printer here is what it gets:

img_3919

What function do I need to use to generate the "code string" to use? My intention is to save this code string to use it any time it is needed.

Thanks!

mike42 commented 5 years ago

I don't believe your usage includes any actual print commands. I should be able to point you at the best examples to use if you can clarify the I/O and compatibility constraints that you're working with.

Would you mind sharing the model of printer you are using, and the PHP code you've come up with so far?

alanleyva commented 5 years ago

Great. I'm working with the cheapest printer I could find, trying to make it work so my clients don't have to spend too much. Here is the model. I could not find much information about it online but it works with the info I found on this page

And, as the code, I'm working with javascript. Here is a list of comand I can run on the printer and they work just fine.

  ESC = "\u001B";
  GS = "\u001D";
  Line = "\u000A";
  Tabs = "\u0044n4";
  Tab = "\u0009";
  Sp = "  ";
  PrintPage = "\u000C";
  alignLeft = "\u001B610";
  alignRigth = "\u001B612";
  alignCenter = "\u001B611";
  InitializePrinter = ESC + "40";
  BoldOn = ESC + "E" + "\u0001";
  BoldOff = ESC + "E" + "\0";
  DoubleOn = GS + "!" + "\u0011"; // 2x sized text (double-high + double-wide)
  DoubleOff = GS + "!" + "\0";

Hope it's enough to understand what I need. If you have a Bluetooth printer in hand I can give you access to my web app if you like.

Thanks!

mike42 commented 5 years ago

This project is all PHP. Here is an image printing example to start with. Try also bitImageColumnFormat, and change php://stdout to something like foo.bin to write to a file.

It's still not clear to me what type of binary data your printer is going to understand (does it have image printing commands at all? Does it know the ESC/POS image commands? which ones?), or how you are taking the output of this library and sending it to your printer without mangling it.

I think that these are basic pieces of information to start your troubleshooting. Contact your vendor if you need clarification on the supported commands, as trial and error can take a lot of paper :)

alanleyva commented 5 years ago

Hi Mike. Sorry for taking too long to respond, I've been stuck with a lot of changes lately

I'm back to trying to make the logo work on the tickets.

Okay so, the printers I'm using do accept ESC/POS image commands, here is the page of the manufacturer.

Now, what I'm trying to accomplish is to save all the logo 'code' inside a table row on my DB And send that to the printer via Javascript.

I'm pretty sure that what I need to use is:

$tux = EscposImage::load("resources/tux.png", false);
$printer -> bitImage($tux);

But instead of sending the print commando to the priner, I need it to be return, something like:

$image = $printer -> bitImage($tux);
// or
$image = $printer -> graphics($tux);

Then I can use that response to send it to the printer via Javascript.

Is it posisble?

alanleyva commented 5 years ago

Okay, I think we're getting closer. Using $tux->toColumnFormat() i got this response: screen shot 2019-03-04 at 17 44 05

And this is my logo .png foo_icon

mike42 commented 5 years ago

You are not quite on the right track, so here is some more detail. Just remember you are solving three unrelated problems:

Firstly, you really do need to call an image printing command on a Printer object to print an EscposImage, that is just how this API works. :)

Try printing to a file so that you have the binary data. You can then try printing this raw, serving it up (or use echo file_get_contents("test.bin")) to send that to the browser, or otherwise making sure that you can get these commands to your printer, and that they are understood.

As always, if you get gibberish, then eliminate variables as possible, because many things will modify binary output on the way to the printer if you let them.

<?php
require __DIR__ . '/vendor/autoload.php';

use Mike42\Escpos\EscposImage;
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;

$logo = EscposImage::load("logo.png");

$connector = new FilePrintConnector("test.bin");
$printer = new Printer($connector);
$printer->bitImageColumnFormat($logo);

$printer->cut();
$printer->close();

If I cat test.bin > /dev/usb/lp0, I then get this output:

image

That is your column format data, spread through some line-spacing and image-related commands, followed by a cut. Most receipt printers are pure black-and-white, and you will get better results if you convert your image manually to black and white.

Next, use a DummyPrintConnector so that you can avoid writing to a file. This is based on an example in the repository called 281-get-data.php.

<?php
require __DIR__ . '/vendor/autoload.php';

use Mike42\Escpos\EscposImage;
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\DummyPrintConnector;

$logo = EscposImage::load("logo.png");

$connector = new DummyPrintConnector();
$printer = new Printer($connector);
$printer->bitImageColumnFormat($logo);

$printer->cut();

// Get the data out as a string
$data = $connector -> getData();
$printer->close();

// Return it however you like
header('Content-type: application/octet-stream');
header('Content-Length: '.strlen($data));
echo $data;

You seem to know what you want to do with regards to transport, so the rest is up to you. You can remove cut() at the end, but the ESC @ (initialize) command will always be sent at the start of a receipt from this library, since we track the state of the printer between commands.