Is there any way to print from Google Chrome to thermal printer (escpos) in local network without using apps like QZ tray?

余生长醉 提交于 2020-01-06 04:59:46

问题


Are PWA functionality (service-worker) can help or there no way to do that?


回答1:


This document contains a sample program that prints easily using the socket interface in C language.
UB-E04 Technical Reference Guide

It seems that the equivalent can be implemented using JavaScript WebSocket.
This article is available in both Japanese and English, and both provide examples of using WebSocket easily from vanilla JavaScript.
5分で動かせるwebsocketのサンプル3つ / WebSocket Tutorials
Introducing WebSockets: Bringing Sockets to the Web

The following is a sample program in Linux C language described in the document.
Sending to a printer can be done with such a simple program.
The data to be sent must be created in the format described in the ESC/POS command reference.

/* TCP9100 programming sample for LINUX
 * HOW TO BUILD
 * cc ltcp.c
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
int main(int argc, char* argv[])
{
    int sock;
    struct sockaddr_in addr;
    if (argc != 2) {
        printf("usage: ltcp <ip address>\n");
        exit(1);
    }

    /* create socket */
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
        perror("socket()");
        exit(1);
    }

    /* initialize the parameter */
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(9100);
    addr.sin_addr.s_addr = inet_addr(argv[1]);

    /* connect */
    if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        perror("connect()");
    }
    printf("connected\n");

    /* send data */
    send(sock, "EPSON UB-E04\n", 13, 0);

    /* close socket */
    close(sock);
    return 0;
}


来源:https://stackoverflow.com/questions/59517689/is-there-any-way-to-print-from-google-chrome-to-thermal-printer-escpos-in-loca

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!