|
| 1 | +# node-native-printer |
| 2 | + |
| 3 | +A node module to natively print your files |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +* ### Windows: |
| 8 | + |
| 9 | + * **[edge](https://github.com/tjanczuk/edge)**, [electron-edge](https://github.com/kexplo/electron-edge) (depending on your environment) or any other fork of these. The package you choose **should be installed before** node-native-printer. During installation you'll be prompted to select which package do |
| 10 | + you want to use.<br>**BE CAREFULLY**: it will be listed all packages in `node_modules` containing the word "edge"; be sure to make the right choice |
| 11 | + |
| 12 | + If you wish, you can manually specify which fork of edge or electron-edge to use. See [installation](#installation) |
| 13 | + |
| 14 | +* ### Unix (Mac and Linux) |
| 15 | + |
| 16 | + * **[node-gyp](https://github.com/nodejs/node-gyp)** to build native modules |
| 17 | + * **[cups](https://www.cups.org/)** (Linux only). You need libcups2-dev and libcups2. The first contains source code needed to build native modules, the second contains command-line executables needed in order to print. **It may also need libcups2 in production**. See [API](#api) |
| 18 | + |
| 19 | +## Imporant notes |
| 20 | + |
| 21 | +Due to important differences in enviroments and ecosystems between Microsoft and Unix-based systems, this module has been written in C++ for Unix and C# for Windows. This can result in different behaviours during the execution of the methods (such as different execution time, returning values, allowed options for printer, etc.). |
| 22 | + |
| 23 | +*** |
| 24 | +## Installation |
| 25 | +``` |
| 26 | +npm install node-native-printer --save |
| 27 | +``` |
| 28 | + |
| 29 | +You will be prompted for selecting edge backend based on installed packages that contains the word "edge". If you wish to enter manually the name of edge backend package, select `Not listed` and type it manually. |
| 30 | + |
| 31 | +#### Flags (Windows only) |
| 32 | + * `-p` or `--production`: to be used when packaging with (i.e.) electron-builder. It will skip prompting for edge backend and will take correctly the dll path needed for windows.<br> |
| 33 | + **Note** that `-p` flag require that edge backend has been specified during installation. |
| 34 | + |
| 35 | +## Usage |
| 36 | +First of all you need to require the module inside your app this way: |
| 37 | +```javascript |
| 38 | +const printer = require('node-native-printer'); |
| 39 | +``` |
| 40 | + |
| 41 | +Then you can set a printer to work on, calling `setPrinter(printerName)`. In this way you don't need to pass else the printer name on each method it's required. |
| 42 | + |
| 43 | +## API |
| 44 | + |
| 45 | +### `listPrinters()` |
| 46 | + return an array with all installed printers |
| 47 | + |
| 48 | +### `defaultPrinterName()` |
| 49 | + return the default printer name |
| 50 | + |
| 51 | +### `setPrinter(printer)` |
| 52 | + set the printer to work on |
| 53 | + |
| 54 | + |
| 55 | +### `getCurrentPrinter()` |
| 56 | + get current printer you are working on |
| 57 | + |
| 58 | +### `printerInfo(printer)` |
| 59 | + |
| 60 | + return general info about current working printer such jobs and options: |
| 61 | + |
| 62 | +* **printer**: printer of which get informations |
| 63 | +* **returning value**: |
| 64 | + * **Windows**: return only jobs in printer queue |
| 65 | + * **Unix**: return jobs in printer queue and CUPS options. Theese last depends on printer |
| 66 | + |
| 67 | +### `printerOptions(printer)` |
| 68 | + return printer-specific options: |
| 69 | + * **printer**: name of the printer to print on. If not specified it will print on previously setted printer. If printer is not set it will print on default printer. |
| 70 | + * **Returning value**: |
| 71 | + * **Windows**: return an object containing main options for printer: |
| 72 | + |
| 73 | + ```json |
| 74 | + { |
| 75 | + "Collate": "array containing collation options", |
| 76 | + "Duplexing": "array containing collation options", |
| 77 | + "MaxCopy": "max number of copies you can send to printer", |
| 78 | + "SupportsColor": "boolean indicating whether a print can print with colors", |
| 79 | + "PaperSheets": "available paper formats supported from printer. If custom is present it can be submitted custom width and height", |
| 80 | + "Resolutions": "printer resolutions (i.e.: High, Medium)" |
| 81 | + } |
| 82 | + ``` |
| 83 | + |
| 84 | + * **Unix**: return an object containing printer-specific options and from PPD file. |
| 85 | + |
| 86 | +### `print(filename[, options, printer])` |
| 87 | + * **filename**: file to print |
| 88 | + * **options**: a JSON object containing options for printer: |
| 89 | + * **Windows**: default options: |
| 90 | + ```json |
| 91 | + { |
| 92 | + "collate": true, |
| 93 | + "color": true, |
| 94 | + "copies": 1, |
| 95 | + "duplex": "Default", |
| 96 | + "landscape": false, |
| 97 | + "paperSize": "", |
| 98 | + "fromPage": 0, |
| 99 | + "toPage": 0 |
| 100 | + } |
| 101 | + ``` |
| 102 | + |
| 103 | + List of supported extensions can be found [here](https://github.com/MatteoMeil/node-native-printer/blob/master/supported-extensions.md) |
| 104 | + |
| 105 | + **Notes:** duplex is case sensitive, so be careful to write correctly. `"paperSize"` refers to size of sheet to print on; if you want to print on a paper with custom dimensions, pass `"Custom.WidthxHeight"` where Width and Height are **integer** dimensions in hundredths of inch. `"fromPage": 0` means document will be printed from first page; `"toPage": 0` means document will be printed from `"fromPage"` to last page. |
| 106 | + |
| 107 | + * **Unix**: you can use [command-line options](https://www.cups.org/doc/options.html) in JSON-style and/or printer-specific options retrieved from `printerOptions()`; i.e.: |
| 108 | + |
| 109 | + ```json |
| 110 | + { |
| 111 | + "landscape": true, |
| 112 | + "n": 2, |
| 113 | + "sides": "two-sided-long-edge" |
| 114 | + } |
| 115 | + ``` |
| 116 | + |
| 117 | + For options that doesn't have a value (like `landscape` or `fit-to-page`) you can assign a boolean (see above) |
| 118 | + |
| 119 | + It will be generated and executed a command like `lp -d printerName /path/to/filename -o landscape -n 2 -o sides=two-sided-long-edge` |
| 120 | + * **Returning value (only for Unix)**: job id of work sent to printer |
| 121 | + |
| 122 | +### `printText(text[, options, printer])` |
| 123 | + |
| 124 | +Same as `print()` but you can pass directly a string of text on the first parameter. |
| 125 | + |
| 126 | +## Electron Packaging (Windows only) |
| 127 | +As from [electron's docs](https://electronjs.org/docs), there are two methods of packaging your app: with one of the [builders](https://electronjs.org/docs/tutorial/application-distribution) or manually making your own an [asar](https://github.com/electron/asar) archive. |
| 128 | + |
| 129 | + * **Builders** (tested with [electron-builder](https://github.com/electron-userland/electron-builder)): it's enough to add flag `-p` or `--production`.<br> |
| 130 | + **Example** with electron-builder: |
| 131 | + ``` |
| 132 | + "build": { |
| 133 | + "npmArgs": "--production" |
| 134 | + } |
| 135 | + ``` |
| 136 | + * **Manually packing**: you'll need to copy all `lib/windows` folder, under `<app-root>/resources/app.asar.unpacked/node_modules/node-native-printer/lib/windows`.<br> |
| 137 | + The tree will look like: |
| 138 | + ``` |
| 139 | + /app folder |
| 140 | + +-- ... |
| 141 | + +-- resources/ |
| 142 | + +-- app.asar |
| 143 | + +-- electron.asar |
| 144 | + +-- app.asar.unpacked/ |
| 145 | + +-- node_modules/ |
| 146 | + +-- node-native-printer/ |
| 147 | + +-- lib/ |
| 148 | + +-- windows/ |
| 149 | + +-- nl/ |
| 150 | + +-- x64/ |
| 151 | + +-- x86/ |
| 152 | + +-- PdfiumViewer.dll |
| 153 | + +-- PdfiumViewer.xml |
| 154 | + +-- windows_printer.dll |
| 155 | + ``` |
0 commit comments