PavelS0 / docx_template_dart

A Docx template engine
Apache License 2.0
40 stars 20 forks source link

If the sample code is not working try this #39

Open burakcanyildirim opened 1 year ago

burakcanyildirim commented 1 year ago
   final data = await rootBundle.load('assets/template.docx');
    final bytes = data.buffer.asUint8List();
    final docx = await DocxTemplate.fromBytes(bytes);

    ByteData byteData = await rootBundle.load('assets/logo.png');
    Uint8List testFileContent = byteData.buffer.asUint8List();

    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", "Dodument Name"))
    ..add(TextContent("passport", "Passport NE0323 4456673"))
    ..add(TableContent("table", [
    RowContent()
    ..add(TextContent("key1", "Viberg"))
    ..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 dbBytes = await rootBundle.load('assets/generated.docx');
    final of = await writeToFile(dbBytes);
    if (d != null) await of.writeAsBytes(d);

And use this metod:

  Future<File> writeToFile(ByteData data) async {
    final buffer = data.buffer;
    Directory tempDir = await getTemporaryDirectory();
    String tempPath = tempDir.path;
    var filePath = tempPath + '/new.docx';
    print(filePath);
    final bytes = data.buffer.asUint8List();
    return new File(filePath).writeAsBytes(bytes);
  }
AriesHoo commented 1 year ago

hi, it can save as docx ?

burakcanyildirim commented 1 year ago

Yes but paste this code in pubspec.yaml: dependency_overrides: archive: 3.3.0 https://github.com/PavelS0/docx_template_dart/issues/37#issuecomment-1330462403

Fiat2U commented 1 year ago

Please try https://github.com/Fiat2U/docx_template_flutter_sample to see a working sample.