Open yogesharora388 opened 7 months ago
@yogesharora388 i will add this in future release
I have tried to make it happen but unable to raise the PR as not having a permission
Need to add at top of file ('html_to_widgets.dart')
import 'dart:convert';
RegExp base64RegExp = RegExp(
r'^(?:[A-Za-z0-9+/][A-Za-z0-9+/][A-Za-z0-9+/][A-Za-z0-9+/])*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$',
);
Then just replace the method _parseImageElement with below stuff
/// Function to parse an image element and download image as bytes and return an Image widget
Future<Widget> _parseImageElement(dom.Element element) async {
final src = element.attributes["src"];
try {
if (src != null) {
if (src.startsWith("data:image")) {
// To handle a case if someone added a space after base64 string
List components = src.split("base64, ");
if (components.isEmpty) {
components = src.split("base64,");
}
if (components.length > 1) {
var base64Encoded = components[1];
Uint8List listData = base64Decode(base64Encoded);
return Image(MemoryImage(listData),
alignment: customStyles.imageAlignment);
} else {
return Text("");
}
} else if (base64RegExp.hasMatch(src)) {
Uint8List listData = base64Decode(src);
return Image(MemoryImage(listData),
alignment: customStyles.imageAlignment);
} else {
final netImage = await _saveImage(src);
return Image(MemoryImage(netImage),
alignment: customStyles.imageAlignment);
}
} else {
return Text("");
}
} catch (e) {
return Text("");
}
}
@alihassan143 Can you please take a look in above code ? I am able to export the image after making such changes.
@yogesharora388 you need to forked repo than you can able to file a PR
@alihassan143 Created a PR https://github.com/alihassan143/htmltopdfwidgets/pull/29
Hello, could you adapt the code like this, for example?
Future<Uint8List> _saveImage(String url) async {
try {
**if(url.startsWith("data:image"))
{
return (base64.decode(url.split(',')[1]));
}**
/// Download image
final Response response = await get(Uri.parse(url));
/// Get temporary directory
return response.bodyBytes;
} catch (e) {
throw Exception(e);
}
}
Can we please have a functionality to export base64 encoded image as well ?