Search code examples
flutterdartepub

Get text content from an epub file flutter


I am trying to extract text content from an epub file but unable to do so. I have tried converting it to bytes and then read it but it's not returning a proper text. I can't find any library that helps me do so. I only need the text file from the epub file so that I can read it with text to speech.


Solution

  • I did it using the epubx package

    Here is the complete code

          File _epubFile = File(file.path);
          final contents = await _epubFile.readAsBytes();
          EpubBookRef epub = await EpubReader.openBook(contents.toList());
          var cont = await EpubReader.readTextContentFiles(epub.Content!.Html!);
          List<String> htmlList = [];
          for (var value in cont.values) {
            htmlList.add(value.Content!);
          }
          var doc = parse(htmlList.join());
          final String parsedString = parse(doc.body!.text).documentElement!.text;
    
          return parsedString;