0

I am in process of making a Flutter App where we can select pictures and then generate a MS word file with selected images inserted in Table. Initially I though I will generate a pdf with table and convert to word and made progress on that. I generated pdf but when I convert to word I see its getting converted as one big image and not as Table with images as I need to alter images sometimes in word. Now I am struck can anyone help with some way forward.

Bimal
  • 23
  • 2

1 Answers1

0

Try using this package docx_template

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


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



  Content c = Content();
  c
    ..add(TextContent("docname", "Simple docname"))
    ..add(TableContent("table", [
      RowContent()
        ..add(TextContent("key1", "Paul"))
        ..add(ImageContent('img', testFileContent)),
    ]))

      

  final d = await docx.generate(c);
  final of = File('generated.docx');
  if (d != null) await of.writeAsBytes(d);
Kaushik Chandru
  • 15,510
  • 2
  • 12
  • 30
  • Hi I tried the code I get the following error message FileSystemException: Cannot open file, path = 'generated.docx' (OS Error: Read-only file system, errno = 30) is it something to do with permission ? – Bimal Jul 06 '22 at 11:28
  • Yes it worked thanks for the help in flutter I Just changed the below lines. ` final d = await docx.generate(c); final dir = await getApplicationDocumentsDirectory(); final file = File('${dir.path}/generated.docx'); if (d != null) await file.writeAsBytes(d); ` – Bimal Jul 06 '22 at 12:26