# Node.js examples — `posprintUrl()`

See [docs/POSPRINT-URL.md](../../docs/POSPRINT-URL.md) for URL format and size limits.

```js
const { posprintUrl } = require('./posprint_url');
// or: import { posprintUrl } from './posprint_url.js';  // with "type": "module"
```

## From an object

Build the `.pos` document in JavaScript:

```js
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:

```js
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:

```js
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:

```js
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.
