qzind / tray

Browser plugin for sending documents and raw commands to a printer or attached device.
https://qz.io
Other
848 stars 276 forks source link

Run as a Windows Service #116

Open tresf opened 8 years ago

tresf commented 8 years ago

Enhancement placeholder to run QZ Tray as a Windows Service, milestoned for the 2.2 release.

Note; A prerequisite to starting this task is Headless Support (#51) which is milestoned for the 2.1 release.

Possible utilities for creating a Windows Service:

cwg999 commented 7 years ago

In windows 7 I was able to create a service using nssm install qztray javaw -DtrustedRootCert=C:\cert\mycert.pem "-jar \"C:\Program Files\QZ Tray\qz-tray.jar\"" and editing the created C:\Windows\System32\config\systemprofile\AppData\Roaming\qz\allowed.dat file with the location I needed to allow.

Looking forward to a more straightforward solution in 2.2!

tresf commented 6 years ago

@cwg999 another client tested out your solution and shared the following with us...

  1. Make sure QZ Tray is downloaded and installed.
  2. Make sure you can already print with QZ Tray (click "Always allow" at least once)
  3. Download nssm, extract to e.g. the Desktop
  4. Open cmd as Administrator
  5. Navigate to the 64-bit version of nssm, e.g. cd "%USERPROFILE%\Desktop\nssm-2.24\win64\"
  6. Execute the command to install the service using nssm:
    REM set OPTS=-DtrustedRootCert="%USERPROFILE%\Desktop\override-cert.crt"
    nssm install qz-tray javaw "%OPTS% -jar """%PROGRAMFILES%\QZ Tray\qz-tray.jar""""
    (svc name)^       ^(program)         ^(arguments, escaped)
    REM FIXME Escape quotes for cmd
  7. Copy prefs.properties and allowed.dat from location: %appdata%\qz to C:\Windows\System32\config\systemprofile\AppData\Roaming\qz\
  8. (Re)Start the service via the service manager in Windows.
  9. Test the printing.
  10. Check the logs in C:\Windows\System32\config\systemprofile\AppData\Roaming\qz\ for a positive outcome. Note: Virtual printers that show pop-ups to save the document (e.g. PDFCreator) will not be shown; this won't work when QZ Tray is running as a service.
klabarge commented 4 years ago

Here's the steps I performed to get NSSM + QZ Tray working.

Environment

Steps

  1. Download and install QZ Tray

    • Note: QZ Tray 2.1 + is highly recommended for this. The 2.0 branch does not support headless operation.
  2. Launch the demo page(file:///C:/Program%20Files/QZ%20Tray/demo/sample.html), and select "Allow and Remember" to whitelist the certificate. This required because the dialogue prompts will not be displayed when running as a service.

    • ⚠️ Note: Only the initial connection will succeed. Signing will need to be setup in order to print.
  3. Download NSSM. Extract this to a sane, safe location. I used C:\nssm-2.24-101-g897c7ad

    • Note: There's a bug with Windows 10 + NSSM 2.24. Use the "featured pre-release version" nssm 2.24-101-g897c7ad if on Windows 10.
  4. Create a batch file for launching QZ Tray

    • NSSM does not support environment variables (they need to be manually added to the service). In order to not hard-code the Java location, I chose to create a batch file.
      • If you do not want to create a batch file, you will need to supply this to the If you do not wish to create a batch file, you will need to use full/path/to/java.exe -jar full/path/to/qz-tray.jar in the next step
    @ECHO OFF
    REM launch QZ Tray 2.0.x
    java -Xms512M -jar "%PROGRAMFILES%\QZ Tray\qz-tray.jar" %1
    REM launch QZ Tray 2.1 (headless support)
    REM java -Xms512M -jar "%PROGRAMFILES%\QZ Tray\qz-tray.jar" --headless %1
  5. Install the service

    REM change dir to NSSM 64-bit directory
    cd c:\nssm-2.24-101-g897c7ad\win64
    REM create a service called "qz-tray" that points to your batch file
    REM if you do not want a batch file, replace path of batch file with full/path/to/java.exe -jar full/path/to/qz-tray.jar
    nssm install qz-tray full\path\to\qztray-service.bat
  6. Copy %APPDATA%\qz\allowed.dat to C:\Windows\System32\config\systemprofile\AppData\Roaming\qz\allowed.dat

    copy /Y %APPDATA%\qz\allowed.dat C:\Windows\System32\config\systemprofile\AppData\Roaming\qz\allowed.dat
  7. Verify QZ Tray is not running on the desktop

  8. Open Services, find qz-tray, and start it

  9. Refresh the demo page. It should now be connected.

    • Remember that signing must be setup in order to proceed any farther.
tresf commented 4 years ago

@klabarge thanks, I've adapted these for our deployment section here: https://github.com/qzind/tray/wiki/Windows-Service