Karyo
Documentation/ Integration and extension

Documents and printing

Karyo 2.1.1rendered at build time
On this page

Every artefact Karyo renders for the world outside it: eleven templates, eleven download routes, one archive, one per-owner override seam, and one printer.

1. One renderer, two output families

libs/karyo-documents holds the whole rendering stack: DocumentRenderer (Qute + openhtmltopdf), DocumentStore (the archive SPI), DocumentTemplateProvider (the override SPI) and CsvWriter. Four domain modules render through it - inventory, orders, layout and fulfillment.

DocumentRenderer builds its own Qute Engine rather than injecting the application's, "so escaping behaviour does not depend on the app-wide engine config" (DocumentRenderer). Treatment is keyed on the template path's suffix alone:

  • .html is parsed with the TEXT_HTML variant, so the registered HtmlEscaper escapes every interpolated value, then converted to PDF by htmlToPdf, which installs a no-op FSUriResolver that blocks all sub-resource fetches - the templates use inline CSS and reference no external images or fonts, so refusing every URI neutralises SSRF from injected markup (DocumentRenderer.htmlToPdf).
  • .zpl is parsed as text and its bindings pre-sanitised: ^ and ~, ZPL's two control characters, are stripped from string values and from one level of nested map values (DocumentRenderer.sanitizeZpl, DocumentRenderer.stripZpl). The KDoc is explicit that List values are not descended into, and that a future ZPL template binding a list of strings must sanitise them itself.

Because the treatment is keyed on the path suffix, an override template returned by a provider gets exactly the same escaping and sanitisation as the bundled one it replaces - the KDoc states the reasoning: "an uploaded template is trusted-by-role code, not trusted to skip the injection defenses" (DocumentRenderer.render).

2. Eleven templates, eleven routes

Template Route Media type Declared action
pick-ticket.html GET /api/v1/pick-orders/{id}/pick-ticket.pdf application/pdf fulfillment.read
bol.html GET /api/v1/shipments/{id}/bol.pdf application/pdf fulfillment.read
packing-slip.html GET /api/v1/shipments/{id}/packing-slip.pdf application/pdf fulfillment.read
packet-list.html GET /api/v1/shipments/{id}/packet-list.pdf application/pdf fulfillment.read
packet-content-list.html GET /api/v1/shipping-units/{id}/content-list.pdf application/pdf fulfillment.read
label.zpl GET /api/v1/shipping-units/{id}/label.zpl text/plain fulfillment.read
ul-content-list.html GET /api/v1/unit-loads/{id}/content-list.pdf application/pdf inventory.read
ul-label.zpl GET /api/v1/unit-loads/{id}/label.zpl text/plain inventory.read
ul-advice-label.zpl GET /api/v1/asns/{id}/ul-labels.zpl text/plain orders.read
delivery-note.html GET /api/v1/delivery-orders/{id}/delivery-note.pdf application/pdf orders.read
location-label.zpl GET /api/v1/locations/{id}/label.zpl text/plain layout.read

Four of the five shipping documents are gated on shipment state through DocumentAvailabilityStrategy and answer 409 document-not-ready before it (ShipmentDocumentResource); the packet content list is the exception and carries no gate. The delivery note is gated on the order reaching PICKED (DeliveryOrderResource.deliveryNote). The pick ticket carries no gate at all.

Every route takes ?store=true, an opt-in that archives the rendered bytes through Instance<DocumentStore> and is a no-op when the docstore module is not on the classpath.

3. The archive is write-through only

karyo-docstore-core implements DocumentStore and exposes three routes: GET /api/v1/documents (paginated), GET /api/v1/documents/{id}/content and DELETE /api/v1/documents/{id} (DocumentResource.list, DocumentResource.content, DocumentResource.delete). Reading declares operations.view, which every standard profile from viewer to administrator holds, and deleting declares documents.delete, which the manager and administrator profiles hold (services/document-service/karyo-docstore-core/src/main/kotlin/com/karyo/docstore/api/v1/DocumentResource.kt, DocumentResource.list, DocumentResource.content, DocumentResource.delete). There is no upload route - the only way a document enters the archive is ?store=true on one of the eleven render routes. The 10 MB MAX_CONTENT_BYTES guard (DocumentStoreService.deleteById) therefore protects an internal caller, not a request body.

Facility scoping is careful and worth recording, because it is the case most likely to be got wrong: a stored document's Facility is the subject entity's Facility, never the acting principal's, so an operator acting across Facilities writes a row belonging to the Facility of the entity (DocumentStoreService; ADR 0046). store refuses a Facility outside the writable scope outright with 403 (DocumentStoreService.store); findForRead and deleteById answer 404 rather than 403 for an out-of-scope row, because "a 403 would confirm the row exists to a principal not entitled to know that" (DocumentStoreService.findForRead, DocumentStoreService.deleteById).

