BlueGit

timfa posprint

Files

posprint / lib / browser / examples.md

examples.md

examples.md 2.3 KiB Download Raw

Browser examples — posprintUrl()

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

Browsers cannot read arbitrary paths on disk. “Local file” here means a user-selected .pos file via <input type="file">, or JSON fetched from your server.

Requires CompressionStream('deflate-raw') (recent Chromium, Firefox, Safari).

<script type="module">
  import { posprintUrl } from './posprint_url.js';
</script>

From an object

Build the document in JavaScript and open the link:

<button id="print">Print</button>
<script type="module">
  import { posprintUrl } from './posprint_url.js';

  document.getElementById('print').addEventListener('click', async () => {
    const url = await posprintUrl({
      version: 1,
      metadata: {
        title: 'Barcode demo',
        page_width_mm: 53,
        chars_per_line: 32,
      },
      body: '@barcode RASTER CODE128 ${data}\nHello\n@cut\n',
    });
    // Auto-print (browser protocol prompt is the consent gate):
    // const url = await posprintUrl(pos, { noconfirm: true });
    window.location.href = url;
  });
</script>

Or render a link without navigating immediately:

const url = await posprintUrl({ /* … */ });
const a = document.createElement('a');
a.href = url;
a.textContent = 'Print receipt';
document.body.append(a);

From a local .pos file (user picks file)

<input type="file" id="pos-file" accept=".pos,application/json" />
<a id="print-link" hidden>Print</a>

<script type="module">
  import { posprintUrl } from './posprint_url.js';

  document.getElementById('pos-file').addEventListener('change', async (ev) => {
    const file = ev.target.files?.[0];
    if (!file) return;

    const json = await file.text();
    const url = await posprintUrl(json);

    const link = document.getElementById('print-link');
    link.href = url;
    link.hidden = false;
  });
</script>

From a file on your server

If the .pos lives on the same site, fetch it and pass the JSON string:

const res = await fetch('/templates/receipt.pos');
const json = await res.text();
const url = await posprintUrl(json);
window.location.href = url;

Length warning

On Windows, keep the full URL under ~7000 characters so the OS can launch posprint. Prefer short templates and @image https://… for logos.

Source
1# Browser examples — `posprintUrl()`
2
3See [docs/POSPRINT-URL.md](../../docs/POSPRINT-URL.md) for URL format and size limits.
4
5Browsers cannot read arbitrary paths on disk. “Local file” here means a user-selected `.pos` file via `<input type="file">`, or JSON fetched from your server.
6
7Requires `CompressionStream('deflate-raw')` (recent Chromium, Firefox, Safari).
8
9```html
10<script type="module">
11 import { posprintUrl } from './posprint_url.js';
12</script>
13```
14
15## From an object
16
17Build the document in JavaScript and open the link:
18
19```html
20<button id="print">Print</button>
21<script type="module">
22 import { posprintUrl } from './posprint_url.js';
23
24 document.getElementById('print').addEventListener('click', async () => {
25 const url = await posprintUrl({
26 version: 1,
27 metadata: {
28 title: 'Barcode demo',
29 page_width_mm: 53,
30 chars_per_line: 32,
31 },
32 body: '@barcode RASTER CODE128 ${data}\nHello\n@cut\n',
33 });
34 // Auto-print (browser protocol prompt is the consent gate):
35 // const url = await posprintUrl(pos, { noconfirm: true });
36 window.location.href = url;
37 });
38</script>
39```
40
41Or render a link without navigating immediately:
42
43```js
44const url = await posprintUrl({ /* … */ });
45const a = document.createElement('a');
46a.href = url;
47a.textContent = 'Print receipt';
48document.body.append(a);
49```
50
51## From a local `.pos` file (user picks file)
52
53```html
54<input type="file" id="pos-file" accept=".pos,application/json" />
55<a id="print-link" hidden>Print</a>
56
57<script type="module">
58 import { posprintUrl } from './posprint_url.js';
59
60 document.getElementById('pos-file').addEventListener('change', async (ev) => {
61 const file = ev.target.files?.[0];
62 if (!file) return;
63
64 const json = await file.text();
65 const url = await posprintUrl(json);
66
67 const link = document.getElementById('print-link');
68 link.href = url;
69 link.hidden = false;
70 });
71</script>
72```
73
74## From a file on your server
75
76If the `.pos` lives on the same site, fetch it and pass the JSON string:
77
78```js
79const res = await fetch('/templates/receipt.pos');
80const json = await res.text();
81const url = await posprintUrl(json);
82window.location.href = url;
83```
84
85## Length warning
86
87On Windows, keep the full URL under ~7000 characters so the OS can launch posprint. Prefer short templates and `@image https://…` for logos.