FLUTTER ECOSYSTEM

erykkruk/docs_gee

纯 Dart 库,用于生成无原生依赖的 DOCX(Word)文件。支持段落、标题、列表、表格、文本格式和分页。

docs_gee 项目封面
Stars
4
Forks
2
最近推送(UTC)
2026年8月24日
项目状态
未归档
erykkruk GitHub avatar
GITHUB User

erykkruk ↗

Codigee
语言Dart

技术话题

此仓库发布的插件

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

展开 / 收起项目 README

docs_gee

pub package likes popularity License: MIT

A pure Dart library for generating Microsoft Word (DOCX) and PDF documents. Create professional documents programmatically with rich formatting, tables, lists, and more - all from a single document model. Created and supported by Codigee.

If you find this package useful, please consider giving it a star on GitHub and a like on pub.dev. It helps the package grow and stay maintained!

Documentation

Full hosted documentation is available at codigee.com/open-source/docs-gee:

Why docs_gee?

  • Pure Dart - No native dependencies, works everywhere Dart runs
  • Dual Format - Generate both DOCX and PDF from the same document model
  • Cross-Platform - iOS, Android, Web, macOS, Windows, Linux
  • Lightweight - Only one dependency (archive for ZIP)
  • Simple API - Intuitive document builder pattern

Features

Feature DOCX PDF
Text formatting (bold, italic, underline, strikethrough)
Text colors & highlighting
Headings (H1-H4)
Paragraph styles (subtitle, caption, quote, code, footnote)
Text alignment (left, center, right, justify)
Bullet & numbered lists
Nested lists (up to 9 levels)
Tables with borders & colors
Per-cell border control
Page breaks
Line breaks (soft return) -
Hyperlinks (external URLs) -
Internal links (bookmarks) -
Table of Contents -
Document metadata
Custom fonts
Extended characters (German, French)
Polish characters Partial*
Emoji support -

*PDF supports Ó/ó natively; other Polish characters (ą, ę, ć, etc.) fall back to ASCII equivalents due to font encoding limitations.

Installation

dependencies:
  docs_gee: ^1.2.2
dart pub add docs_gee
# or
flutter pub add docs_gee

Quick Start

import 'dart:io';
import 'package:docs_gee/docs_gee.dart';

void main() {
  // Create document
  final doc = Document(title: 'My Report', author: 'John Doe');

  // Add content
  doc.addParagraph(Paragraph.heading('Quarterly Report', level: 1));
  doc.addParagraph(Paragraph.text('This report summarizes Q4 performance.'));

  // Add a table
  doc.addTable(Table(
    rows: [
      TableRow(cells: [
        TableCell.text('Metric', backgroundColor: 'E0E0E0'),
        TableCell.text('Value', backgroundColor: 'E0E0E0'),
      ]),
      TableRow(cells: [
        TableCell.text('Revenue'),
        TableCell.text('\$1.2M', alignment: Alignment.right),
      ]),
    ],
  ));

  // Generate both formats
  File('report.docx').writeAsBytesSync(DocxGenerator().generate(doc));
  File('report.pdf').writeAsBytesSync(PdfGenerator().generate(doc));
}

Usage Examples

Rich Text Formatting
doc.addParagraph(Paragraph(
  runs: [
    TextRun('Normal, '),
    TextRun('bold, ', bold: true),
    TextRun('italic, ', italic: true),
    TextRun('colored', color: 'FF0000'),
  ],
));
Lists
// Bullet list
doc.addParagraph(Paragraph.bulletItem('First item'));
doc.addParagraph(Paragraph.bulletItem('Second item'));

// Numbered list
doc.addParagraph(Paragraph.numberedItem('Step one'));
doc.addParagraph(Paragraph.numberedItem('Step two'));

// Nested list
doc.addParagraph(Paragraph.bulletItem('Parent'));
doc.addParagraph(Paragraph.bulletItem('Child', indentLevel: 1));
Tables
doc.addTable(Table(
  borders: TableBorders.all(),
  rows: [
    TableRow(cells: [
      TableCell.text('Name', backgroundColor: 'CCCCCC'),
      TableCell.text('Score', backgroundColor: 'CCCCCC'),
    ]),
    TableRow(cells: [
      TableCell.text('Alice'),
      TableCell.text('95', alignment: Alignment.right),
    ]),
  ],
));
Cell Borders

Override table-level borders on individual cells:

doc.addTable(Table(
  borders: const TableBorders.all(),
  rows: [
    TableRow(cells: [
      // Cell with custom red borders
      TableCell.text('Alert', borders: const CellBorders.all(color: 'FF0000', size: 8)),
      TableCell.text('Normal cell'),
    ]),
    TableRow(cells: [
      // Cell with only bottom border
      TableCell.text('Underlined', borders: const CellBorders.bottom()),
      // Cell with no borders (overrides table borders)
      TableCell.text('Clean', borders: const CellBorders.none()),
    ]),
  ],
));

