BlueGit

timfa posprint

Files

posprint / lib / browser / examples.md

examples.md

examples.md 2.4 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',
    });
    // Silent print after verify URL returns 200 (and domain trust):
    // const url = await posprintUrl(pos, {
    //   noconfirm: true,
    //   referrer: `https://${location.host}/api/posprint/verify?code=${code}`,
    // });
    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 // Silent print after verify URL returns 200 (and domain trust):
35 // const url = await posprintUrl(pos, {
36 // noconfirm: true,
37 // referrer: `https://${location.host}/api/posprint/verify?code=${code}`,
38 // });
39 window.location.href = url;
40 });
41</script>
42```
43
44Or render a link without navigating immediately:
45
46```js
47const url = await posprintUrl({ /* … */ });
48const a = document.createElement('a');
49a.href = url;
50a.textContent = 'Print receipt';
51document.body.append(a);
52```
53
54## From a local `.pos` file (user picks file)
55
56```html
57<input type="file" id="pos-file" accept=".pos,application/json" />
58<a id="print-link" hidden>Print</a>
59
60<script type="module">
61 import { posprintUrl } from './posprint_url.js';
62
63 document.getElementById('pos-file').addEventListener('change', async (ev) => {
64 const file = ev.target.files?.[0];
65 if (!file) return;
66
67 const json = await file.text();
68 const url = await posprintUrl(json);
69
70 const link = document.getElementById('print-link');
71 link.href = url;
72 link.hidden = false;
73 });
74</script>
75```
76
77## From a file on your server
78
79If the `.pos` lives on the same site, fetch it and pass the JSON string:
80
81```js
82const res = await fetch('/templates/receipt.pos');
83const json = await res.text();
84const url = await posprintUrl(json);
85window.location.href = url;
86```
87
88## Length warning
89
90On Windows, keep the full URL under ~7000 characters so the OS can launch posprint. Prefer short templates and `@image https://…` for logos.