Skip to content

元素系统

elements 数组定义 PDF 中的内容,元素按顺序渲染。

可用元素类型

text - 文本

json
{
  "type": "text",
  "content": "这是文本内容",
  "style": "title"
}

支持 Jinja2 模板变量:

json
{
  "type": "text",
  "content": "报告日期: {{metadata.title}}",
  "style": "body"
}

table - 表格

json
{
  "type": "table",
  "dataSource": "sales_data",
  "columns": ["产品", "销量", "金额"],
  "style": "tableStyle",
  "columnWidths": [2, 1.5, 1.5]
}
属性说明
dataSource数据源名称
columns显示的列名列表
style引用的表格样式
columnWidths列宽比例
header是否显示表头(默认 true)

chart - 图表

json
{
  "type": "chart",
  "chartType": "bar",
  "dataSource": "sales_data",
  "xAxis": "产品",
  "yAxis": "销量",
  "title": "销量对比",
  "width": 6,
  "height": 4
}

支持的 chartTypebarlinepiescatterarea

属性说明
chartType图表类型
dataSource数据源名称
xAxisX 轴字段
yAxisY 轴字段(可为数组)
title图表标题
width宽度(英寸)
height高度(英寸)
grid是否显示网格
showLegend是否显示图例

image - 图片

json
{
  "type": "image",
  "path": "images/logo.png",
  "width": 200,
  "alignment": "center",
  "keepAspectRatio": true
}

spacer - 空白间隔

json
{
  "type": "spacer",
  "height": 0.5
}

pagebreak - 分页符

json
{
  "type": "pagebreak"
}

list - 列表

json
{
  "type": "list",
  "items": ["项目一", "项目二", "项目三"],
  "style": "body"
}

元素组合示例

json
{
  "elements": [
    {"type": "text", "content": "销售报告", "style": "title"},
    {"type": "spacer", "height": 0.5},
    {"type": "table", "dataSource": "sales", "style": "table1"},
    {"type": "spacer", "height": 0.5},
    {"type": "chart", "chartType": "bar", "dataSource": "sales", "xAxis": "产品", "yAxis": "销量"},
    {"type": "pagebreak"},
    {"type": "image", "path": "images/chart.png", "width": 400}
  ]
}

下一步

Released under the MIT License.