BlueGit

timfa posprint

Files

posprint / lib / php / examples.md

examples.md

examples.md 1.4 KiB Download Raw

PHP examples — posprint_url()

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

require __DIR__ . '/posprint_url.php';

From an array

Build the .pos document in PHP and pass an associative array:

$url = posprint_url([
    'version' => 1,
    'metadata' => [
        'title' => 'Barcode demo',
        'page_width_mm' => 53.0,
        'chars_per_line' => 32,
    ],
    'body' => "@barcode RASTER CODE128 \${data}\nHello\n@cut\n",
]);

echo $url, "\n";
// posprint://print?v=1&d=…

Use in HTML:

<a href="<?= htmlspecialchars($url, ENT_QUOTES, 'UTF-8') ?>">Print receipt</a>

From a local .pos file

Read the file as JSON text and pass the string (same encoding path as an array):

$path = __DIR__ . '/../../examples/url-demo.pos';
$json = file_get_contents($path);
if ($json === false) {
    throw new RuntimeException("Could not read {$path}");
}

// Optional: strip UTF-8 BOM if present (Windows editors)
$json = preg_replace('/^\xEF\xBB\xBF/', '', $json) ?? $json;

$url = posprint_url($json);
echo $url, "\n";

Or decode first to validate, then encode:

$doc = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
$url = posprint_url($doc);

Length warning

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

Source
1# PHP examples — `posprint_url()`
2
3See [docs/POSPRINT-URL.md](../../docs/POSPRINT-URL.md) for URL format and size limits.
4
5```php
6require __DIR__ . '/posprint_url.php';
7```
8
9## From an array
10
11Build the `.pos` document in PHP and pass an associative array:
12
13```php
14$url = posprint_url([
15 'version' => 1,
16 'metadata' => [
17 'title' => 'Barcode demo',
18 'page_width_mm' => 53.0,
19 'chars_per_line' => 32,
20 ],
21 'body' => "@barcode RASTER CODE128 \${data}\nHello\n@cut\n",
22]);
23
24echo $url, "\n";
25// posprint://print?v=1&d=…
26```
27
28Use in HTML:
29
30```php
31<a href="<?= htmlspecialchars($url, ENT_QUOTES, 'UTF-8') ?>">Print receipt</a>
32```
33
34## From a local `.pos` file
35
36Read the file as JSON text and pass the string (same encoding path as an array):
37
38```php
39$path = __DIR__ . '/../../examples/url-demo.pos';
40$json = file_get_contents($path);
41if ($json === false) {
42 throw new RuntimeException("Could not read {$path}");
43}
44
45// Optional: strip UTF-8 BOM if present (Windows editors)
46$json = preg_replace('/^\xEF\xBB\xBF/', '', $json) ?? $json;
47
48$url = posprint_url($json);
49echo $url, "\n";
50```
51
52Or decode first to validate, then encode:
53
54```php
55$doc = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
56$url = posprint_url($doc);
57```
58
59## Length warning
60
61On Windows, keep the full URL under ~7000 characters so the OS can launch posprint. For large templates, keep images as `@image https://…` in the body instead of embedding bytes in JSON.