DocumentStoreService.list is the only endpoint in the application that clamps page size, to 200, and its companion KDoc explains why it does so locally rather than in the shared PaginationParams (DocumentStoreService.deleteById).

Filenames are always generated as {documentType}-{numericId}.{ext} at the call sites (UnitLoadDocumentService.archive, AsnDocumentService.archive, OrderDocumentService.deliveryNotePdf, LocationDocumentService.labelZpl, PickDocumentService.pickTicketPdf, ShipmentDocumentService.archive), so the Content-Disposition header DocumentResource.content builds carries no caller-supplied text.

4. The override seam belongs to the document-templates engine

DocumentTemplateProvider is a first-non-null seam consulted by DocumentRenderer.render before the bundled classpath template, resolved through Instance<> so libs/karyo-documents has no compile-time dependency on whoever implements it (DocumentTemplateProvider). Its contract is precise about the null case: ownerClientId == null means "no owner context" and must resolve to no override, never to some other client's template.

Its implementation is the document-templates engine, available on every installation; the console page shows while the installation's profile shows Document templates. Every template route declares settings.manage (services/document-service/karyo-doctemplates-core/src/main/kotlin/com/karyo/doctemplates/api/v1/DocumentTemplateResource.kt, DocumentTemplateResource), an administration action the standard administrator profile holds and that is granted only at the Company or a Business Unit. A holder of it authors overrides on the console's template administration page (tests/e2e/tests/profiles.spec.ts). A goods owner with no active override gets the bundled template. Whatever a provider returns, the renderer applies the escaping and ZPL sanitisation of §1 to it.

The engine's provider answers from the document-templates cache, so a render costs no query for the template: a label is rendered once per label at pick rate. The cache holds the answer for one owner and path, a missing override included, for ten minutes at most and for 300 owner-and-path pairs at most. The engine's three writers, upload, activate and delete, drop the whole cache when they return. A second application instance would keep its own cache and serve the old template until its entries expire; the installation runs one instance (ADR 0012).

5. Configure the shipper address before shipping

bol.html renders the Ship From block from ShipFromConfig, mapped under karyo.shipping.ship-from. Set KARYO_SHIP_FROM_NAME, KARYO_SHIP_FROM_STREET, KARYO_SHIP_FROM_CITY, KARYO_SHIP_FROM_ZIP_CODE and KARYO_SHIP_FROM_COUNTRY in the production environment file. The deployment settings list their meanings and literal-value rules. The templates expose all five fields, and the runtime catalogue identifies them as environment-only, installation-wide settings requiring a restart.

Non-empty demo defaults remain for local boot compatibility with SmallRye configuration. ShipFromStartupWarning logs a warning when the name, street, city or postal code still identifies the demo. The production deploy preflight refuses missing fields and retained demo identifiers. An explicitly enabled KARYO_DEMO stack may keep them. US alone is a valid country, not a demo identifier. Starting the application outside the deploy script logs a warning but does not refuse boot.

Every goods owner and Facility shares this address; per-tenant overrides remain a follow-up. Before commissioning shipping, dispatch a synthetic manual shipment and verify its bill of lading's Ship From block. The customer's Ship To block is separate.

6. One printer, one printable document

LabelPrintService pushes raw ZPL to a TCP socket, port 9100 by default, with a 3-second connect timeout and no spool (LabelPrintService.printTo). Its KDoc gives the reason for failing fast rather than queueing: "a floor operator standing at the printer needs the error now; a spooler printing queued labels later creates mislabeling risk" (LabelPrintService). Its two failure modes map to RFC 7807 through dedicated mappers - 503 no-printer-configured and 502 printer-unreachable (NoPrinterConfiguredExceptionMapper.toResponse, PrinterUnreachableExceptionMapper.toResponse).

The target is karyo.print.url, resolved for the calling Facility (Facility, Business Unit, Company, then the instance value) with the "none" sentinel (LabelPrintService.print). Each Facility can name its own printer. A Facility with no printer of its own prints to the inherited one, or fails with no-printer-configured.

printTo accepts an arbitrary host:port and is deliberately not reachable from REST - the only caller is LabelPrintService.print, which resolves karyo.print.url for the Facility and then calls it. UnitLoadResource.printLabel calls print. printTo is public for tests.

The print call is also the only print endpoint in the API: POST /api/v1/unit-loads/{id}/label/print (UnitLoadResource.printLabel), which declares inventory.write. The other three ZPL documents - the shipping-unit carton label, the ASN advice labels and the location label - can only be downloaded. An integrator or a floor station wanting those on a printer has to fetch the ZPL and push it themselves, and a downloaded label is no proof that a printer received it. The implementer guide covers printers as part of operating an installation.