alihassan143 / htmltopdfwidgets

Html To Pdf
https://pub.dev/packages/htmltopdfwidgets
Apache License 2.0
12 stars 19 forks source link

Not able to export base64 encoded image data from HTML to PDF #27

Open yogesharora388 opened 7 months ago

yogesharora388 commented 7 months ago

Can we please have a functionality to export base64 encoded image as well ?

alihassan143 commented 7 months ago

@yogesharora388 i will add this in future release

yogesharora388 commented 6 months ago

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("");
    }
  }
yogesharora388 commented 6 months ago

@alihassan143 Can you please take a look in above code ? I am able to export the image after making such changes.

alihassan143 commented 6 months ago

@yogesharora388 you need to forked repo than you can able to file a PR

yogesharora388 commented 6 months ago

@alihassan143 Created a PR https://github.com/alihassan143/htmltopdfwidgets/pull/29

Eimer-78 commented 4 months ago

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);
    }
  }
Eimer-78 commented 4 months ago

Bildschirmfoto vom 2024-05-13 23-39-57