Comments (10)
Use:
PdfImage image = await pdfImageFromImageProvider(
pdf: document,
image: AssetImage('assets/images/1.jpg'),
);
pdfImageFromImageProvider
is part of the printing package
from dart_pdf.
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:
- 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);
}
- the
ui.image
is referring to the importimport 'dart:ui' as ui;
from dart_pdf.
You can copy the updated function from the source here.
from dart_pdf.
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.
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.
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.
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.
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.
got it.
from dart_pdf.
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)
- Features not working. HOT 2
- How to add signature image at the bottom right corner of last pdf document. HOT 1
- directPrintPdf multiple copies
- How to block/unable save to pdf
- Error on mac os build with flutter 3.21.0-1.0.pre.2 HOT 1
- Paragraph inside Partition does not pass content to the next page as expected.
- Printing a PDF with layoutPdf fails to print: The 'net.nfet.printing' channel sent a message from native to Flutter on a non-platform thread
- Bangla text Rendering issue.
- PdfPreview renders inaccurate colors on Android
- Khmer Language is still does't working, Please help to improve too
- I/flutter ( 3281): Exception: Exception: Unable to guess the image type 1311710 bytes
- Does not apply PdfColor in alpha HOT 1
- Pdf/A
- Restrictions on customizations
- The Urdu tag is for issues relating to displaying user interfaces and managing data in Udru written language
- When I press the shared button it calls onPrinted instead of onShared
- Extend NewPage with freeSpace HOT 3
- Callback for Print Success/Failure Notification HOT 3
- Not forwarding email extras when sharing
- Emoji's not displaying in PDF generated with dart_pdf HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dart_pdf.