| 1 | <?php
|
| 2 | /**
|
| 3 | * Build a posprint:// URL from a .pos document (array or JSON string).
|
| 4 | *
|
| 5 | * Encoding: UTF-8 JSON → raw deflate (gzdeflate) → base64url (no padding).
|
| 6 | *
|
| 7 | * @param array|string $pos PosFile as associative array or JSON string
|
| 8 | * @return string posprint://print?v=1&d=…
|
| 9 | * @throws InvalidArgumentException
|
| 10 | */
|
| 11 | function posprint_url($pos): string
|
| 12 | {
|
| 13 | if (is_array($pos)) {
|
| 14 | $json = json_encode($pos, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
| 15 | if ($json === false) {
|
| 16 | throw new InvalidArgumentException('json_encode failed: ' . json_last_error_msg());
|
| 17 | }
|
| 18 | } elseif (is_string($pos)) {
|
| 19 | $json = $pos;
|
| 20 | } else {
|
| 21 | throw new InvalidArgumentException('pos must be array or JSON string');
|
| 22 | }
|
| 23 |
|
| 24 | $compressed = gzdeflate($json, 9);
|
| 25 | if ($compressed === false) {
|
| 26 | throw new InvalidArgumentException('gzdeflate failed');
|
| 27 | }
|
| 28 |
|
| 29 | $b64 = rtrim(strtr(base64_encode($compressed), '+/', '-_'), '=');
|
| 30 | return 'posprint://print?v=1&d=' . $b64;
|
| 31 | }
|