Skip to content

Documents

Updated

A PDF entity (ent-pdf) shows the pages of a PDF document on a plane in the world: a menu on a stand, a handout on a desk, a slide deck a presenter pages through. Its PDF component (script class PDFComponent) shows one page at a time and can draw a small previous/next pill under the page. This page explains its props, how it sizes itself, the three ways a page changes, and how scripts build slide remotes, page indicators and document switchers on top of it.

  • Props and setProps. Scripts change the page by setting the page prop. See Components.
  • Signals. PDF::ready, PDF::error and PDF::page reach scripts through a Signal Emitter on the PDF entity.
  • Intrinsic sizing. The sizing: 'auto' mode is explained on Images.
  • Keyboard and UI input. Examples use Input.keyboard and UI buttons. See Input and interaction and UI.

comp-pdf

Prop Type Default Description
src string none The document: a URL or an asset from the project.
width number 1.2 Plane width in metres. The height follows the page’s aspect ratio.
sizing 'manual' | 'auto' none auto fits each page inside width × height instead.
height number none The height bound for sizing: 'auto'; absent, it equals width. Ignored otherwise.
page number 1 The page shown, counted from 1. Kept within the document’s pages.
controls boolean true A previous/next pill under the page, when the document has more than one page.

Without sizing: 'auto' the plane is width wide and as tall as the page’s shape makes it. A portrait A4 page at width 1.2 is about 1.7 tall, and a landscape slide at the same width is much shorter.

With sizing: 'auto' each page fits inside width × height (a square of width when height is absent). Landscape and portrait pages then take the same box, which is useful when a document mixes both.

The plane is square until the first page has rendered, then takes the page’s shape. A Frame on the entity follows every resize.

The page shown changes in three ways:

  1. The pill. Previous and next step one page and wrap around at the ends. A step writes the new number into the page prop, so it is saved, and shared with other players when the entity is networked.
  2. The page prop. A script sets it with setProps({ page }). Once the document has loaded, a number past the end is stored as the last page, one below 1 as the first.
  3. Loading. A new src loads the document and shows page.

Each page change draws the new page into a fresh texture. That takes a moment for a heavy page, so change pages in response to a person, not every frame.

Sent through the Signal Emitters on the entity.

Event EventType Data When
PDF::ready EventType.PDF.Ready { pages }, the page count The document and its first page are shown.
PDF::error EventType.PDF.Error { message } The document cannot load.
PDF::page EventType.PDF.Page { page, pages } Another page is shown, from the pill or a page change.

The first page is announced by ready, not by page. Read the page count from ready: it is the only place a script learns it.

A slide deck the presenter drives with the left and right arrow keys. The scene has a PDF entity named Deck with a Signal Emitter whose signal is deck. In the live example below, Deck shows a sample PDF served with these docs at /example-assets/pdf-sample.pdf.

DeckKeys.jx
#pragma lifecycle(startup, update)
let deck;
let pages = 1;
function startup() {
deck = findEntity('Deck').getComponent(PDFComponent);
deck.setProps({ controls: false, page: 1 });
on('deck', (event) => {
if (event.eventType === EventType.PDF.Ready) pages = event.data.pages;
});
}
function update() {
const page = deck.props.page;
if (Input.keyboard.isKeyReleased(Key.ArrowRight) && page < pages) {
deck.setProps({ page: page + 1 });
}
if (Input.keyboard.isKeyReleased(Key.ArrowLeft) && page > 1) {
deck.setProps({ page: page - 1 });
}
}
  • controls: false hides the pill, because the keys replace it.
  • pages starts at 1 and is updated from PDF::ready, the only event that carries the page count.
  • deck.props.page is the page shown now, whether the keys, the pill or another player changed it last.
  • The checks against 1 and pages stop at the first and last slides instead of wrapping.

UI buttons that turn pages, and a label that shows “Page 3 of 12”. The scene has:

  • A PDF entity named Handout with a Signal Emitter whose signal is handout.
  • Two UI Buttons, each with a Signal Emitter: signal handout/next on one, handout/prev on the other.
  • A Text entity named Page Label.
