Coder Social home page Coder Social logo

lockerace / rawprintinghttpservice Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 4.0 29 KB

DEPRECATED (see below) A Windows service which serve HTTP protocol to send binary data from browser (javascript) to client local printer

License: MIT License

C# 100.00%
escpos escpos-printer nodejs javascript angular receipt-printer

rawprintinghttpservice's Introduction

DEPRECATED!

A windows service has its own limitation:

When using LocalSystem logon

  • all users local and network printers can be listed correctly
  • cannot print to network printers.

When using NetworkService logon

  • only local printers can be listed
  • can print to network printers

When using specific user logon

  • only local and network printers that are registered by the user can be listed
  • can print to network printers which the user has privilege
  • a lot of effort to change logon

CONCLUSION: I moved this project to system tray mode, which starts whenever an user sign in. So every users can have their own set of printers.

Raw Printing HTTP Service

A Windows service which serve HTTP protocol to send binary data from browser (javascript) to client local printer. It supports ESC/POS codes and works well with Generic/Text Only Driver. It supports all browser that has XHR implementation.

Configurations

Config file will be in the same directory you install the service named config.json. parameters :

  • allowedDomains: string[] => list of allowed origin domains (please include protocol & port)
  • port: int => port number to listen (default: 9100)

example :

{
    "allowedDomains": [
        "http://localhost:4200",
        "http://localhost:3000"
    ],
    "port": 9100
}

Paths :

  • GET / => view current status of the server and remove already allowed domains via GUI response :

    • type: html
  • GET /printers => list all printers available at current machine. HTTP request header origin must be within allowed domains.

    response :

    • type: json
    • values:
      • machineName: string => NetBIOS resolved computer name
      • printers: string[] => list of printer names installed locally
    • example : { "machineName" : "Somebody-PC", "printers" : ["Microsoft XPS Document Writer", "Fax"] }
  • GET /permissions => ask local user permission to allow requested domain to print

    query parameters :

    • h: string (required) => domain origin that will be allowed to print which include protocol & port (URI encoded). e.g.: http%3A%2F%2Flocalhost%3A4200

    response :

    • type: html
    • description: dialog with 2 buttons, allow and block
  • POST /permissions => add/remove allowed domain origin to print

    accept: json

    body parameters :

    • host: string (required) => domain origin that will be allowed to print which include protocol & port. e.g.: http://localhost:4200
    • status: string (required) => valid status values :
      • allow
      • remove

    response :

    • type: html
    • description: script to close allow origin dialog opened at GET /permission
  • POST / => send print job information to printer

    accept: json

    body parameters :

    • printer: string (required) => printer name
    • data: string (required) => base64 encoded binary data
    • id: string (required) => document id/print job name

    response :

    • type: json
    • values:
      • success: boolean => print job sent status
      • data: string => document id/print job name if success or error message if failed

    example : { "printer": "LX-300+", "data": "SGVsbG8gV29ybGQh", "id": "Test Document" }

Notes

  • For network printer, you must install it locally first (drivers & authentication)
  • You can use esc-pos-encoder to generate binary data (works with either Node.js or browser)

Usage

Angular + esc-pos-encoder:

@Component({
  selector: 'app-root',
  template: `
      <div *ngIf="message">{{message}}</div>
      <button (click)="print()">Print</button>
  `})
class AppComponent {
  message: string;
  constructor(private http: HttpClient) {}
  print() {
    // Generate the document to print
    let encoder = new EscPosEncoder();
    let result = encoder
      .initialize()
      .text('The quick brown fox jumps over the lazy dog')
      .newline()
      .encode();
    let docId = 'Test Document';
    
    // Convert UInt8Array to base64 string
    let buf = Buffer.from(result.buffer);
    let binData = buf.toString('base64');
    
    // Send print job to printer
    this.http.post('http://localhost:9100', {printer: 'LX-300', data: binData, id: docId})
      .subscribe((response) => {
        if (response.success) {
          this.message = 'Print job sent successfully';
        } else {
          this.message = response.data;
        }
      }, (err) => {
        // If failed, ask for print permission
        let winOpt = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=400,height=300,top=0,left=0';
        window.open('http://localhost:9100/permissions?h=http%3A%2F%2Flocalhost%3A4200', 'Ask Permission', winOpt);
      });
  }
}

Credits

Michael Davies

rawprintinghttpservice's People

Contributors

lockerace avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.