Skip to content
Advertisement

output printer ESC codes from twig in Symfony

I have a system that uses Symfony, and is connected to a (citizen ct-s2000) POS printer.

What I currently do is render the string to send to the printer, using the twig service:

$this->templatingService->render('SamPosBundle:Ticket:template1.html.twig', array('order' => $order))

and send this to the printer using fwrite, after that I feed the paper 1 line and cut the paper using:

fwrite($handle, chr(hexdec('0A')));
fwrite($handle, chr(hexdec('1B')).chr(hexdec('69')));

This all works like a charm, however.

Now I am looking for a way to send the escape codes from WITHIN the twig template

so that I can use the codes to underline etc., and finally cut the paper, from inside the twig template.

I’m sure this would involve a twig extension to create an additional filter, which I know how to make, I just don’t know WHAT EXACTLY it should do or how to go about the conversion from twig to escape code which would be picked up by fwrite

I’ve been looking for 2 days, and I really can’t seem to figure this out on my own, so any help would be greatly appreciated.

Advertisement

Answer

Its alive, just the syntax for the getFilters() was slightly different:

public function getGlobals()
{
    return [
        'ticket_cut' => chr(hexdec('1B')).chr(hexdec('69'))
    ];
}




public function getFilters()
{
    return array(
        new Twig_SimpleFilter('ticketBold', array($this, 'ticketBold')),
    );
}



public function ticketBold($string)
{

   return chr(hexdec('1B')).chr(hexdec('45'))."1".$string.chr(hexdec('1B')).chr(hexdec('45'))."0";

}

Thx for pointing me in the right direction!

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement