DavBfr / dart_pdf

Pdf creation module for dart/flutter
https://pub.dev/packages/pdf
Apache License 2.0
1.42k stars 633 forks source link

Uint8List failing conversion to pw.Image #1574

Open codingiswhyicry opened 10 months ago

codingiswhyicry commented 10 months ago

Describe the bug When attempting to take a UIntList8 and convert it to an image, my PDF fails to convert. I've narrowed down the bug and ensured a. The image being used is not corrupted and contains data, b. the PDF generation only fails on one line. I'm not sure if this is a bug overall, or if I am missing something.

This is being run on Flutter web only, and the images are user-generated earlier in the application. These complicate the ways that you can turn an image into a PDF, so I won't be able to use await rootBundle or other File / dart.io based workarounds.

To Reproduce Code snippet to reproduce the behavior:


  pdfWidget.Widget returnImageGradeItem(
      String answer, Icon icon, Uint8List image) {

    return pw.Padding(
      padding: const pw.EdgeInsets.fromLTRB(
        10.0,
        10.0,
        10.0,
        10.0,
      ),
      child: pw.Container(
        decoration: pw.BoxDecoration(
          borderRadius: pw.BorderRadius.circular(10.0),
          border: pw.Border.all(
            color: pdf.PdfColors.blueGrey300,
            width: 1.0,
          ),
        ),
        child: pw.Padding(
          padding: const pw.EdgeInsets.all(10.0),
          child: pw.Column(
            mainAxisAlignment: pw.MainAxisAlignment.start,
            crossAxisAlignment: pw.CrossAxisAlignment.start,
            children: [
              pw.SizedBox(height: 10),
              pw.Row(
                mainAxisAlignment: pw.MainAxisAlignment.start,
                crossAxisAlignment: pw.CrossAxisAlignment.start,
                children: [
                  pw.Container(
                    width: 50.0,
                    height: 50.0,
                    child: pw.Image(
                      pw.MemoryImage(image), // This is the line that never completes and fails. The PDF compiles fine without this line.
                    ),
                  ),
                  pw.SizedBox(width: 10),
                  pw.Column(
                    mainAxisAlignment: pw.MainAxisAlignment.start,
                    crossAxisAlignment: pw.CrossAxisAlignment.start,
                    children: [
                      pw.Flexible(
                        child: pw.Text(
                          answer,
                          style: pw.TextStyle(
                            fontSize: 13.0,
                          ),
                        ),
                      ),
                      pw.SizedBox(width: 40),
                      printIcon,
                    ],
                  ),
                ],
              ),
              pw.SizedBox(height: 15),
            ],
          ),
        ),
      ),
    );
  }

  Future<pw.Document> returnUWPDF(
    Object customization,
    Object escrowInfo,
    Object manager,
    pdf.PdfPageFormat format,
  ) async {

// This function just goes through the answers we've received and determine if it needs a regular grade widget, or one with an image to verify it. 

itemGrades.entries.forEach(
      (element) async {
        var item = manager.result!.items.keys.elementAt(index);

        // Checks to see if it has a photo verification image for the answer. 
        if (item.isPhotoVerified == true && item.photo != null) {
          resultItemWidgets.add(
            returnImageGradeItem(element.key, element.value, item.photo!),
          );
        } else {
          resultItemWidgets.add(
            returnGradeItem(element.key, element.value),
          );
        }
        index += 1;
      },
    );
  }
}

Expected behavior

I expected the pdf to contain images from the item. Instead, it either does not finish at all or does not include the grades with images required.

Flutter Doctor

[✓] Flutter (Channel stable, 3.13.1, on macOS 13.4 22F66 darwin-arm64, locale en-US)
    • Flutter version 3.13.1 on channel stable at /Users/amandasouthworth/Dev/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision e1e47221e8 (5 months ago), 2023-08-22 21:43:18 -0700
    • Engine revision b20183e040
    • Dart version 3.1.0
    • DevTools version 2.25.0

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/macos#android-setup for detailed instructions).

[✓] VS Code (version 1.81.1)
    • VS Code at /Users/amandasouthworth/Downloads/Visual Studio Code.app/Contents
    • Flutter extension version 3.80.0

[✓] Connected device (2 available)
    • macOS (desktop) • macos  • darwin-arm64   • macOS 13.4 22F66 darwin-arm64
    • Chrome (web)    • chrome • web-javascript • Google Chrome 120.0.6099.234

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.

Desktop (please complete the following information):

Additional context

bksbora commented 10 months ago

use a async function in initstate and put your photo into a variable before create your pdf. final example = (await rootBundle.load('assets/orta.jpeg')).buffer.asUint8List();

codingiswhyicry commented 10 months ago

Thank you for your ideas! I've already done those things within the context of the view. The view and PDF logic are separate pieces of code, and I only shared the PDF logic because it's the only place the actual bug is. The PDF gets objects with the Unit8List, so the variables are there.