// Selective sides with different styles
TableCell(
  paragraphs: [Paragraph.text('Custom')],
  borders: const CellBorders(
    top: Border(color: 'FF0000', size: 8, style: BorderStyle.double),
    bottom: Border(color: '0000FF'),
  ),
);
Semantic Styles
doc.addParagraph(Paragraph.heading('Title', level: 1));
doc.addParagraph(Paragraph.subtitle('Document subtitle'));
doc.addParagraph(Paragraph.quote('A famous quote...'));
doc.addParagraph(Paragraph.codeBlock('const x = 42;'));
doc.addParagraph(Paragraph.caption('Figure 1: Chart'));
Page Breaks
doc.addParagraph(Paragraph.heading(
  'New Chapter',
  level: 1,
  pageBreakBefore: true,
));
Line Breaks (Soft Return) - DOCX only

Line breaks allow multiple lines within a single paragraph (like Shift+Enter in Word).

// Using \n in text (automatic conversion)
doc.addParagraph(Paragraph.text('Line 1\nLine 2\nLine 3'));

// Using explicit line break runs (for different formatting per line)
doc.addParagraph(Paragraph(
  runs: [
    TextRun('Bold line', bold: true),
    TextRun.lineBreak(),
    TextRun('Normal line'),
    TextRun.lineBreak(),
    TextRun('Italic line', italic: true),
  ],
));
Hyperlinks
// External link
doc.addParagraph(Paragraph(
  runs: [
    TextRun('Visit '),
    TextRun('our website', hyperlink: 'https://example.com'),
    TextRun(' for more info.'),
  ],
));
Internal Links (Bookmarks)
// Create a bookmark
doc.addParagraph(Paragraph.heading(
  'Chapter 1: Introduction',
  level: 1,
  bookmarkName: 'chapter1',
));

// Link to the bookmark
doc.addParagraph(Paragraph(
  runs: [
    TextRun('Go to '),
    TextRun('Chapter 1', bookmarkRef: 'chapter1'),
  ],
));
Table of Contents
// Enable automatic Table of Contents
final doc = Document(
  title: 'My Document',
  includeTableOfContents: true,
  tocTitle: 'Contents',
  tocMaxLevel: 3,  // Include Heading 1-3
);

doc.addParagraph(Paragraph.heading('Introduction', level: 1));
doc.addParagraph(Paragraph.heading('Getting Started', level: 2));
// TOC will be auto-generated with links to these headings

Platform Support

Platform Support
Android
iOS
Web
macOS
Windows
Linux
Web Usage
import 'dart:html' as html;

void downloadDocument(Uint8List bytes, String filename) {
  final blob = html.Blob([bytes]);
  final url = html.Url.createObjectUrlFromBlob(blob);
  html.AnchorElement(href: url)
    ..setAttribute('download', filename)
    ..click();
  html.Url.revokeObjectUrl(url);
}
Mobile Usage
import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';

Future<void> shareDocument(Uint8List bytes) async {
  final dir = await getTemporaryDirectory();
  final file = File('${dir.path}/document.docx');
  await file.writeAsBytes(bytes);
  await Share.shareXFiles([XFile(file.path)]);
}

API Reference

Main Classes
Class Description
Document / DocxDocument Document container with metadata
Paragraph / DocxParagraph Paragraph with text runs and styling
TextRun / DocxRun Text segment with formatting
Table / DocxTable Table with rows and borders
TableRow / DocxTableRow Table row with cells
TableCell / DocxTableCell Table cell with content
DocxGenerator Generates DOCX bytes
PdfGenerator Generates PDF bytes
Paragraph Styles
Style Method
Normal text Paragraph.text('...')
Heading 1-4 Paragraph.heading('...', level: 1)
Subtitle Paragraph.subtitle('...')
Caption Paragraph.caption('...')
Quote Paragraph.quote('...')
Code block Paragraph.codeBlock('...')
Footnote Paragraph.footnote('...')
Bullet list Paragraph.bulletItem('...')
Dash list Paragraph.dashItem('...')
Numbered list Paragraph.numberedItem('...')
Alpha list Paragraph.alphaItem('...')
Roman list Paragraph.romanItem('...')
Text Formatting
Property Type Description
bold bool Bold text
italic bool Italic text
underline bool Underlined text
strikethrough bool Strikethrough text
color String Hex color (e.g., 'FF0000')
backgroundColor String Highlight color
hyperlink String? External URL link
bookmarkRef String? Internal bookmark reference
isLineBreak bool Line break (use TextRun.lineBreak())

Compatibility

Generated documents are compatible with:

  • Microsoft Word 2007+
  • Google Docs
  • LibreOffice Writer
  • Apple Pages
  • WPS Office
  • Any OOXML-compatible application

Requirements

  • Dart SDK: >=3.0.0 <4.0.0
  • Flutter: >=3.0.0 (if using with Flutter)

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests on GitHub.

License

MIT License - see LICENSE for details.

Codigee - Best Flutter Experts


Made with ❤️ by Codigee