@ag-media/react-pdf-table

中文文档
2.0.3发布于 2025/05/15MITTypeScriptESM + CJS零依赖近 30 天下载 23.6万

Declarative table generator for for the @react-pdf/renderer.

原始使用文档
文档目录8 节
  1. Getting started
  2. Basic example
  3. Beware ⚠
  4. Notes
  5. TableCell
  6. Table's tdProps and trProps
  7. weighting
  8. Limitations / Known issues

@ag-media/react-pdf-table

@ag-media/react-pdf-table is a declarative table generator for @react-pdf/renderer.

Warning
The library is specifically designed for use with @react-pdf/renderer and will not work with other react renderers.

npm version

example

Getting started

npm install --save-dev @ag-media/react-pdf-table

Basic example

import {Table, TR, TH, TD} from '@ag-media/react-pdf-table';

const Component = () => (
    <Table>
        <TH>
            <TD>Header 1</TD>
            <TD>Header 2</TD>
        </TH>
        <TR>
            <TD>Data 1</TD>
            <TD>Data 2</TD>
        </TR>
    </Table>
);

Hint: you can use the short (<TH />, <TR />, <TD />) or long (<TableHeader />, <TableRow />, <TableCell />) named imports.

Refer to the repo's Storybook for more examples.

Beware ⚠

While this library covers the usual table layouts, it's not a complete or bullet-proof solution. Because not all layouts are 100% achievable with the current react-pdf primitives, the table components need to be implemented in react-pdf (upvote this issue).

Notes

TableCell

Content in a TableCell must evaluate to a string, number, <Fragment /> or a @react-pdf/renderer component (View, Text etc.).

Table's tdProps and trProps

These two props will be passed down to all TableCells and TableRows. Can be used to style cells/rows (styles example).

weighting

Weightings can be defined once for all rows in Table's weightings prop and for each TableCell individually. TableCell's weighting props has precedence over the Table's weightings.

If weighting is not defined for a table cell then it will default to ocupy an equal amount of the remaining space.

The sum of all weightings for one line must should to 1, unless you want a row to be smaller than the table.

Limitations / Known issues

  • page wrapping may happen in unexpected ways: example

  • flex layout is used to simulate how a table looks. Because of this, it's trivial to make columns not line up.

  • border collapse

    Because there's no such thing in react-pdf, this has to be "emulated" with negative margins. This is especially painfull for non-default border styles (e.g. outer border)

  • dashed and dotted table borders may meet irregularly or overlap

版本与发布记录

@ag-media/react-pdf-table 共发布 6 个版本,首次发布于 2023/06/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.3latest2025/05/151.5 年前
2.0.22025/04/251.5 年前
2.0.12025/01/141.8 年前
2.0.02024/12/051.9 年前
1.0.12023/06/213 年前
1.0.02023/06/203 年前

关于 @ag-media/react-pdf-table 的常见问题

@ag-media/react-pdf-table 是什么?

Declarative table generator for for the @react-pdf/renderer.

如何安装 @ag-media/react-pdf-table?

在项目目录运行 npm install @ag-media/react-pdf-table,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@react-pdf/renderer(^2.0.2 || ^3.0.0 || ^4.0.0)、@react-pdf/stylesheet(^2.0.0 || ^3.0.0 || ^4.0.0 || ^5.0.0 || ^6.0.0)、react(^16.8.6 || ^17.0.0 || ^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@ag-media/react-pdf-table 支持 TypeScript 吗?

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

@ag-media/react-pdf-table 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ag-media/react-pdf-table 可以商用吗?

@ag-media/react-pdf-table 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ag-media/react-pdf-table 的下载量有多少?

根据 npm 官方下载统计,@ag-media/react-pdf-table 在 2026/10/05 – 2026/11/03 这 30 天内被下载 236,280 次,日均约 7,876 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ag-media/react-pdf-table 还在维护吗?

最新版本 2.0.3 发布于 2025/05/15(1.5 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/06/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。