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 | |
| 3 | See [docs/POSPRINT-URL.md](../../docs/POSPRINT-URL.md) for URL format and size limits. |
| 4 | |
| 5 | ```js |
| 6 | const { posprintUrl } = require('./posprint_url'); |
| 7 | // or: import { posprintUrl } from './posprint_url.js'; // with "type": "module" |
| 8 | ``` |
| 9 | |
| 10 | ## From an object |
| 11 | |
| 12 | Build the `.pos` document in JavaScript: |
| 13 | |
| 14 | ```js |
| 15 | const 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 | |
| 25 | console.log(url); |
| 26 | // posprint://print?v=1&d=… |
| 27 | ``` |
| 28 | |
| 29 | Express route example: |
| 30 | |
| 31 | ```js |
| 32 | app.get('/print-link', (req, res) => { |
| 33 | const url = posprintUrl({ /* … */ }); |
| 34 | res.redirect(url); |
| 35 | }); |
| 36 | ``` |
| 37 | |
| 38 | ## From a local `.pos` file |
| 39 | |
| 40 | Read the file and pass the JSON string: |
| 41 | |
| 42 | ```js |
| 43 | const fs = require('fs'); |
| 44 | const path = require('path'); |
| 45 | |
| 46 | const filePath = path.join(__dirname, '../../examples/url-demo.pos'); |
| 47 | let json = fs.readFileSync(filePath, 'utf8'); |
| 48 | |
| 49 | // Optional: strip UTF-8 BOM |
| 50 | if (json.charCodeAt(0) === 0xfeff) { |
| 51 | json = json.slice(1); |
| 52 | } |
| 53 | |
| 54 | const url = posprintUrl(json); |
| 55 | console.log(url); |
| 56 | ``` |
| 57 | |
| 58 | Or parse and validate first: |
| 59 | |
| 60 | ```js |
| 61 | const doc = JSON.parse(json); |
| 62 | const url = posprintUrl(doc); |
| 63 | ``` |
| 64 | |
| 65 | ## Length warning |
| 66 | |
| 67 | 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. |