HandoutRemote.jx
#pragma lifecycle(startup)
let handout;
let label;
let pages = 1;
function showPage(page) {
label.setProps({ text: `Page ${page} of ${pages}` });
}
function startup() {
handout = findEntity('Handout').getComponent(PDFComponent);
label = findEntity('Page Label').getComponent(TextComponent);
label.setProps({ text: 'Loading…' });
on('handout', (event) => {
if (event.eventType === EventType.PDF.Ready) {
pages = event.data.pages;
showPage(handout.props.page);
}
if (event.eventType === EventType.PDF.Page) {
pages = event.data.pages;
showPage(event.data.page);
}
if (event.eventType === EventType.PDF.Error) {
label.setProps({ text: 'Document unavailable' });
}
});
on('handout/next', (event) => {
if (event.eventType !== EventType.UIButton.Press) return;
const page = handout.props.page;
handout.setProps({ page: page < pages ? page + 1 : 1 });
});
on('handout/prev', (event) => {
if (event.eventType !== EventType.UIButton.Press) return;
const page = handout.props.page;
handout.setProps({ page: page > 1 ? page - 1 : pages });
});
}
  • The label updates from PDF::page, so it is also right when someone uses the pill.
  • PDF::ready covers the first page, which PDF::page does not announce.
  • The buttons wrap around at both ends, like the built-in pill.
  • Each button event is checked against EventType.UIButton.Press, so other events from the same emitter are ignored.

A menu stand that shows the lunch or dinner menu depending on which sign is clicked. The scene has a PDF entity named Menu with a Signal Emitter whose signal is menu, and two Primitive Meshes with Selectables and Signal Emitters: signal menu/lunch and menu/dinner.

MenuSwitcher.jx
#pragma lifecycle(startup)
const MENUS = {
lunch: '/example-assets/handout.pdf',
dinner: '/example-assets/pdf-sample.pdf',
};
let menu;
function show(name) {
menu.setProps({ src: MENUS[name], page: 1 });
log(`Showing the ${name} menu`);
}
function startup() {
menu = findEntity('Menu').getComponent(PDFComponent);
menu.setProps({ sizing: 'auto', width: 0.8, height: 1.1 });
for (const name of Object.keys(MENUS)) {
on(`menu/${name}`, (event) => {
if (event.eventType === EventType.Selectable.Select) {
findEntity(event.entityId)?.getComponent(SelectableComponent)?.setSelected(false); // so the next click selects it again
show(name);
}
});
}
on('menu', (event) => {
if (event.eventType === EventType.PDF.Error) {
log('Menu failed to load:', event.data.message);
show('lunch');
}
});
}
  • setProps({ src, page: 1 }) changes the document and resets the page in one call. Without page: 1, a new document opens at the old page number.
  • sizing: 'auto' with a 0.8 × 1.1 box keeps both menus the same size on the stand, whatever their page shape.
  • A loop registers one listener per menu, so adding a third menu needs only a new line in MENUS.
  • On PDF::error the script falls back to the lunch menu.

A noticeboard that turns to the next page every eight seconds, and pauses for half a minute when a visitor uses the pill. The Behavior entity is a child of a PDF entity with a Signal Emitter whose signal is kiosk.

Kiosk.jx
#pragma lifecycle(startup)
const PAGE_MS = 8000;
const IDLE_MS = 30000;
let board;
let pages = 1;
let expected = 1;
let cancelNext = () => {};
function schedule(ms) {
cancelNext();
cancelNext = delay(() => {
expected = board.props.page < pages ? board.props.page + 1 : 1;
board.setProps({ page: expected });
schedule(PAGE_MS);
}, ms);
}
function startup() {
board = self.parent.getComponent(PDFComponent);
on('kiosk', (event) => {
if (event.eventType === EventType.PDF.Ready) {
pages = event.data.pages;
if (pages > 1) schedule(PAGE_MS);
}
if (event.eventType === EventType.PDF.Page && event.data.page !== expected) {
// Someone used the pill: leave them time to read.
expected = event.data.page;
schedule(IDLE_MS);
}
});
}
  • delay returns a function that cancels the call. schedule cancels the pending turn before setting a new one, so only one is ever waiting.
  • expected remembers which page the script asked for. A PDF::page for any other page must have come from the pill, so the script waits longer.
  • A one-page document never schedules anything.
  • Turn pages in response to people, not every frame. Each change draws a page, and opening a large document or a heavy page can take a few frames.
  • Use sizing: 'auto' when documents of different shapes share one place, instead of resizing the entity from a script.
  • The page is drawn unlit and double-sided, so it costs no lighting work.
  • Counting pages from PDF::page only. The first page is announced by PDF::ready, which is also where the page count arrives.
  • Forgetting page: 1 when changing src. The new document opens at the current page number.
  • Adding a Selectable to the PDF entity. It does not accept one; take clicks on a separate entity, a UI button or a key.
  • Setting height without sizing: 'auto'. height is only a bound for automatic sizing; otherwise the page’s shape decides the height.
  • Assuming pages start at 0. Pages are counted from 1.