https://github.com/artiebits/unix-print
A utility for Unix-like operating systems to print files from Node.js and Electron.
https://github.com/artiebits/unix-print
electron filesystem linux macos nodejs pdf pdf-printer png printer rollo-printer unix zebra-printer
Last synced: 7 months ago
JSON representation
A utility for Unix-like operating systems to print files from Node.js and Electron.
- Host: GitHub
- URL: https://github.com/artiebits/unix-print
- Owner: artiebits
- License: mit
- Created: 2021-09-17T06:51:45.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-11-24T07:45:52.000Z (about 2 years ago)
- Last Synced: 2025-07-25T18:57:53.485Z (7 months ago)
- Topics: electron, filesystem, linux, macos, nodejs, pdf, pdf-printer, png, printer, rollo-printer, unix, zebra-printer
- Language: TypeScript
- Homepage: https://artiebits.com
- Size: 443 KB
- Stars: 97
- Watchers: 4
- Forks: 20
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Unix-print
A utility for Unix-like operating systems to print files from Node.js and Electron.
- Understands different types of files, such as PDF, text, PostScript, and image files.
- Supports label printers such as Rollo and Zebra.
**Table of Contents**
- [Unix-print](#unix-print)
- [Basic Usage](#basic-usage)
- [Installation](#installation)
- [API](#api)
- [`print(pdf, printer, options) => Promise<{stdout, stderr}>`](#printpdf-printer-options--promisevoid)
- [`isPrintComplete(printResponse) => Promise`](#isprintcompleteprintresponse--promiseboolean)
- [`getPrinters() => Promise`](#getprinters--promiseprinter)
- [`getDefaultPrinter() => Promise`](#getdefaultprinter--promiseprinter--null)
- [License](#license)
## Basic Usage
Print a PDF file to the default printer:
```javascript
import { print } from "unix-print";
print("assets/file.pdf").then(console.log);
```
## Installation
Install using Yarn:
```bash
yarn add unix-print
```
Or using npm:
```bash
npm install --save unix-print
```
## API
### `print(pdf, printer, options) => Promise`
A function to print a file to a printer.
**Arguments**
| Argument | Type | Optional | Description |
| -------- | :--------: | -------- | ----------------------------------------------------------------------- |
| file | `string` | Required | A file to print. |
| printer | `string` | Optional | Print a file to the specific printer. |
| options | `string[]` | Optional | Any option from [this list](https://www.computerhope.com/unix/ulp.htm). |
**Returns**
`Promise<{stdout: string | null, stderr: string | null}>`.
To print a file to the default printer:
```javascript
import { print } from "unix-print";
print("assets/file.pdf").then(console.log);
```
To print a file to a specific printer:
```javascript
import { print } from "unix-print";
const fileToPrint = "assets/file.pdf";
const printer = "Zebra";
print(fileToPrint, printer).then(console.log);
```
With some printer-specific options:
```javascript
import { print } from "unix-print";
const fileToPrint = "assets/file.jpg";
const printer = undefined;
const options = ["-o landscape", "-o fit-to-page", "-o media=A4"];
print("assets/file.jpg", printer, options).then(console.log);
```
### `isPrintComplete(printResponse) => Promise`
**Arguments**
| Argument | Type | Optional | Description |
| ------------- | :---------------------------------------: | -------- | ------------------------------ |
| printResponse | {stdout: string | null} | Required | Resolved promise returned from [`print`](#printpdf-printer-options--promisevoid). |
**Returns**
`Promise`: False if the job is on the queue or `stdout` is null, true otherwise.
**Examples**
```javascript
import { isPrintComplete } from 'unix-print';
const fileToPrint = 'assets/file.pdf';
const printJob = await print(fileToPrint);
async function waitForPrintCompletion(printJob) {
while (!await isPrintComplete(printJob)) {
// Wait a bit before checking again (to avoid constant checks)
await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for 1 second
}
console.log('Job complete');
}
await waitForPrintCompletion(printJob);
```
### `getPrinters() => Promise`
**Returns**
`Promise`: List of available printers.
**Examples**
```javascript
import { getPrinters } from "unix-print";
getPrinters().then(console.log);
```
### `getDefaultPrinter() => Promise`
**Returns**
`Promise`: System default printer or `null`.
**Examples**
```javascript
import { getPrinters } from "unix-print";
getDefaultPrinter().then(console.log);
```
## License
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE) for details.