Git Product home page Git Product logo

Comments (10)

DavBfr avatar DavBfr commented on May 20, 2024 1

Use:

PdfImage image = await pdfImageFromImageProvider(
    pdf: document, 
    image: AssetImage('assets/images/1.jpg'),
);

pdfImageFromImageProvider is part of the printing package

from dart_pdf.

crushman1 avatar crushman1 commented on May 20, 2024 1

Thanks! Works perfectly. I didn't think of simply tearing the function out of the pack as an option!
But for people having the same issue, you may have to do some editing:

  1. its most probably going to say pdfImageFromImage is undefined. I had to go into the printing package to search for it. make sure to add it in :
/// Loads an image from a Flutter [ui.Image]
/// into a [PdfImage] instance
Future<PdfImage> pdfImageFromImage(
    {@required PdfDocument pdf, @required ui.Image image}) async {
  final ByteData bytes =
  await image.toByteData(format: ui.ImageByteFormat.rawRgba);

  return PdfImage(pdf,
      image: bytes.buffer.asUint8List(),
      width: image.width,
      height: image.height);
}

  1. the ui.image is referring to the import import 'dart:ui' as ui;

from dart_pdf.

DavBfr avatar DavBfr commented on May 20, 2024 1

You can copy the updated function from the source here.

from dart_pdf.

crushman1 avatar crushman1 commented on May 20, 2024

this is the only way? the printing package conflicts with firestore messaging and i need firestore messaging. is there no other way to do it without printing package?

from dart_pdf.

crushman1 avatar crushman1 commented on May 20, 2024

This is the error i receive the minute i add printing package to yaml :

android\app\src\main\AndroidManifest.xml Error:
WARNING: This version of firebase_messaging will break your Android build if it or its dependencies aren't compatible with AndroidX.
	uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:printing] C:\apps\tyrone_wreckers_management\build\printing\intermediates\library_manifest\debug\AndroidManifest.xml as the library might be using APIs not available in 16
	Suggestion: use a compatible library with a minSdk of at most 16,
         See https://goo.gl/CP92wY for more information on the problem and how to fix it.
		or increase this project's minSdk version to at least 19,
		or use tools:overrideLibrary="net.nfet.flutter.printing" to force usage (may lead to runtime failures)
         This warning prints for all Android build failures. The real root cause of the error may be unrelated.
         *********************************************************

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:printing] C:\apps\tyrone_wreckers_management\build\printing\intermediates\library_manifest\debug\AndroidManifest.xml as the library might be using APIs not available in 16
  	Suggestion: use a compatible library with a minSdk of at most 16,
  		or increase this project's minSdk version to at least 19,
  		or use tools:overrideLibrary="net.nfet.flutter.printing" to force usage (may lead to runtime failures)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 33s
Finished with error: Gradle task assembleDebug failed with exit code 1

from dart_pdf.

crushman1 avatar crushman1 commented on May 20, 2024

changing my minSDK to 19 does not fix the issue. changing my firestore messaging version does nothing and changing printing packing version also does nothing,
I have all the usual stuff done / enabled for example multiDexEnabled true

from dart_pdf.

DavBfr avatar DavBfr commented on May 20, 2024

Just copy the function in your project then:

/// Loads an image from a Flutter [ImageProvider]
/// into a [PdfImage] instance
Future<PdfImage> pdfImageFromImageProvider(
    {@required PdfDocument pdf,
    @required ImageProvider image,
    ImageConfiguration configuration,
    ImageErrorListener onError}) async {
  final Completer<PdfImage> completer = Completer<PdfImage>();
  final ImageStream stream =
      image.resolve(configuration ?? ImageConfiguration.empty);

  Future<void> listener(ImageInfo image, bool sync) async {
    final PdfImage result =
        await pdfImageFromImage(pdf: pdf, image: image.image);
    if (!completer.isCompleted) {
      completer.complete(result);
    }
    stream.removeListener(listener);
  }

  void errorListener(dynamic exception, StackTrace stackTrace) {
    if (!completer.isCompleted) {
      completer.complete(null);
    }
    if (onError != null) {
      onError(exception, stackTrace);
    } else {
      FlutterError.reportError(FlutterErrorDetails(
        context: 'image failed to load',
        library: 'printing',
        exception: exception,
        stack: stackTrace,
        silent: true,
      ));
    }
  }

  stream.addListener(listener, onError: errorListener);
  return completer.future;
}

from dart_pdf.

crushman1 avatar crushman1 commented on May 20, 2024

This no longer works on the latest stable version of flutter version 1.7.8+hotfix.3 . this statement here no longer wants the listener to be a future but now it wants it to be an ImageStreamListener : stream.removeListener(listener);

from dart_pdf.

crushman1 avatar crushman1 commented on May 20, 2024

got it.

from dart_pdf.

biancashouse avatar biancashouse commented on May 20, 2024

Dave,

The web impl gives this error:

Image.toByteData is not supported in Flutter for Web

Is there a web-friendly way to convert the image to bytes ?

Ian

from dart_pdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.