escpos-php icon indicating copy to clipboard operation
escpos-php copied to clipboard

Can't print with my printer code PC862

Open liammizrahi opened this issue 6 years ago • 2 comments

Hi. My printer input must be CP862 But I can't resolve how to print with this format, I tried everything. First I tried to change the encoding of my Hebrew text to CP862 with iconv and it gave my an Error that said I must use UTF-8. then I tried to figure how to change the encoding but I didn't find how. I ran the character-tables.php file and it shows me Table 36 - CP862 WhatsApp Image 2019-12-10 at 16 30 10 But the Hebrew words I added to textRaw just became gibberish. If I print this file: צילום מסך 2019-12-10 ב-21 08 00 Manually with the cmd on windows (print /D:\%COMPUTERNAME\SHARENAME HEBREW.TXT) It does success and give me my Hebrew output WhatsApp Image 2019-12-10 at 16 36 16

I tried the function of:

$profile = CapabilityProfile::load("default");
    $printer -> selectCharacterTable(36);

36- My output CP862 and it still don't work. Give me gibberish In CP862, the letter א (First letter in Hebrew alphabet) is 128, when I try to print $print -> text(chr(128).'\n'); It gives me an error that I must use UTF-8 There's an option to print directly with CP862 or did I do something wrong or there's any solution? Thank a lot

liammizrahi avatar Dec 10 '19 19:12 liammizrahi

Hi @liammizrahi, good job debugging this so far.

I'm working on documenting this better on the wiki at the moment, because some of the information in the issue tracker is a bit outdated.

Basically, if your printer has the glyphs, and the code pages are correctly listed in the escpos-printer-db, then you can use the glyphs as UTF-8.

<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\CapabilityProfile;
use Mike42\Escpos\Printer;
$profile = CapabilityProfile::load("default");
$connector = new FilePrintConnector("/dev/usb/lp0");
$printer = new Printer($connector, $profile);
$printer -> text("שלום\n");
$printer -> cut();
$printer -> close();

I have a TM-T20II, which matches the default profile, so this all works. Printers generally work left-to-right only, and our humble library does not implement the Unicode bi-directional text layout algorithm, so the text comes out backwards (Sigh!).

2019-12-escpos-hebrew

Now if all the characters you need are in CP862, then you can bypass this feature and do your own encoding. textRaw() is the method you need to use, and you might as well fix the character ordering at the same time.

An example which works for me is:

<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\CapabilityProfile;
use Mike42\Escpos\Printer;
$profile = CapabilityProfile::load("default");
$connector = new FilePrintConnector("/dev/usb/lp0");
$printer = new Printer($connector, $profile);
// I used selectCharacterTable(36) for the TM-T20, but I think you can comment it out.
// In general, the correct value for a given code page is located in a programming manual for each model of printer. 
$printer -> selectCharacterTable(36);
$printer -> textRaw(fromHebrew("שלום") . "\n");
$printer -> cut();
$printer -> close();

function fromHebrew($utf8) {
    $cp862 = iconv("UTF-8", "CP862//IGNORE", $utf8);
    return strrev($cp862); // One byte per glyph, full right-to-left lines only!
}

2019-12-escpos-hebrew-2

Best of luck, please let me know how if it works!

mike42 avatar Dec 11 '19 10:12 mike42

Hi @liammizrahi, good job debugging this so far.

I'm working on documenting this better on the wiki at the moment, because some of the information in the issue tracker is a bit outdated.

Basically, if your printer has the glyphs, and the code pages are correctly listed in the escpos-printer-db, then you can use the glyphs as UTF-8.

<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\CapabilityProfile;
use Mike42\Escpos\Printer;
$profile = CapabilityProfile::load("default");
$connector = new FilePrintConnector("/dev/usb/lp0");
$printer = new Printer($connector, $profile);
$printer -> text("שלום\n");
$printer -> cut();
$printer -> close();

I have a TM-T20II, which matches the default profile, so this all works. Printers generally work left-to-right only, and our humble library does not implement the Unicode bi-directional text layout algorithm, so the text comes out backwards (Sigh!).

2019-12-escpos-hebrew

Now if all the characters you need are in CP862, then you can bypass this feature and do your own encoding. textRaw() is the method you need to use, and you might as well fix the character ordering at the same time.

An example which works for me is:

<?php
/* Call this file 'hello-world.php' */
require __DIR__ . '/vendor/autoload.php';
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\CapabilityProfile;
use Mike42\Escpos\Printer;
$profile = CapabilityProfile::load("default");
$connector = new FilePrintConnector("/dev/usb/lp0");
$printer = new Printer($connector, $profile);
// I used selectCharacterTable(36) for the TM-T20, but I think you can comment it out.
// In general, the correct value for a given code page is located in a programming manual for each model of printer. 
$printer -> selectCharacterTable(36);
$printer -> textRaw(fromHebrew("שלום") . "\n");
$printer -> cut();
$printer -> close();

function fromHebrew($utf8) {
    $cp862 = iconv("UTF-8", "CP862//IGNORE", $utf8);
    return strrev($cp862); // One byte per glyph, full right-to-left lines only!
}

2019-12-escpos-hebrew-2

Best of luck, please let me know how if it works!

THANK YOU SO MUCH!! I'll try it now...

liammizrahi avatar Dec 11 '19 18:12 liammizrahi