# Flipbook

`@vigdx/flipbook` is an Angular PDF reader that turns like a book: pdf.js rendering in a worker — or pre-rendered page images with no pdf.js at all — a page-curl turn, tiled zoom, virtualised spreads and thumbnails, full-document search and touch gestures, standalone, with its own tokens.

- Package: [@vigdx/flipbook](https://www.npmjs.com/package/@vigdx/flipbook)
- Source: [libs/flipbook/src/index.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/flipbook/src/index.ts)

```typescript title="Import"
import { FlipbookSurface, provideFlipbook } from '@vigdx/flipbook';
```

[Open the demo](https://vigdx.com/flipbook/demo.md) [Mobile layout](https://vigdx.com/flipbook/demo/mobile.md) [Image source](https://vigdx.com/flipbook/images.md)

## Features

- **Turns under the thumb.** Drag the page and it follows; flick and it turns. A finger turns it from anywhere; a mouse or a pen takes the page by its edge, because a drag across the page selects text.
- **Bounded memory.** Fixed canvas pools and pixel-budgeted tile caches: a 200-page document never mounts 200 canvases.
- **Reads both ways.** RTL mirrors the board and the gestures.
- **Nothing hardwired.** Every color and radius is a CSS custom property, every string an input, and the toolbar is built from exported atoms.
- **Accessible.** Every page carries a selectable text layer that screen readers read, page turns are announced, the drawer is a native dialog, and forced colors and reduced motion are honoured.
- **SSR-safe.** On the server the viewer renders its waiting state and starts nothing; it comes alive on hydration. With `load="visible"` it waits for the viewport before fetching anything.
- **Tailwind optional.** One precompiled stylesheet, or utilities from your own Tailwind CSS v4 build. Tokens follow the page's light or dark color scheme.

## Install

Current version: [@vigdx/flipbook 2.2.0](https://www.npmjs.com/package/@vigdx/flipbook)

```bash
pnpm add @vigdx/flipbook pdfjs-dist
```

Or run `ng add @vigdx/flipbook`: it writes the worker entry and its factory and imports the styles — the steps below — and leaves you one line: `provideFlipbook({ createWorker: createPdfWorker })`, on the route that owns the reader.

## Setup

### 1. Styles

```css title="styles.css"
/* Without Tailwind: the precompiled styles, in one file. */
@import '@vigdx/flipbook/flipbook.css';
```

Already on Tailwind CSS v4? Generate the same classes in your own build instead:

```css title="styles.css"
/* With Tailwind CSS v4: tokens, and the package's code as a source. */
@import 'tailwindcss';
@import '@vigdx/flipbook/theme.css';
@source '../node_modules/@vigdx/flipbook/fesm2022';
```

No UI-kit dependency: the viewer ships its own tokens. Six of them follow `@vigdx/ui` (or any host) when present — `--color-primary`, `--color-error`, `--color-muted`, `--color-border-control`, `--radius-button` and `--radius-input`.

### 2. The pdf.js worker

pdf.js parses documents in a Web Worker that the application has to supply: `ng build` does not bundle a worker from a package in `node_modules`. Create a one-line entry point and a factory with a _relative_ URL, and provide it at the route level so the library lands in a lazy chunk:

```typescript title="src/pdf.worker.ts"
// src/pdf.worker.ts — one line, no Angular imports. It loads pdf.js's
// modern worker, or its legacy worker on browsers that need it.
import '@vigdx/flipbook/pdf-worker';

// src/app/pdf-worker.ts — relative on purpose: @angular/build bundles a
// worker only from your own code, and resolves the URL with path.join.
export function createPdfWorker(): Worker {
  return new Worker(new URL('../pdf.worker', import.meta.url), { type: 'module' });
}
```

```typescript title="flipbook.routes.ts"
// flipbook.routes.ts, loaded via loadChildren
import { Route } from '@angular/router';
// The config entry point: a few hundred bytes, not the viewer.
import { provideFlipbook } from '@vigdx/flipbook/config';
import { createPdfWorker } from './pdf-worker';

export const flipbookRoutes: Route[] = [
  {
    path: '',
    // pdf.js's assets load on demand from jsDelivr, pinned to your version.
    providers: [provideFlipbook({ createWorker: createPdfWorker })],
    loadComponent: () => import('./reader').then((m) => m.Reader),
  },
];
```

### 3. pdf.js assets

Nothing to do. Character maps, standard fonts, WASM codecs and ICC profiles are fetched on demand, only when a document needs them, from jsDelivr, pinned to your `pdfjs-dist` version. Under a CSP, the worker needs `connect-src https://cdn.jsdelivr.net`.

Offline, on an intranet, or keeping readers' requests off third-party servers? Serve them yourself — `ng add @vigdx/flipbook --self-host-assets` does both halves:

```typescript title="self-hosted assets"
// Self-hosting: build.options.assets in angular.json / project.json
[
  { "glob": "**/*", "input": "node_modules/pdfjs-dist/cmaps", "output": "pdfjs/cmaps" },
  { "glob": "**/*", "input": "node_modules/pdfjs-dist/standard_fonts", "output": "pdfjs/standard_fonts" },
  { "glob": "**/*", "input": "node_modules/pdfjs-dist/wasm", "output": "pdfjs/wasm" },
  { "glob": "**/*", "input": "node_modules/pdfjs-dist/iccs", "output": "pdfjs/iccs" }
]

// and on the route:
providers: [provideFlipbook({ createWorker: createPdfWorker, assetsUrl: 'pdfjs/' })]
```

## Pre-rendered pages instead of pdf.js

For a book you publish, convert it once and let the reader fetch images: no pdf.js, no worker, no assets, and the first page in the server-rendered HTML. The converter ships in the package; text for the text layer and search comes along in the manifest.

```bash
# At build or upload time: WebP pages, the manifest and the text
npx -p @vigdx/flipbook vigdx-flipbook-images catalog.pdf public/books/catalog
```

```typescript title="reader.routes.ts"
import { provideFlipbookImages } from '@vigdx/flipbook/images';

// On the reader's route or component, in place of provideFlipbook():
providers: [provideFlipbookImages()]

// <vig-flipbook-surface src="/books/catalog/manifest.json" />
```

See it next to the pdf.js demo, with measurements, on the [image source](https://vigdx.com/flipbook/images.md) page.

## Usage

The surface works alone — give its host a height and a source:

```html
<vig-flipbook-surface class="h-[80dvh]" [src]="'/assets/catalog.pdf'" [(page)]="page" />
```

For the full viewer, provide one controller with `provideFlipbookViewer()` and compose the chrome around the surface. The injected `FlipbookController` is the programmatic API: `goTo(page)`, `next()`, `setZoom()` and signals such as `page` and `numPages`.

```typescript title="reader.ts"
import { Component } from '@angular/core';
import {
  FlipbookSearch,
  FlipbookSurface,
  FlipbookThumbnails,
  FlipbookToolbar,
  provideFlipbookViewer,
} from '@vigdx/flipbook';

@Component({
  selector: 'app-reader',
  imports: [FlipbookSurface, FlipbookToolbar, FlipbookThumbnails, FlipbookSearch],
  providers: [provideFlipbookViewer()],
  template: `
    <vig-flipbook-toolbar [showDownload]="true" [showPrint]="true" [showFullscreen]="true" />
    <vig-flipbook-search />
    <div class="flex min-h-0 flex-1 gap-3">
      <vig-flipbook-thumbnails class="w-36 flex-none" />
      <vig-flipbook-surface class="min-h-0 flex-1" [src]="src" />
    </div>
  `,
})
export class Reader {
  readonly src = '/assets/catalog.pdf';
}
```

## Components

| Selector | What it is |
| --- | --- |
| `vig-flipbook-surface` | The book itself. Works alone with [(page)]. |
| `vig-flipbook-toolbar` | Navigation, zoom, spread, save, print and fullscreen. |
| `vig-flipbook-thumbnails` | Virtualised page rail, vertical or horizontal. |
| `vig-flipbook-search` | Search box with match stepping and highlights. |
| `vig-flipbook-drawer` | Slide-over panel for chrome on small screens. |
| `vig-flipbook-progress` | Load progress bar driven by the byte counts of the load. |
| `[vigFlipbookIconButton], vig-flipbook-icon` | Atoms the toolbar is built from, for chrome of your own. |
