bookcover-web

中文文档
0.24.0发布于 2026/11/03MIT-0TypeScriptES Module

生成印刷就绪的书籍封面。部分 封套 一代系统。

中文使用文档
文档目录7 节
  1. 看到 模式参考 这是作为参数传递的,它实际上配置了封面设计。
  2. init(options): Promise<CoverGenerator>
  3. 资产
  4. 内置的背景
  5. CoverGenerator.generate(options): Promise<GenerateResult>
  6. CoverGenerator.destroy()
  7. 例子

bookcover-web

生成印刷就绪的书籍封面。部分 封套 一代系统。

npm install bookcover-web

Example cover

这是图书封面生成器的浏览器包装器。它使用WASM编译覆盖 typst.ts.

看到 模式参考 这是作为参数传递的,它实际上配置了封面设计。

import {init, CoverGenerator} from 'bookcover-web'

init(options): Promise<CoverGenerator>

使用初始化的WASM编译器和可选的呈现器创建生成器实例。

interface InitOptions {
    wasm_url: string              // URL to typst_ts_web_compiler_bg.wasm
    renderer_wasm_url?: string    // URL to typst_ts_renderer_bg.wasm (required for SVG/PNG)
    assets_prefix?: string        // URL prefix for static assets (templates, frames, backgrounds)
    fonts_prefix?: string         // URL prefix for the fonts tree (manifest.json + families)
}

资产

Typst模板被捆绑在里面 bookcover-core 本身(绑到确切的安装) 版本-不需要设置)。生成仍然在运行时获取三种资源,没有 在npm包中包含:WASM二进制文件、frame images (frames/),以及字体 树。最简单的设置是托管树 https://assets.paper.bible/ (歌珥启用):

这个 typst/ WASM二进制文件以brotli压缩方式存储并提供 Content-Encoding: br 每个客户端,没有内容协商-编译器是27 MB raw格式和6.9 MB压缩文件,所以这不是可选的。浏览器可以透明地处理它。如果你 从非浏览器客户端获取这些,确保它可以解码 br (curl 需要 --compressed, 或者将压缩字节写入磁盘)。

// Match the wasm version to your installed @myriaddreamin/typst-ts-web-compiler version
const typst_version = '0.7.0'

const generator = await init({
    wasm_url: `https://assets.paper.bible/typst/${typst_version}/typst_ts_web_compiler_bg.wasm`,
    renderer_wasm_url: `https://assets.paper.bible/typst/${typst_version}/typst_ts_renderer_bg.wasm`,
    assets_prefix: 'https://assets.paper.bible/',
    fonts_prefix: 'https://assets.paper.bible/fonts',
})

而要使用self-host,请在origin上的任何前缀下提供相同的布局:

  • WASM -使用Vite,直接从安装的包中导入url(没有托管) 需要): import wasm_url from '@myriaddreamin/typst-ts-web-compiler/pkg/typst_ts_web_compiler_bg.wasm?url' (同样 对于渲染器)。没有捆绑器,从 node_modules 到你的 public/ dir。

  • <assets_prefix>/frames/ - - - - - - painted.png 和 torn.png从回购文件中抄来的 assets/frames/. (backgrounds/ 仅当您的UI提供建议时才需要 背景图片-见下文。)

  • <fonts_prefix>/ -字体树(manifest.json每户一人,不得有退路 下 _noto/). 使用repo的字体配置生成它:

    curl -O https://raw.githubusercontent.com/gracious-tech/bookcover/main/font_config.json
    npx typst-fonts-download --fonts public/fonts --config font_config.json
    

内置的背景

内置背景文件的文件名(例如: beach.jpg)是它的ID——嵌入协议发送一个内置的 作为 bg_image_builtin 没有字节。将其连接到其中任何一个以获取副本:

  • https://assets.paper.bible/backgrounds/<name> -最终版本的原版(平均2.8MB) 输出
  • https://assets.paper.bible/backgrounds/previews_2700/<name> (BG_PREVIEW_DIR) - ~ 380kb, 足以在屏幕上预览一个6x9”的封面
  • https://assets.paper.bible/backgrounds/previews_800/<name> -大缩略图
  • https://assets.paper.bible/backgrounds/thumbnails/<name> - 160x120拾荒砖

将文件名传递给 generate() 作为 image_builtin 无论你渲染哪个副本,都是自动的 文本颜色来自其烘烤的颜色数据,预览与最终输出完全匹配。 get_builtin_bg_regions(name) 返回相同的数据,和 get_builtin_bg(name) 添加了 原始像素大小(用于分辨率检查而无需下载)。

CoverGenerator.generate(options): Promise<GenerateResult>

interface GenerateOptions {
    schema: unknown               // raw schema (see above)
    image?: Blob                  // background image
    // Filename of the built-in background `image` is a copy of — colors come from its baked data
    image_builtin?: string
    // Shrink `image` to at most this many pixels per inch of cover before compiling (previews)
    image_max_dpi?: number
    format?: 'pdf'|'svg'|'png'   // default: 'pdf'
    ppi?: number                  // PNG resolution, default: 144
    split?: boolean               // include split panels in result
    pdf_pages?: boolean           // PDF: front and back pages (no spine), bleed all round
    custom_fonts?: Uint8Array[]   // raw TTF bytes for custom fonts
}

interface GenerateResult {
    data: Uint8Array | string     // full cover (bytes for PDF/PNG, string for SVG)
    split?: SplitResult<Uint8Array> | SplitResult<string>
}

CoverGenerator.destroy()

撤销blob url以释放内存。当不再需要实例时调用。

例子

import {init} from 'bookcover-web'

const generator = await init({
    wasm_url: 'https://assets.paper.bible/typst/0.7.0/typst_ts_web_compiler_bg.wasm',
    renderer_wasm_url: 'https://assets.paper.bible/typst/0.7.0/typst_ts_renderer_bg.wasm',
    assets_prefix: 'https://assets.paper.bible/',
    fonts_prefix: 'https://assets.paper.bible/fonts',
})

const result = await generator.generate({
    schema: {title1: 'My Book', author: 'Jane Doe', /* ... */},
    format: 'svg',
    split: true,
})

// result.data is the full SVG string
// result.split.front / .back / .spine are individual panel SVGs

版本与发布记录

bookcover-web 共发布 28 个版本,首次发布于 2026/07/16;过去 12 个月发布了 28 个版本。

版本发布日期距今
0.24.0latest2026/11/03今天
0.23.02026/10/267 天前
0.22.32026/10/2311 天前
0.22.22026/10/2212 天前
0.22.12026/10/2212 天前
0.22.02026/10/2212 天前

关于 bookcover-web 的常见问题

bookcover-web 是什么?

生成印刷就绪的书籍封面。部分 封套 一代系统。

如何安装 bookcover-web?

在项目目录运行 npm install bookcover-web,或使用 pnpm add / yarn add / bun add。

bookcover-web 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

bookcover-web 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

bookcover-web 可以商用吗?

bookcover-web 使用 MIT-0 许可。声明的许可为 MIT-0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

bookcover-web 还在维护吗?

最新版本 0.24.0 发布于 2026/11/03(今天);至今共发布 28 个版本,其中过去 12 个月发布了 28 个;首次发布于 2026/07/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。