docx_creator 1.0.0
docx_creator: ^1.0.0 copied to clipboard
A developer-first Dart package for creating professional DOCX documents with fluent API, Markdown/HTML parsing, and comprehensive formatting.
docx_creator #
A developer-first Dart package for creating professional DOCX documents with a fluent API, Markdown/HTML parsing, and comprehensive formatting options.
β¨ Features #
- π Fluent API - Chain methods for clean, readable code
- π Full Formatting - Bold, italic, colors, superscript, and more
- π Tables - Multiple styles (grid, zebra, professional)
- π Lists - Bullet and numbered with custom styling
- π¨ Colors - Predefined + custom hex colors
- π Headers/Footers - Page numbers, styled text
- πΌοΈ Images - Embed images with alignment
- π Parsers - Convert Markdown & HTML to DOCX
- π Page Backgrounds - Custom background colors
π¦ Installation #
dependencies:
docx_creator: ^1.0.0
π Quick Start #
import 'package:docx_creator/docx_creator.dart';
void main() async {
final doc = docx()
.h1('My Document')
.p('Hello, World!')
.bullet(['Feature 1', 'Feature 2', 'Feature 3'])
.table([
['Name', 'Score'],
['Alice', '95'],
['Bob', '87'],
])
.build();
await DocxExporter().exportToFile(doc, 'output.docx');
}
π API Reference #
Document Builder #
| Method | Description |
|---|---|
.h1(text) |
Heading level 1 |
.h2(text) |
Heading level 2 |
.h3(text) |
Heading level 3 |
.p(text) |
Paragraph |
.bullet([...]) |
Bullet list |
.numbered([...]) |
Numbered list |
.table(data) |
Table from 2D array |
.quote(text) |
Blockquote |
.code(text) |
Code block |
.hr() |
Horizontal rule / divider |
.divider() |
Divider (alias for hr) |
.pageBreak() |
Page break |
.section(...) |
Page settings |
.build() |
Build document |
Text Styling #
DocxText('Normal text')
DocxText.bold('Bold text')
DocxText.italic('Italic text')
DocxText.underline('Underlined')
DocxText.strike('Strikethrough')
DocxText.superscript('Β²')
DocxText.subscript('n')
DocxText.code('inline code')
DocxText.link('URL', href: 'https://...')
DocxText('Custom', color: DocxColor('#4285F4'))
Colors #
// Predefined
DocxColor.red
DocxColor.blue
DocxColor.green
// Custom hex
DocxColor('#FF5722')
DocxColor('4285F4')
Tables #
.table(data, style: DocxTableStyle.grid)
.table(data, style: DocxTableStyle.zebra)
.table(data, style: DocxTableStyle.professional)
.table(data, style: DocxTableStyle.plain)
Headers & Footers #
.section(
header: DocxHeader.text('My Document'),
footer: DocxFooter.pageNumbers(),
backgroundColor: DocxColor('#F5F5F5'),
)
Parsing #
// From Markdown
final nodes = DocxParser.fromMarkdown('''
# Title
This is **bold** and *italic*.
- Item 1
- Item 2
''');
// From HTML
final nodes = DocxParser.fromHtml('''
<h1>Title</h1>
<p><strong>Bold</strong> text</p>
''');
// Add to document
for (var node in nodes) {
builder.add(node);
}
π Complete Example #
import 'package:docx_creator/docx_creator.dart';
void main() async {
final doc = docx()
.section(
header: DocxHeader.styled('Report', color: DocxColor.blue, bold: true),
footer: DocxFooter.pageNumbers(),
backgroundColor: DocxColor('#FAFAFA'),
)
.h1('Annual Report 2024')
.p('Executive summary with key findings.')
.h2('Highlights')
.bullet([
'Revenue increased 25%',
'Customer base grew 40%',
'Launched 3 new products',
])
.h2('Financial Summary')
.table([
['Quarter', 'Revenue', 'Growth'],
['Q1', '\$1.2M', '+15%'],
['Q2', '\$1.5M', '+25%'],
['Q3', '\$1.8M', '+20%'],
['Q4', '\$2.1M', '+17%'],
], style: DocxTableStyle.professional)
.pageBreak()
.h1('Appendix')
.paragraph(DocxParagraph(children: [
DocxText('For questions, contact '),
DocxText.link('support@company.com', href: 'mailto:support@company.com'),
]))
.build();
await DocxExporter().exportToFile(doc, 'annual_report.docx');
print('Document created!');
}
π§ Advanced Usage #
Rich Text Paragraphs #
DocxParagraph(children: [
DocxText('Normal '),
DocxText.bold('bold '),
DocxText('and '),
DocxText('colored', color: DocxColor.red),
])
Custom Table Styling #
DocxTable.fromData(data, style: DocxTableStyle(
border: DocxBorder.double,
headerFill: '4472C4',
evenRowFill: 'F5F5F5',
cellPadding: 150,
))
Images #
DocxImage(
bytes: imageBytes,
extension: 'png',
width: 400,
height: 300,
align: DocxAlign.center,
altText: 'Company Logo',
)
π Supported Elements #
| Element | Status |
|---|---|
| Paragraphs | β |
| Headings (H1-H6) | β |
| Bold/Italic/Underline | β |
| Superscript/Subscript | β |
| Colors (predefined + hex) | β |
| Bullet Lists | β |
| Numbered Lists | β |
| Tables | β |
| Images | β |
| Headers/Footers | β |
| Page Numbers | β |
| Page Breaks | β |
| Horizontal Rules | β |
| Blockquotes | β |
| Code Blocks | β |
| Hyperlinks | β |
| Page Backgrounds | β |
| Markdown Parsing | β |
| HTML Parsing | β |
π License #
MIT License - see LICENSE for details.
π€ Contributing #
Contributions are welcome! Please read our contributing guidelines before submitting PRs.
Made with β€οΈ for the Dart community