Print pages — printing a record to a PDF file
Declare print on a record page, then archive a record's PDF as an attachment with the print service, rendered in headless Chromium as the requesting user
A print page is an ordinary page with one more key, print. The console
draws it on screen as before and, when a user prints, puts only that page on
paper: the sheet size, margins, page numbers and repeated header and footer come
from the print declaration, mapped to print CSS by the console.
@objectstack/service-print prints the same page on the server. It renders the
record's print page in headless Chromium, as the requesting user, and
archives the PDF as a file attached to the record — downloadable from the
record's attachments, and usable wherever a file is.
Declare the print page
A print page is a kind: 'full' record page whose blocks are all from the
printable subset (PRINTABLE_PAGE_COMPONENT_TYPES in @objectstack/spec/ui;
os validate refuses anything else). The record's object must allow files.
import { definePage } from '@objectstack/spec/ui';
export const InvoiceDocumentPage = definePage({
name: 'invoice_document',
label: 'Invoice',
type: 'record',
object: 'invoice',
kind: 'full', // not isDefault: the invoice keeps its own working record page
print: { paperSize: 'A4', orientation: 'portrait', repeatHeader: true, pageNumbers: true },
regions: [
{ name: 'header', components: [{ type: 'element:text', properties: { content: 'ACME Ltd' } }] },
{ name: 'main', components: [{ type: 'record:details' }] },
],
});
// …and on the object: enable: { files: true }A record prints on any record page of its object that declares print, so
an object keeps its everyday record page and adds one or more documents beside
it (an invoice, a delivery note, a quote). The server renders the page the
request names on the console's own routes: the object's default record page
(isDefault: true) on the record route, …/OBJECT/record/ID, and any other
print page on the named record-page route, …/OBJECT/record/ID/page/PAGE. A
page that is not a record page of the object (type: 'record' with that
object), or that declares no print, is refused rather than printed as some
other page.
Print in the browser
A user can print a print page without the service. When the record page bound to a
record declares print, the record view shows a Print button that opens the browser's
print preview of that page; saving a PDF there is the browser's own "Save as PDF". A print
page that is not the object's default opens for the same record at
/apps/APP/OBJECT/record/ID/page/PAGE, with the same button. PAGE must be a record page
bound to OBJECT: any other name shows "Page not found", never the default page, so that URL
never puts a different document on paper. (Printing from the browser, print is also
accepted on a home or app page; the service prints record pages only.)
The console mounts a print stylesheet for a page that declares print, and only for it.
Every rule sits inside @media print, so the page on screen is unchanged and is itself the
preview. Only the print page reaches paper: the console chrome and the record view's own
panels are hidden, and the page runs onto as many sheets as it needs.
| Key | On paper |
|---|---|
paperSize (A4, A5, Letter, Legal), orientation | @page { size }; A4 portrait when omitted |
margins | The @page margin, one side per key, in millimetres; a side left out keeps the console default |
pageNumbers | "page / pages" in the bottom-right margin box |
repeatHeader, repeatFooter | The page's header / footer region repeated on every sheet |
repeatTableHeaders | A table's column headings repeated on every sheet it runs onto; on when omitted |
avoidBreakInside | break-inside: avoid on each block of a region |
Blocks print whole:
record:line_itemsloads at most its row cap (limit, 500 when unset). On a print page, when the child object has more lines than that, the block draws the cap's lines and a note naming the cap and, when the server reports one, the total, so a printed copy never drops lines silently.- A collapsible
record:detailssection draws open, and a section prints every declared field, empty ones included, with no "Show N empty fields" row. A section'shideEmptystill hides what it hides.
Compose the print service
The service is composed explicitly; a deployment that does not list it never loads a browser driver.
import { PrintServicePlugin } from '@objectstack/service-print';
export default defineStack({
// …
plugins: [new PrintServicePlugin()],
});The engine is headless Chromium, in one of two placements. Configure exactly one, plus the URL the browser loads the console from:
| Setting | Plugin option | What it is |
|---|---|---|
OS_PRINT_CHROMIUM_PATH | chromiumPath | Local placement: a Chromium executable on this host, launched per print. |
OS_PRINT_BROWSER_ENDPOINT | browserEndpoint | Remote placement: a browser's DevTools WebSocket endpoint (ws:// or wss://), connected per print. For hosts that cannot carry a browser of their own. The requesting user's session cookie travels in the endpoint's frames: use wss:// for a browser off this host, and ws:// only on loopback. |
OS_PRINT_CONSOLE_URL | consoleUrl | The console's URL as that browser reaches it, /_console included (http://127.0.0.1:3000/_console). A URL carrying credentials (user:password@) is refused. |
A plugin option wins over its variable. At startup the plugin probes the engine — launches the executable or connects to the endpoint — and logs the placement and Chromium version. An engine that is not configured, is configured twice, or does not answer is reported once, at startup, with the setting to fix; every print is then refused until it is. The remote endpoint is never logged beyond its scheme and host, since it may carry a token.
Chinese, Japanese and Korean text prints with whatever fonts the browser's host
has: install a CJK font there (for example fonts-noto-cjk), or those
characters print as empty boxes.
Print a record
The service is reached in-process, through the print service:
const print = kernel.getService<IPrintService>('print');
print.status();
// { state: 'ready', placement: 'local', version: '141.0.7390.37' }
const file = await print.printRecordToFile(
{ app: 'sales', object: 'invoice', recordId: 'inv_1', page: 'invoice_document' },
context, // the requesting user's execution context
);
// { fileId, attachmentId, size, filename: 'invoice_document-inv_1.pdf' }The page is rendered as the requesting user — never as the system — through a short-lived session minted for that user and revoked as soon as the print ends, whether it succeeded or not. The PDF is archived as theirs: the attachment is written under their own permissions, and both the file and the attachment name them. A user can never archive a record they could not read, or attach a file where they could not attach one by hand.
Print from the console
PrintServicePlugin also mounts one HTTP route on the kernel's HTTP server,
POST /api/v1/print/records, so a user can print the record they are looking
at. Its JSON body is the same request, { app, object, recordId, page, filename? }, and it answers 201 with the stored file,
{ fileId, attachmentId, size, filename }. The print runs as the signed-in
caller, exactly as an in-process print with their context does, and the file is
attached once, by the service.
Reach it from the console with an ordinary type: 'api' action on the object:
import { defineAction } from '@objectstack/spec/ui';
export const GenerateInvoicePdf = defineAction({
name: 'generate_invoice_pdf',
label: 'Generate PDF',
objectName: 'invoice',
type: 'api',
target: '/api/v1/print/records',
method: 'POST',
recordIdParam: 'recordId', // the clicked record's id
bodyExtra: { app: 'sales', object: 'invoice', page: 'invoice_document' },
locations: ['list_item'],
refreshAfter: true,
});The route refuses, before it asks the service anything: an anonymous request
(UNAUTHENTICATED / 401); a sign-in held by an authentication policy (403 with
the policy's own code); a request from an impersonation session — an
administrator signed in as the user — (PERMISSION_DENIED / 403: a print is
archived in the user's name, so it is never made for them by someone else); and
a query parameter or a body field outside the request (VALIDATION_ERROR /
400). Every refusal below then reaches the caller with its own code and
status; a 5xx answers with the route's own sentence, and the service's text is
written to the server log. The route writes no audit row of its own: the print's
session sign-in and sign-out are recorded as any sign-in is, and the file and
the attachment name the caller.
Refusals
Each refusal carries code and status, and every one of them is decided
before a session is minted or a browser is started, in this order:
| Order | Condition | code / status |
|---|---|---|
| 1 | The caller is not a signed-in user acting for themselves (system, agent, service or guest principal; a sign-in held by an authentication policy) | PERMISSION_DENIED / 403 |
| 2 | The engine is not ready (status() says why) | SERVICE_UNAVAILABLE / 503 |
| 3 | No such page | RESOURCE_NOT_FOUND / 404 |
| 3 | The page is not a record page of the object (type: 'record', that object), or declares no print | VALIDATION_ERROR / 400 |
| 4 | The record does not exist, or the caller cannot read it | RECORD_NOT_FOUND / 404 |
| 5 | The object does not declare enable: { files: true } | FILES_DISABLED / 403 |
| 5 | The caller may not create attachments at all | PERMISSION_DENIED / 403 |
| 5 | The caller may not attach a file to this record (attaching needs edit access to it) | ATTACHMENT_PARENT_ACCESS / 403 |
An engine that did not answer its last probe is probed once more only after step 5, so no browser is started before every refusal above has had its say.
After those, a print can still fail as the render or the archive does: the page
not rendering within the 30 s budget (TIMEOUT / 504), or the archive's own
refusals, such as a file over the upload size limit (PAYLOAD_TOO_LARGE / 413).
A failure to end the session afterwards never replaces the print's own outcome;
it is logged, and the session expires on its own within 60 s.