BlueGit

timfa posprint

Files

posprint / lib / node / examples.md

examples.md

examples.md 1.4 KiB Download Raw

Node.js examples — posprintUrl()

See docs/POSPRINT-URL.md for URL format and size limits.

const { posprintUrl } = require('./posprint_url');
// or: import { posprintUrl } from './posprint_url.js';  // with "type": "module"

From an object

Build the .pos document in JavaScript:

const url = posprintUrl({
  version: 1,
  metadata: {
    title: 'Barcode demo',
    page_width_mm: 53,
    chars_per_line: 32,
  },
  body: '@barcode RASTER CODE128 ${data}\nHello\n@cut\n',
});

console.log(url);
// posprint://print?v=1&d=…

Express route example:

app.get('/print-link', (req, res) => {
  const url = posprintUrl({ /* … */ });
  res.redirect(url);
});

From a local .pos file

Read the file and pass the JSON string:

const fs = require('fs');
const path = require('path');

const filePath = path.join(__dirname, '../../examples/url-demo.pos');
let json = fs.readFileSync(filePath, 'utf8');

// Optional: strip UTF-8 BOM
if (json.charCodeAt(0) === 0xfeff) {
  json = json.slice(1);
}

const url = posprintUrl(json);
console.log(url);

Or parse and validate first:

const doc = JSON.parse(json);
const url = posprintUrl(doc);

Length warning

On Windows, keep the full URL under ~7000 characters so the OS can launch posprint. For large templates, use @image https://… in the body instead of embedding image data.

Source
1# Node.js examples — `posprintUrl()`
2
3See [docs/POSPRINT-URL.md](../../docs/POSPRINT-URL.md) for URL format and size limits.
4
5```js
6const { posprintUrl } = require('./posprint_url');
7// or: import { posprintUrl } from './posprint_url.js'; // with "type": "module"
8```
9
10## From an object
11
12Build the `.pos` document in JavaScript:
13
14```js
15const url = posprintUrl({
16 version: 1,
17 metadata: {
18 title: 'Barcode demo',
19 page_width_mm: 53,
20 chars_per_line: 32,
21 },
22 body: '@barcode RASTER CODE128 ${data}\nHello\n@cut\n',
23});
24
25console.log(url);
26// posprint://print?v=1&d=…
27```
28
29Express route example:
30
31```js
32app.get('/print-link', (req, res) => {
33 const url = posprintUrl({ /* … */ });
34 res.redirect(url);
35});
36```
37
38## From a local `.pos` file
39
40Read the file and pass the JSON string:
41
42```js
43const fs = require('fs');
44const path = require('path');
45
46const filePath = path.join(__dirname, '../../examples/url-demo.pos');
47let json = fs.readFileSync(filePath, 'utf8');
48
49// Optional: strip UTF-8 BOM
50if (json.charCodeAt(0) === 0xfeff) {
51 json = json.slice(1);
52}
53
54const url = posprintUrl(json);
55console.log(url);
56```
57
58Or parse and validate first:
59
60```js
61const doc = JSON.parse(json);
62const url = posprintUrl(doc);
63```
64
65## Length warning
66
67On Windows, keep the full URL under ~7000 characters so the OS can launch posprint. For large templates, use `@image https://…` in the body instead of embedding image data.