# Books

How an agent typesets a book on its workspace, checks the PDF and prints it at Lulu on your own Lulu account. What the workspace is and what it costs is on [Services](/services).

## Before you start

- You need your own account at Lulu. Lulu charges that account. STACK never pays a supplier.
- Connect it as in Print on demand: Lulu needs a key and a secret.
- The agent needs an open account, its Passport and a workspace.

The connect step is on [Print on demand](/docs/account/print-on-demand#connect).

## Typeset the interior

```text
In the workspace, typeset manuscript.typ as a 6 by 9 inch book with Typst and write book.pdf.
```

Every workspace has Typst, which turns plain text into a PDF. The agent runs it with `stack_workspace_run`:

```bash
typst compile book.typ book.pdf
```

- Set the trim size in the file, for example #set page(width: 6in, height: 9in, margin: 0.75in).
- Typst uses the fonts on the computer. Pass the PDF through Ghostscript so every font is embedded.

```bash
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -dEmbedAllFonts=true -sOutputFile=book-print.pdf book.pdf
pdfinfo book-print.pdf
```

`pdfinfo` prints the page count and the page size. Check both before you ask for a quote.

## Make the cover

A book has a second PDF for the cover. Lulu gives the size it must have for each product and page count, and the agent makes the cover to that size with Typst, the same way, then checks it with `pdfinfo`. Ghostscript, qpdf and poppler are installed, so the agent can also merge, split and repair PDFs.

## Save both files to the drive

```typescript
const ws = stack.accounts.workspace;
const interior = await ws.saveFile('acct_123', workspaceId, { path: 'book-print.pdf' }, { passportToken });
const cover = await ws.saveFile('acct_123', workspaceId, { path: 'cover.pdf' }, { passportToken });
```

The tool is `stack_workspace_save_file`. It takes a file of any size the drive takes: see [Workspace](/docs/account/workspace#save).

## Lulu

Lulu prints and ships books on demand. It prints from PDF files only. Lulu charges the card stored on the Lulu account you connected, and a new print job moves on from unpaid by itself. Quotes are in US dollars.

Connect it with the key and the secret of your Lulu account. The CLI reads them from a hidden prompt, or from the environment variables the command names, never from an argument. Only the owner or an admin can connect it:

```bash
npx @getstackrun/cli account making-connect-supplier lulu
```

From code: `connectSupplier('lulu', { api_key, api_secret })`. Lulu's sandbox is a separate Lulu account and host, and STACK uses the real one.

## Quote and order

```typescript
const design = await stack.accounts.making.uploadDesign('acct_123', {
  drive_file_id: interior.drive_file_id,
}, { passportToken });

const { quotes } = await stack.accounts.making.quote('acct_123', {
  design_id: design.id,
  supplier: 'lulu',
  spec: { quantity: 1, product: '0600X0900BWSTDPB060UW444MXX', page_count: 120 },
  recipient: {
    name: 'Anna Berg', email: 'anna@example.com', phone: '+14155550123',
    address1: '1 Main St', city: 'San Francisco', state: 'CA', postal_code: '94102', country: 'US',
  },
}, { passportToken });

const order = await stack.accounts.making.order('acct_123', quotes[0].id, { passportToken });
```

```bash
npx @getstackrun/cli account making acct_123 quote --design mkd_abc123 --supplier lulu \
  --quantity 1 --product 0600X0900BWSTDPB060UW444MXX --page-count 120 --recipient-file recipient.json
npx @getstackrun/cli account making acct_123 order mkq_abc123
```

- product is Lulu's pod package id: the trim size, colour, paper and binding in one code. It is required. The one above is a 6 by 9 inch black and white paperback.
- page_count is the number of pages in the interior. It is 1 when left out, so give it, as Lulu prices by page.
- One offer comes back for each shipping level Lulu prices for the destination, such as MAIL or EXPRESS.
- The print job is priced again when it is made. If it costs more than 2 percent, or 1.00, above the quote, STACK cancels it at once and refuses the order as price_changed.
- Lulu's status words are mapped as they come: in production, shipped and delivered, with tracking and an estimated arrival when Lulu gives them.
- Cancelling asks Lulu to cancel the print job, and Lulu refuses once production has started.

Ordering and approving work as in [Print on demand](/docs/account/print-on-demand#approve). Lulu charges your account.

An order through STACK carries one file, and STACK sends it to Lulu as both the interior and the cover. A book with a separate cover cannot be ordered through STACK yet. The agent makes both PDFs and keeps them on the drive, and the order itself is placed on Lulu with your account.

## When it is refused

- 409 MAKING_SUPPLIER_ACCOUNT_REQUIRED - your Lulu account is not connected.
- 422 MAKING_SUPPLIER_REJECTED - Lulu refused the product, the specification or the file. The supplier's reason comes back with it, for example format_unsupported (the file is not a PDF), product_required, product_invalid, no_shipping_option or price_changed.
- 409 MAKING_QUOTE_EXPIRED - the quote has lapsed. Quote again.
- 403 CONTROL_DENIED - with controls on, your policy or your own server refused it. Controls are asked as making.quote and making.order.

## Reference

Every field is on [Making](/docs/api/account-making). Every tool is in the [tool reference](/docs/mcp-tools).
