搜索插件、代码仓与 APP 分类

插件详情v0.4.0

docx_template

一个 Docx 模板引擎。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2150
喜欢数
107
Pub 分数
130

安装插件

flutter pub add docx_template

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Dart 包,通过模板语法实现 DOCX 文件的动态生成。它允许用户在 Word 文档中定义占位符,并在运行时用实际数据替换它们,非常适合程序化创建报告、发票或证书。该引擎支持模板中的富文本格式、图片和表格。专为跨平台使用而设计,可在 Android、iOS、Web、macOS、Windows 和 Linux 上无缝运行。专为需要轻量级、灵活解决方案以实现文档自动化的开发者设计,无需依赖外部服务。

适合场景

  • 生成动态报告
  • 创建可打印的发票
  • 自动化证书创建
  • 生成标准化文档
  • 将数据导出为 Word 格式

主要特点

  • 基于模板的 DOCX 生成
  • 对占位符和变量的支持
  • 富文本和图像嵌入
  • 跨平台兼容性
  • 轻量且无依赖
  • 轻松集成到Flutter应用

适合对象

  • Flutter 开发人员构建文档密集型应用
  • 需要生成报告的业务应用
  • 用于自动化输出的教育工具
  • Teams automating paperwork workflows
  • 寻求离线文档创建的开发者

使用建议

  • 仅限 DOCX 格式(不包括 ODT 或其他格式)
  • 复杂的布局需要仔细的模板结构
  • 不支持宏等高级 Word 功能的内置支持
  • 最适合静态或半动态内容
  • 性能可能因非常大的模板而有所不同

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

docx_template_dart

A Docx template engine

Generates docx from template file.

In order to use the library, you need to learn how to insert and edit content control tags in Microsoft Word. LibreOffice and other office programs are not supported because they do not have a content control tag system.

To do this, go to the project repository and download the template file template.docx

Then open the file in Microsoft Word, go to the program settings and enable Developer mode. You may need to restart the program. After that, the developer tab will be available.

In order to make the tags visible, you need to enable "design mode" on the developer tab, to view and edit the tag parameters, you need select tag, then click "properties" button on developer tab.

To create a tag, you need to use the "Aa" buttons on the developer tab, clicking on the button will open a window where: the TAG field can be one from the following list: list, table, text, plain, img the TITLE field is the name of the tag, which will be passed to constructors of the Content classes

For example: If we set the tag equal to 'list' and the title equal to 'cars', then we must use the corresponding ListContent ('cars', [*here contents*])

For the 'text' tag and the title equal to 'block_name' we use: TextContent('block_name', 'Example text') etc.

List of supported tags:

  • list - a list, which can contain the following tags: text, plain
  • table - table row
  • text - a simple text field
  • plain - a block that can contain text, tables, images, lists, which can be repeated several times if you wrap it in a list tag
  • img - block with a picture, can be used in text or inside tables

Example

  final f = File("template.docx");
  final docx = await DocxTemplate.fromBytes(await f.readAsBytes());

  /* 
    Or in the case of Flutter, you can use rootBundle.load, then get bytes
    
    final data = await rootBundle.load('lib/assets/users.docx');
    final bytes = data.buffer.asUint8List();

    final docx = await DocxTemplate.fromBytes(bytes);
  */

  // Load test image for inserting in docx
  final testFileContent = await File('test.jpg').readAsBytes();

  final listNormal = ['Foo', 'Bar', 'Baz'];
  final listBold = ['ooF', 'raB', 'zaB'];

  final contentList = <Content>[];

  final b = listBold.iterator;
  for (var n in listNormal) {
    b.moveNext();

    final c = PlainContent("value")
      ..add(TextContent("normal", n))
      ..add(TextContent("bold", b.current));
    contentList.add(c);
  }

  Content c = Content();
  c
    ..add(TextContent("docname", "Simple docname"))
    ..add(TextContent("passport", "Passport NE0323 4456673"))
    ..add(TableContent("table", [
      RowContent()
        ..add(TextContent("key1", "Paul"))
        ..add(TextContent("key2", "Viberg"))
        ..add(TextContent("key3", "Engineer"))
        ..add(ImageContent('img', testFileContent)),
      RowContent()
        ..add(TextContent("key1", "Alex"))
        ..add(TextContent("key2", "Houser"))
        ..add(TextContent("key3", "CEO & Founder"))
        ..add(ListContent("tablelist", [
          TextContent("value", "Mercedes-Benz C-Class S205"),
          TextContent("value", "Lexus LX 570")
        ]))
        ..add(ImageContent('img', testFileContent))
    ]))
    ..add(ListContent("list", [
      TextContent("value", "Engine")
        ..add(ListContent("listnested", contentList)),
      TextContent("value", "Gearbox"),
      TextContent("value", "Chassis")
    ]))
    ..add(ListContent("plainlist", [
      PlainContent("plainview")
        ..add(TableContent("table", [
          RowContent()
            ..add(TextContent("key1", "Paul"))
            ..add(TextContent("key2", "Viberg"))
            ..add(TextContent("key3", "Engineer")),
          RowContent()
            ..add(TextContent("key1", "Alex"))
            ..add(TextContent("key2", "Houser"))
            ..add(TextContent("key3", "CEO & Founder"))
            ..add(ListContent("tablelist", [
              TextContent("value", "Mercedes-Benz C-Class S205"),
              TextContent("value", "Lexus LX 570")
            ]))
        ])),
      PlainContent("plainview")
        ..add(TableContent("table", [
          RowContent()
            ..add(TextContent("key1", "Nathan"))
            ..add(TextContent("key2", "Anceaux"))
            ..add(TextContent("key3", "Music artist"))
            ..add(ListContent(
                "tablelist", [TextContent("value", "Peugeot 508")])),
          RowContent()
            ..add(TextContent("key1", "Louis"))
            ..add(TextContent("key2", "Houplain"))
            ..add(TextContent("key3", "Music artist"))
            ..add(ListContent("tablelist", [
              TextContent("value", "Range Rover Velar"),
              TextContent("value", "Lada Vesta SW Sport")
            ]))
        ])),
    ]))
    ..add(ListContent("multilineList", [
      PlainContent("multilinePlain")
        ..add(TextContent('multilineText', 'line 1')),
      PlainContent("multilinePlain")
        ..add(TextContent('multilineText', 'line 2')),
      PlainContent("multilinePlain")
        ..add(TextContent('multilineText', 'line 3'))
    ]))
    ..add(TextContent('multilineText2', 'line 1\nline 2\n line 3'))
    ..add(ImageContent('img', testFileContent));

  final d = await docx.generate(c);
  final of = File('generated.docx');
  if (d != null) await of.writeAsBytes(d);

DocxTemplate.generate takes two additional parameters:

  1. tagPolicy set rules to remove content control tags from the document
  2. imagePolicy Where ImagePolicy.remove - deletes the image if the ImageContent object is not provided ImagePolicy.save - leaves the original images from the template if the ImageContent object is not specified

所属分类

依赖

依赖清单 4 项

源码仓库

使用此插件的项目

暂无相关数据。