Skip to content
PressJetDocs Docs v0.1.0-beta.9 GitHub
Edit this page
Source docs v0.1.0-beta.9 Install v0.1.0-beta.7
On this page

Render from the browser with tickets

A render ticket lets a web page get a PDF straight from Pressjet without holding a key. Your backend, which knows the signed-in user, asks Pressjet for a ticket with its secret key and passes it to the page. The ticket opens one rendering operation for a few minutes. It can also be limited to one template, one exact request body and one browser origin. The PDF never passes through your backend, and a ticket copied from the page opens nothing else.

DocuSign's embedded signing and Pressjet's own signing panel links work the same way.

browser ──(your session)──▶ your backend ──(pj_sk_ key)──▶ POST /v1/tickets
browser ◀────────── ticket ─────────────┘
browser ──(Authorization: Bearer pj_tk_…)──▶ POST /v1/templates/invoice/render ──▶ PDF
Through your backend Publishable key Render ticket
PDF bytes pass through your backend Yes No No
Credential in the page None Permanent, until disabled Expires in minutes
What a copied credential renders — Anything its operations allow Only what the ticket names
Your backend decides the content Yes No Yes, when the body is pinned

Configure the server

Make a ticket key and give the same list to every replica:

pressjet keys ticket
export PRESSJET_TICKET_KEYS="<the printed key>"

PRESSJET_TICKET_KEYS holds one or more base64 keys, separated by commas. The first one signs new tickets, and every listed key is accepted when checking them. To rotate, put a new key first, then remove the old one an hour later. The server also needs API keys: with PRESSJET_AUTH=disabled it refuses to start with ticket keys.

Issue a ticket from your backend

After you check that the user may see invoice 604, ask for a ticket that renders that invoice and nothing else:

import { Pressjet } from "pressjet";

const pressjet = new Pressjet({ apiKey: process.env.PRESSJET_API_KEY });

app.get("/invoices/:id/pdf-ticket", async (request, response) => {
  const invoice = await invoices.findForUser(request.params.id, request.user); // your check
  const ticket = await pressjet.createTicket({
    operation: "template-render",
    template: "invoice",
    body: { version: 1, data: invoice.data },
    origin: "https://app.example.com",
    expiresIn: 300,
  });
  response.json(ticket);
});

The answer is what the page needs:

{
  "ticket": "pj_tk_eyJ2Ijox….Qk3t…",
  "expiresAt": 1796457600,
  "method": "POST",
  "path": "/v1/templates/invoice/render",
  "body": "{\"version\":1,\"data\":{\"number\":\"604\"}}"
}

The other SDKs offer the same call: client.create_ticket(...) in Python, client.CreateTicket(...) in Go and $client->createTicket(...) in PHP.

Field Meaning
operation generate, html, preview, check, template-render or job-pdf (see Open a durable job's PDF). A ticket never signs, merges, redacts, or reaches the signing API. The issuing key must allow the operation.
template The template ID. template-render requires it, and every other operation refuses it.
body Optional. With it, the ticket accepts only this exact request, sent byte for byte as the answer's body text. Verification codes are then signed for the issuing key. Without it, the page may send any body for the operation, and its codes are unsigned.
origin Optional. With it, only requests from that Origin are accepted, and the answers carry CORS headers for it. A browser needs it. A native app, which sends no Origin, does not.
expiresIn 1 to 3600 seconds, default 300.
singleUse Optional. The first successful answer spends the ticket, and a second request gets 409 ticket_used. See Single-use tickets.
link Optional. The ticket carries its body and opens as a plain URL. See Open as a link.
job The durable job a job-pdf ticket opens.
filename Optional, for a link or job-pdf ticket: the file name the PDF is shown under.

Use it in the page

import { Pressjet } from "pressjet";

const ticket = await (await fetch(`/invoices/604/pdf-ticket`)).json();
const pressjet = new Pressjet({ baseUrl: "https://pdf.example.com" });
const pdf = await pressjet.renderWithTicket(ticket);

window.open(URL.createObjectURL(new Blob([pdf], { type: "application/pdf" })));

Without the SDK, send the ticket's body unchanged:

const answer = await fetch(`https://pdf.example.com${ticket.path}`, {
  method: ticket.method,
  headers: { authorization: `Bearer ${ticket.ticket}`, "content-type": "application/json" },
  body: ticket.body,
});

A ticket without a pinned body takes the body you pass: pressjet.renderWithTicket(ticket, { body }).

What the server checks

Every request with a ticket checks all of the following. No replica stores anything, and any replica with the same ticket keys gives the same answer.

  • The ticket's signature, against the listed ticket keys. A changed ticket, or one from another deployment, gets 401 unauthorized.
  • The expiry. A late ticket gets 401 ticket_expired.
  • The issuing key. It must still be in the key file, enabled, and allowed the operation. Disabling or rotating the key revokes its tickets within the five seconds the server takes to reload the file.
  • The route, template and body. Anything else gets 403 ticket_mismatch.
  • The origin. Another origin, or no Origin header when the ticket names one, gets 403 origin_not_allowed.

The request then counts against the issuing key's rate limit, its share of concurrent requests and in-flight bytes, and its cache scope. The access log shows the key's id with "ticket": true.

A ticket works until it expires, as often as it is sent, unless it is single-use. Keep expiresIn short, and pin the body when the content matters.

Single-use tickets

Add "singleUse": true and the first successful answer spends the ticket. A request that fails before its answer, such as a refused document or a busy server, does not spend it, so the page may retry. Once spent, the ticket gets 409 ticket_used.

Spent tickets must be remembered by every replica:

  • With the durable jobs database configured, it remembers them for every replica. Nothing else is needed.
  • Without it, set PRESSJET_TICKET_SINGLE_USE=memory on a deployment with one replica. The process remembers spent tickets until they expire, and forgets them when it restarts.
  • With neither, the server refuses to issue a single-use ticket (501 tickets_not_configured) rather than issue one it cannot hold to its word.

An <iframe>, an <a href> or a download cannot send an Authorization header. A link ticket carries its request inside, so the ticket alone is the URL:

const ticket = await pressjet.createTicket({
  operation: "template-render",
  template: "invoice",
  body: { version: 1, data: invoice.data },
  link: true,
  singleUse: true,
  filename: "invoice-604.pdf",
});
// ticket.url is "/v1/tickets/pj_tk_…"
<iframe src="https://pdf.example.com/v1/tickets/pj_tk_…"></iframe>

In the page, pressjet.ticketUrl(ticket) gives the full URL. The answer is the PDF, shown inline under filename. It carries Cache-Control: no-store and Referrer-Policy: no-referrer.

A link carries generate, html or template-render with a body of at most 4096 bytes, which keeps the URL under common proxy limits. A larger document belongs in a durable job. A link takes no origin, because a navigation sends none. The URL is the credential, and browsers keep it in their history, so keep links short-lived and single-use.

Open a durable job's PDF

For large documents or batches, the backend queues a durable job with its key. It then issues a job-pdf ticket for that job:

const ticket = await pressjet.createTicket({ operation: "job-pdf", job: job.id, singleUse: true });
// ticket.url is "/v1/jobs/<id>/pdf?ticket=pj_tk_…", for an iframe or a link;
// ticket.path with the ticket as Authorization: Bearer works with fetch.

The ticket opens that one job's PDF, for the project of the key that issued it, and nothing else: not the job's status, not another job. A job that is still rendering answers 404 not_found without spending a single-use ticket.

Errors · HTTP contract · API keys