# EPUB and PDF

Export your docs as EPUB or PDF.

`@vuesho/epub` packages your pages into an EPUB book, which the site serves as a file for e-readers and offline reading. These docs are also [a book](/docs.epub). To print some pages to PDF, use the [Puppeteer script](#pdf) instead.

## Setup [#setup]

### Install the Package [#install-the-package-step]

```npm
npm i @vuesho/epub
```

### Add the Plugin [#add-the-plugin-step]

```ts title="vuesho.config.ts"
import { epubPlugin } from "@vuesho/epub";
import { createSite, recommendedPreset } from "vuesho/config";

export default createSite(docs.toSource())
  .plugins(
    recommendedPreset(),
    // `/cover.png` is a file in `public/`.
    epubPlugin({ author: "Acme", cover: "/cover.png" }), // [!code highlight]
  )
  .config({ site: { name: "Acme Docs" } });
```

The build writes the book to `/docs.epub`, so it's there once deployed and in `vuesho preview`, not in `vuesho dev`. Its title comes from `site.name`, and its language from `site.lang`. On a site without a name, set `title`.

## Chapters [#chapters]

Each page is a chapter, in sidebar order, and pages the sidebar doesn't list come last. Pages with `noindex` are left out. With [Internationalization](/i18n), the book has the default language's pages. To leave more pages out, set `filter`:

```ts title="vuesho.config.ts"
epubPlugin({ filter: ({ page }) => !page.path.startsWith("changelog/") });
```

A chapter holds a page's title and its [Markdown version](/ai#markdown-version), and a link to a page of the book leads to its chapter. An e-reader shows a component's content without its props, so an empty component shows nothing:

```md tab="Page"
<Card title="Deploying" href="/deploying">
  Put your site online.
</Card>
```

```md tab="Book"
Put your site online.
```

To keep a component's props, give it a Markdown version, as in [AI and LLMs](/ai#component-markdown).

The book holds the pages' images, and the build downloads remote ones into it. An image that can't be read is left out with a warning.

### Pages Rendered on Request [#pages-rendered-on-request]

The book is a snapshot of the docs from the build. A page from a [dynamic source](/dynamic-rendering) has its content from the build, and a page that differs per reader has its public version, as for an [audience](/dynamic-rendering#audiences). Rebuild the site to update the book.

## Configuring the Book [#configuring-the-book]

`path` changes the book's URL, and its last segment is the downloaded file's name. To make two books, add `epubPlugin()` twice, with different paths and filters:

```ts title="vuesho.config.ts"
epubPlugin({ path: "/downloads/acme-guide.epub" });
```

`css` replaces the book's stylesheet. To extend the default one, start from `defaultEpubStyles`:

```ts title="vuesho.config.ts"
import { defaultEpubStyles, epubPlugin } from "@vuesho/epub";

epubPlugin({ css: `${defaultEpubStyles}\nbody { font-size: 1.1em; }` });
```

### Writing the Book to a File [#writing-the-book-to-a-file]

`outputPath` also writes the book to a file during the build. The path is relative to the project root:

```ts title="vuesho.config.ts"
epubPlugin({ outputPath: "dist/docs.epub" });
```

With `path: false`, the book is only written to `outputPath`, which keeps it out of the deployed site:

```ts title="vuesho.config.ts"
epubPlugin({ path: false, outputPath: "dist/docs.epub" });
```

### Reproducible Builds [#reproducible-builds]

The book's date is the last change of its pages, from the collection's [`lastModified`](/markdown/collections#last-modified-dates), or else the time of the build. For a reproducible build, set `SOURCE_DATE_EPOCH` in seconds, like the time of the last commit:

```bash
SOURCE_DATE_EPOCH=$(git log -1 --format=%ct) npx vuesho build
```

## PDF [#pdf]

[Puppeteer](https://pptr.dev) can print a few pages of your built site to PDF.

### Install Puppeteer [#install-puppeteer-step]

```npm
npm i -D puppeteer
```

### Write the Script [#write-the-script-step]

The script opens each page on the `vuesho preview` server and saves it as a PDF in `pdfs/`:

```ts title="scripts/export-pdf.ts"
import { mkdir } from "node:fs/promises";
import puppeteer from "puppeteer";

const origin = "http://localhost:3000";
const paths = ["/", "/guide/install"];

const browser = await puppeteer.launch();
await mkdir("pdfs", { recursive: true });
for (const path of paths) {
  const page = await browser.newPage();
  await page.goto(origin + path, { waitUntil: "networkidle2" });
  const name = path === "/" ? "index" : path.slice(1).replaceAll("/", "-");
  await page.pdf({ path: `pdfs/${name}.pdf`, width: 950, printBackground: true });
  await page.close();
}
await browser.close();
```

### Hide the Navigation [#hide-the-navigation-step]

Hide the sidebar and the table of contents in print, in your [stylesheet](/styling). For the notebook layout, use `#nd-notebook-layout` instead of `#nd-docs-layout`:

```css title="src/theme.css"
@import "vuesho/style/neutral.css";

/* [!code ++:10] */
@media print {
  #nd-docs-layout {
    --fd-sidebar-width: 0px !important;
  }

  #nd-sidebar,
  [data-toc-popover] {
    display: none;
  }
}
```

### Run the Script [#run-the-script-step]

Build the site and preview it, then run the script in a second terminal:

```bash
npx vuesho build
npx vuesho preview
```

```bash
node scripts/export-pdf.ts
```

### Building for Print [#building-for-print]

A collapsed accordion or an unselected tab doesn't print, and forms like feedback are no use in a PDF. Build the site for print with a `VITE_PRINT` flag that swaps these components for flat versions:

```vue title="src/components/print/Group.vue"
<template>
  <div><slot /></div>
</template>
```

```vue title="src/components/print/Section.vue"
<script setup lang="ts">
defineProps<{ title?: string; value?: string }>();
</script>

<template>
  <section>
    <p>
      <strong>{{ title ?? value }}</strong>
    </p>
    <slot />
  </section>
</template>
```

```ts title="src/print-components.ts"
import * as ui from "vuesho/components";
import Group from "./components/print/Group.vue";
import Section from "./components/print/Section.vue";

const print = import.meta.env.VITE_PRINT === "1"; // [!code highlight]

export const Accordions = print ? Group : ui.Accordions;
export const Accordion = print ? Section : ui.Accordion;
export const Tabs = print ? Group : ui.Tabs;
export const Tab = print ? Section : ui.Tab;
```

Register the components in the config. Code tabs still print their selected tab. Leave out the plugins that add forms, like [AI chat](/ai#ai-chat) and [feedback](/plugins/feedback):

```ts title="vuesho.config.ts"
const print = process.env.VITE_PRINT === "1"; // [!code ++]
const p = "/src/print-components.ts"; // [!code ++]

createSite(docs.toSource()).plugins(
  recommendedPreset(),
  { name: "site:print", components: { Accordions: p, Accordion: p, Tabs: p, Tab: p } }, // [!code ++]
  !print && chatPlugin({ model: "openai/gpt-5" }), // [!code highlight:2]
  !print && feedbackPlugin({ onPageFeedbackAction }),
);
```

```bash
VITE_PRINT=1 npx vuesho build
VITE_PRINT=1 npx vuesho preview
```

## Options [#options]

### EpubPluginOptions

| Prop           | Type                                                 | Description                                                                                                                                                                         |
| -------------- | ---------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `path?`        | `string \| false \| undefined`                       | The book's path in the output, without the base path, whose last segment is the downloaded file's name, or `false` for a book written only to `outputPath`. Default: `"/docs.epub"` |
| `title?`       | `string \| undefined`                                | The book's title. Default: `` `site.name` of the config ``                                                                                                                          |
| `author?`      | `string \| string[] \| undefined`                    | The author, or a list of authors. Default: `"anonymous"`                                                                                                                            |
| `description?` | `string \| undefined`                                | A summary of the book, which e-readers show with its title.                                                                                                                         |
| `publisher?`   | `string \| undefined`                                | The book's publisher. Default: `"anonymous"`                                                                                                                                        |
| `cover?`       | `string \| undefined`                                | The cover image: a site path without the base path, like `/cover.png` from `public/`, a path from the project root, or a URL.                                                       |
| `css?`         | `string \| undefined`                                | The book's stylesheet. Default: `` `defaultEpubStyles` ``                                                                                                                           |
| `filter?`      | `((env: PageEnv<AppShape>) => boolean) \| undefined` | Leaves out a page when it returns `false`. The book has content pages, so `env.page` is set, and never a page with `noindex`.                                                       |
| `outputPath?`  | `string \| undefined`                                | Another file the build writes the book to, from the project root, like `"docs.epub"`.                                                                                               |
