David PHAM-VAN

Add imageFromAssetBundle and networkImage

# Changelog
## 5.0.0-nullsafety.2
- Add imageFromAssetBundle and networkImage
## 5.0.0-nullsafety.1
- Fix PdfPreview default locale
... ...
... ... @@ -46,16 +46,15 @@ doc.addPage(pw.Page(
pageFormat: PdfPageFormat.a4,
build: (pw.Context context) {
return pw.Center(
child: pw.Text("Hello World"),
child: pw.Text('Hello World'),
); // Center
})); // Page
```
To load an image from an ImageProvider:
To load an image from a Flutter asset:
```dart
const imageProvider = const AssetImage('assets/image.png');
final image = await flutterImageProvider(imageProvider);
final image = await imageFromAssetBundle('assets/image.png');
doc.addPage(pw.Page(
build: (pw.Context context) {
... ... @@ -68,8 +67,7 @@ doc.addPage(pw.Page(
To use a TrueType font from a flutter bundle:
```dart
final font = await rootBundle.load("assets/open-sans.ttf");
final ttf = pw.Font.ttf(font);
final ttf = await fontFromAssetBundle('assets/open-sans.ttf');
doc.addPage(pw.Page(
build: (pw.Context context) {
... ... @@ -83,7 +81,7 @@ To save the pdf file using the [path_provider](https://pub.dev/packages/path_pro
```dart
final output = await getTemporaryDirectory();
final file = File("${output.path}/example.pdf");
final file = File('${output.path}/example.pdf');
await file.writeAsBytes(await doc.save());
```
... ...
... ... @@ -15,10 +15,13 @@
*/
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:flutter/rendering.dart' as rdr;
import 'package:flutter/services.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart';
/// Loads an image from a Flutter [ImageProvider]
... ... @@ -63,7 +66,108 @@ Future<ImageProvider> flutterImageProvider(
/// Loads a font from an asset bundle key. If used multiple times with the same font name,
/// it will be included multiple times in the pdf file
Future<TtfFont> fontFromAssetBundle(String key, AssetBundle bundle) async {
Future<TtfFont> fontFromAssetBundle(String key, [AssetBundle? bundle]) async {
bundle ??= rootBundle;
final data = await bundle.load(key);
return TtfFont(data);
}
/// Load an image from an asset bundle key.
Future<ImageProvider> imageFromAssetBundle(String key,
[AssetBundle? bundle]) async {
bundle ??= rootBundle;
final data = await bundle.load(key);
return MemoryImage(data.buffer.asUint8List());
}
final HttpClient _sharedHttpClient = HttpClient();
/// Store network images in a cache
abstract class PdfBaseImageCache {
/// Create a network image cache
const PdfBaseImageCache();
/// The default cache used when none specified
static final defaultCache = PdfMemoryImageCache();
/// Add an image to the cache
Future<void> add(String key, Uint8List bytes);
/// Retrieve an image from the cache
Future<Uint8List?> get(String key);
/// Does the cache contains this image?
Future<bool> contains(String key);
/// Remove an image from the cache
Future<void> remove(String key);
/// Clear the cache
Future<void> clear();
}
/// Memory image cache
class PdfMemoryImageCache extends PdfBaseImageCache {
/// Create a memory image cache
PdfMemoryImageCache();
final _imageCache = <String, Uint8List>{};
@override
Future<void> add(String key, Uint8List bytes) async {
_imageCache[key] = bytes;
}
@override
Future<Uint8List?> get(String key) async {
return _imageCache[key];
}
@override
Future<void> clear() async {
_imageCache.clear();
}
@override
Future<bool> contains(String key) async {
return _imageCache.containsKey(key);
}
@override
Future<void> remove(String key) async {
_imageCache.remove(key);
}
}
/// Download an image from the network.
Future<ImageProvider> networkImage(
String url, {
bool cache = true,
Map<String, String>? headers,
PdfImageOrientation? orientation,
double? dpi,
PdfBaseImageCache? imageCache,
}) async {
imageCache ??= PdfBaseImageCache.defaultCache;
if (cache && await imageCache.contains(url)) {
return MemoryImage((await imageCache.get(url))!,
orientation: orientation, dpi: dpi);
}
final request = await _sharedHttpClient.getUrl(Uri.parse(url));
headers?.forEach((String name, String value) {
request.headers.add(name, value);
});
final response = await request.close();
final builder = await response.fold(
BytesBuilder(), (BytesBuilder b, List<int> d) => b..add(d));
final List<int> data = builder.takeBytes();
final bytes = Uint8List.fromList(data);
if (cache) {
await imageCache.add(url, bytes);
}
return MemoryImage(bytes, orientation: orientation, dpi: dpi);
}
... ...
... ... @@ -4,7 +4,7 @@ description: Plugin that allows Flutter apps to generate and print documents to
homepage: https://github.com/DavBfr/dart_pdf/tree/master/printing
repository: https://github.com/DavBfr/dart_pdf
issue_tracker: https://github.com/DavBfr/dart_pdf/issues
version: 5.0.0-nullsafety.1
version: 5.0.0-nullsafety.2
environment:
sdk: ">=2.12.0-0 <3.0.0"
... ...