Skip to content

Commit 450874b

Browse files
committed
Initial Commit
0 parents  commit 450874b

16 files changed

+1166
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.vscode/
2+
build/
3+
node_modules/
4+
wiki/
5+
.env
6+
.idea/

README.md

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
&nbsp;&nbsp;&nbsp;&nbsp;return an array with all installed printers
47+
48+
### `defaultPrinterName()`
49+
&nbsp;&nbsp;&nbsp;&nbsp;return the default printer name
50+
51+
### `setPrinter(printer)`
52+
&nbsp;&nbsp;&nbsp;&nbsp;set the printer to work on
53+
54+
55+
### `getCurrentPrinter()`
56+
&nbsp;&nbsp;&nbsp;&nbsp;get current printer you are working on
57+
58+
### `printerInfo(printer)`
59+
60+
&nbsp;&nbsp;&nbsp;&nbsp;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+
&nbsp;&nbsp;&nbsp;&nbsp;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+
```

binding.gyp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "unix_printer",
5+
"sources": ["./lib/unix/methods.cpp", "./lib/unix/API.cpp"],
6+
'cflags': ['-fexceptions'],
7+
'cflags_cc': ['-fexceptions'],
8+
"conditions": [
9+
['OS=="mac"', {
10+
'xcode_settings': {
11+
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES'
12+
}
13+
}],
14+
['OS!="win"', {
15+
'cflags':[
16+
'<!(cups-config --cflags)'
17+
],
18+
'ldflags':[
19+
'<!(cups-config --libs)'
20+
#'-lcups -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lz -lpthread -lm -lcrypt -lz'
21+
],
22+
'libraries':[
23+
'<!(cups-config --libs)'
24+
#'-lcups -lgssapi_krb5 -lkrb5 -lk5crypto -lcom_err -lz -lpthread -lm -lcrypt -lz'
25+
],
26+
'link_settings': {
27+
'libraries': [
28+
'<!(cups-config --libs)'
29+
]
30+
}
31+
}]
32+
]
33+
}
34+
]
35+
}

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if(process.platform == 'win32')
2+
module.exports = new (require('./src/windows_printer'))();
3+
else
4+
module.exports = new (require('./src/unix_printer'))();

install/install.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const win = require('./install_win.js');
2+
const unix = require('./install_unix.js');
3+
4+
var platform = process.platform;
5+
6+
process.on('exit', code => {
7+
if (code === 1) {
8+
console.log("Installation aborted.\n\n");
9+
}
10+
});
11+
12+
if(platform === 'win32'){
13+
win();
14+
}
15+
else{
16+
unix();
17+
}

install/install_unix.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const { spawnSync } = require('child_process');
2+
const { spawn } = require('child_process');
3+
4+
module.exports = function(){
5+
var cmd,
6+
output,
7+
platform = process.platform;
8+
9+
if(platform != 'darwin'){
10+
cmd = spawnSync('dpkg', ['-l']);
11+
output = cmd.stdout.toString('utf-8');
12+
13+
if(cmd.stderr.toString('utf-8')){
14+
console.log("Error: ");
15+
console.log(cmd.stderr.toString('utf-8'));
16+
process.exit(1);
17+
}
18+
19+
if(output.includes('libcups2-dev')){
20+
console.log('libcups2-dev found!');
21+
}
22+
else{
23+
console.error('libcups2-dev is required but was not found');
24+
process.exit(1);
25+
}
26+
27+
}
28+
29+
cmd = spawnSync('npm', ['run', 'build']);
30+
31+
if(output = cmd.stderr.toString('utf-8')){
32+
console.log(output);
33+
process.exit(0);
34+
}
35+
36+
console.log(cmd.stdout.toString('utf-8'));
37+
};

install/install_win.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports=function(){console.log('You cannot use this module on windows.')}

0 commit comments

Comments
 (0)