Get text content from an epub file flutter - 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.

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;

Related

How do i open srt file on flutter?

I am trying to load subtitle to a video using the flutter video player package it works good for short files but it stopped as the file get bigger
I trayed subtitle_wrapper package but it has many bugs
Future<ClosedCaptionFile> getSubtitle(String url) async {
final data = NetworkAssetBundle(Uri(path: url));
final newdata = await data.load(url);
String fileContents = getStringFromBytes(newdata);
return captionFile = SubRipCaptionFile(fileContents);
}
this is getStringFromBytes function
getStringFromBytes(ByteData data) { final buffer = data.buffer;
var list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
return utf8.decode(list); }
it wasn't the size after all I tested on some srt files that have a blank space for some duration and the flutter converter do a check on every element if the length is<3 it break on get out of the loop

Store image uploaded by user into Flutter Web as an actual .jpg file

I am using the flutter_web_image_picker package to allow the user to select -and then upload to Firebase- an image.
However, the package returns an image widget, which I can display, but I cannot upload to Firebase. Therefore, I am trying to read the package's code and update it to fit my needs.
In general, I think the packages main functionalities are:
It gets the file
//...
final reader = html.FileReader();
reader.readAsDataUrl(input.files[0]);
await reader.onLoad.first;
final encoded = reader.result as String;
Then it 'strippes' it
final stripped = encoded.replaceFirst(RegExp(r'data:image/[^;]+;base64,'), '');
final imageName = input.files?.first?.name;
//...
To finally return it as a Widget:
final imageName = imageName;
final imageData = base64.decode(stripped);
return Image.memory(imageData, semanticLabel: imageName);
As I said, it works perfectly, however, I need to adapt it to my needs:
I would like to get the image as a .jpg file so that I can upload it to Firebase.
Is any of the variables above the actual .jpg file? Is there any transformation that I should perform to get a .jpg file?
Thanks!
I based my answer on this post.
Basically, on the flutter_web_image_picker package, before the code I posted, there were a few lines that get an actual html file:
final html.FileUploadInputElement input = html.FileUploadInputElement();
input..accept = 'image/*';
input.click();
await input.onChange.first;
if (input.files.isEmpty) return null;
Then using firebase's pacakge, I uploaded the image as follow:
import 'package:firebase/firebase.dart' as fb;
fb.StorageReference storageRef = fb.storage().ref('myLocation/filename.jpg');
fb.UploadTaskSnapshot uploadTaskSnapshot = await storageRef.put(input.files[0]).future;
Uri imageUri = await uploadTaskSnapshot.ref.getDownloadURL();
return imageUri;

How to convert a PDF file to an image with Flutter?

How can I convert a PDF file into an image with Flutter?
I want to print the image to an ESC/POS printer using esc_pos_printer. This package won't accept PDFImage, it needs to be a Flutter Image.
I see plenty of PHP plugins that do this but nothing for Flutter.
edit: There is an answer to another question here which shows some code to decode an image from "pdf64" but I can't figure out exactly what "pdf64" is.
I created a PDF from html using flutter_html_to_pdflike this:
Directory appDocDir = await getApplicationDocumentsDirectory();
var targetPath = appDocDir.path;
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
htmlContent, targetPath, targetFileName);
generatedPdfFilePath = generatedPdfFile.path;
Now I need to know how to create a Flutter Image from that PDF or the bytecode to send raw to the printer.
You can use https://pub.dev/packages/printing:
await for (var page in Printing.raster(document)) {
final image = page.asImage();
...
}
This plugin can also convert your Html to Pdf with this:
final pdf = await Printing.convertHtml(
format: PdfPageFormat.a4,
html: '<html><body><p>Hello!</p></body></html>',
));

How can I parse the contents of a PDF file in dart?

Is there a way I can parse the contents of PDF files into widgets or something more understandable or perhaps into something that can be modified and again converted to PDF format?
You can use pdf library to achieve this task
import 'dart:io';
import 'package:pdf/pdf.dart' as pw;
Import like this and then use following method to extract text from pdf
String loadPdf({required String pdfPath}) {
String text = "";
// Load the PDF file
var file = File(pdfPath);
var bytes = file.readAsBytesSync();
// Decode the PDF file
var pdf = pw.PdfDocument.fromBytes(bytes);
// Extract the text from the PDF
for (var page in pdf.pages) {
var pageText = page.getText();
text += pageText;
}
return text;
}

How to upload a specific type of file with FileUploadInputElement in Flutter for Web

I was trying to create a simple file uploader for a project in flutter, meant to upload images on a Firebase storage bucket, but I can't find a way to select the file type.
I've read the not-so-exhaustive documentation of Dart, but I didn't find anything similar to what I'm trying to do.
static Future<Uri> showUploadDialog(
firebase.StorageReference filePath, String fileName) async {
Completer completer = Completer();
InputElement uploadInput = FileUploadInputElement();
uploadInput.click();
uploadInput.onChange.listen(
(changeEvent) {
final file = uploadInput.files.first;
final reader = FileReader();
reader.onLoadEnd.listen(
(loadEndEvent) async {
debugPrint("Selected the file to be uploaded.");
// Here we handle the file.
completer.complete(uploadFile(file, filePath, fileName));
},
);
reader.readAsDataUrl(file);
},
);
return await completer.future;
}
My aim is to be able to see a FileUploadInputElement which doesn't allow me to upload a file different from an image file (with an image specific extension).
You need to set the element's accept attribute (before simulating the click on it).
For example, in order to only accept png and jpg files, you would neet to set it as .png,.jpg:
InputElement uploadInput = FileUploadInputElement();
uploadInput.accept = '.png,.jpg'
uploadInput.click();
The syntax for the accept attribute is the one specified for the HTML input accept attribute:
<input accept="file_extension|audio/*|video/*|image/*|media_type">
More details can be found online, such as in w3schools: