react-pdf by vercel-labs/json-render
npx skills add https://github.com/vercel-labs/json-render --skill react-pdf一个 React PDF 渲染器,使用 @react-pdf/renderer 从 JSON 规范生成 PDF 文档。
npm install @json-render/core @json-render/react-pdf
import { renderToBuffer } from "@json-render/react-pdf";
import type { Spec } from "@json-render/core";
const spec: Spec = {
root: "doc",
elements: {
doc: { type: "Document", props: { title: "Invoice" }, children: ["page"] },
page: {
type: "Page",
props: { size: "A4" },
children: ["heading", "table"],
},
heading: {
type: "Heading",
props: { text: "Invoice #1234", level: "h1" },
children: [],
},
table: {
type: "Table",
props: {
columns: [
{ header: "Item", width: "60%" },
{ header: "Price", width: "40%", align: "right" },
],
rows: [
["Widget A", "$10.00"],
["Widget B", "$25.00"],
],
},
children: [],
},
},
};
const buffer = await renderToBuffer(spec);
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
import { renderToBuffer, renderToStream, renderToFile } from "@json-render/react-pdf";
// 内存缓冲区
const buffer = await renderToBuffer(spec);
// 可读流(可管道传输到 HTTP 响应)
const stream = await renderToStream(spec);
stream.pipe(res);
// 直接输出到文件
await renderToFile(spec, "./output.pdf");
所有渲染函数都接受一个可选的第二个参数:{ registry?, state?, handlers? }。
| 组件 | 描述 |
|---|---|
Document | 顶级 PDF 包装器(必须是根元素) |
Page | 具有尺寸(A4、LETTER)、方向、边距的页面 |
View | 通用容器(内边距、外边距、背景、边框) |
Row, Column | 具有间隙、对齐、分布方式的弹性布局 |
Heading | h1-h4 标题文本 |
Text | 正文文本(字体大小、颜色、粗细、对齐方式) |
Image | 来自 URL 或 base64 的图像 |
Link | 带有文本和 href 的超链接 |
Table | 具有类型化列和行的数据表格 |
List | 有序或无序列表 |
Divider | 水平分隔线 |
Spacer | 垂直空白空间 |
PageNumber | 当前页码和总页数 |
import { defineCatalog } from "@json-render/core";
import { schema, defineRegistry, renderToBuffer } from "@json-render/react-pdf";
import { standardComponentDefinitions } from "@json-render/react-pdf/catalog";
import { z } from "zod";
const catalog = defineCatalog(schema, {
components: {
...standardComponentDefinitions,
Badge: {
props: z.object({ label: z.string(), color: z.string().nullable() }),
slots: [],
description: "一个彩色徽章标签",
},
},
actions: {},
});
const { registry } = defineRegistry(catalog, {
components: {
Badge: ({ props }) => (
<View style={{ backgroundColor: props.color ?? "#e5e7eb", padding: 4 }}>
<Text>{props.label}</Text>
</View>
),
},
});
const buffer = await renderToBuffer(spec, { registry });
传递一个 StateStore 以完全控制状态:
import { createStateStore } from "@json-render/react-pdf";
const store = createStateStore({ invoice: { total: 100 } });
store.set("/invoice/total", 200);
导入模式和目录而不引入 React:
import { schema, standardComponentDefinitions } from "@json-render/react-pdf/server";
每周安装量
250
代码仓库
GitHub 星标
13.3K
首次出现
2026年3月7日
安全审计
安装于
codex245
cursor243
gemini-cli242
kimi-cli242
github-copilot242
amp242
React PDF renderer that generates PDF documents from JSON specs using @react-pdf/renderer.
npm install @json-render/core @json-render/react-pdf
import { renderToBuffer } from "@json-render/react-pdf";
import type { Spec } from "@json-render/core";
const spec: Spec = {
root: "doc",
elements: {
doc: { type: "Document", props: { title: "Invoice" }, children: ["page"] },
page: {
type: "Page",
props: { size: "A4" },
children: ["heading", "table"],
},
heading: {
type: "Heading",
props: { text: "Invoice #1234", level: "h1" },
children: [],
},
table: {
type: "Table",
props: {
columns: [
{ header: "Item", width: "60%" },
{ header: "Price", width: "40%", align: "right" },
],
rows: [
["Widget A", "$10.00"],
["Widget B", "$25.00"],
],
},
children: [],
},
},
};
const buffer = await renderToBuffer(spec);
import { renderToBuffer, renderToStream, renderToFile } from "@json-render/react-pdf";
// In-memory buffer
const buffer = await renderToBuffer(spec);
// Readable stream (pipe to HTTP response)
const stream = await renderToStream(spec);
stream.pipe(res);
// Direct to file
await renderToFile(spec, "./output.pdf");
All render functions accept an optional second argument: { registry?, state?, handlers? }.
| Component | Description |
|---|---|
Document | Top-level PDF wrapper (must be root) |
Page | Page with size (A4, LETTER), orientation, margins |
View | Generic container (padding, margin, background, border) |
Row, Column | Flex layout with gap, align, justify |
Heading | h1-h4 heading text |
Text |
import { defineCatalog } from "@json-render/core";
import { schema, defineRegistry, renderToBuffer } from "@json-render/react-pdf";
import { standardComponentDefinitions } from "@json-render/react-pdf/catalog";
import { z } from "zod";
const catalog = defineCatalog(schema, {
components: {
...standardComponentDefinitions,
Badge: {
props: z.object({ label: z.string(), color: z.string().nullable() }),
slots: [],
description: "A colored badge label",
},
},
actions: {},
});
const { registry } = defineRegistry(catalog, {
components: {
Badge: ({ props }) => (
<View style={{ backgroundColor: props.color ?? "#e5e7eb", padding: 4 }}>
<Text>{props.label}</Text>
</View>
),
},
});
const buffer = await renderToBuffer(spec, { registry });
Pass a StateStore for full control over state:
import { createStateStore } from "@json-render/react-pdf";
const store = createStateStore({ invoice: { total: 100 } });
store.set("/invoice/total", 200);
Import schema and catalog without pulling in React:
import { schema, standardComponentDefinitions } from "@json-render/react-pdf/server";
Weekly Installs
250
Repository
GitHub Stars
13.3K
First Seen
Mar 7, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex245
cursor243
gemini-cli242
kimi-cli242
github-copilot242
amp242
ManimGL 最佳实践指南:场景、动画、3D 与交互式开发教程
830 周安装
| Body text (fontSize, color, weight, alignment) |
Image | Image from URL or base64 |
Link | Hyperlink with text and href |
Table | Data table with typed columns and rows |
List | Ordered or unordered list |
Divider | Horizontal line separator |
Spacer | Empty vertical space |
PageNumber | Current page number and total pages |