Saturday 13 May 2023

How to Print HTML on Bluethooth Thermal Printer

In am Trying to Print the follow HTML in a Bluethooth GoldenSky Printer. I am using the Web Bluethooth Api Plugin to select the device, and i am getting the HtmlContent with a fetch method in javascript. But i cant do this, or only PLAIN HTML. I didnt found the solution,

enter image description here

Here is my code:

fetch('/pedidos/dinamic/cupom?idPedido=5&acao=agrupado-bluethooth')
            .then(function (response) {
                return response.text();
            })
            .then(function (htmlContent) {
                console.log(htmlContent);

                var DataToPrint = htmlContent;

                const PRINTER_SERVICE_UUID = '000018f0-0000-1000-8000-00805f9b34fb';
                // Request access to Bluetooth devices

                var ADDRESS = '02:32:46:F9:36:F8';
                var SERVICE = '000018f0-0000-1000-8000-00805f9b34fb';
                var WRITE = '00002af1-0000-1000-8000-00805f9b34fb';
                // feed paper

                var deviceHandle;
                navigator.bluetooth.requestDevice({filters: [{services: [SERVICE]}]}).then(device => {
                    console.log(device);
                    deviceHandle = device;
                    return device.gatt.connect()
                }).then(server => {
                    console.log(server);
                    return server.getPrimaryService(SERVICE);
                }).then(service => {
                    console.log(service);
                    return service.getCharacteristic(WRITE);
                }).then(channel => {
                    console.log(channel);
                    var encoder = new TextEncoder('utf-8');
                    return channel.writeValue(encoder.encode(DataToPrint));
                }).catch(error => {
                    console.error(error)
                }).finally(() => {
                    deviceHandle.gatt.disconnect();
                });
            })
            .catch(function (error) {
                console.error('Error fetching HTML:', error);
            });
  • I already try to use jsPdf to get from html and output with datauristring or arraybuffer, but no success. And i try to with qz-tray but i cant connect to the printer, because the device is not installed like a printer, just a simple bluethooth device. So i need to do this or a builder to create a new template for this. If has a solution in PHP can be hekpfull... render html, convert and return this to javascript to send to bluethooth device.


from How to Print HTML on Bluethooth Thermal Printer

No comments:

Post a Comment