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

There is a conflict using AssetManifest of printing package and services package side by side #1295

Closed mhbdev closed 1 year ago

mhbdev commented 1 year ago

I am confronting this issue since I have upgraded to the latest flutter version on the master channel. To solve the issue we have to name services package import to prevent the conflicts. The issue is appearing in lib\src\fonts\font.dart file.

Here is a the solution:

import 'package:flutter/services.dart' as services;
  /// Get the font to use in a Pdf document
  Future<Font> getFont({
    PdfBaseCache? pdfCache,
    bool protect = false,
    Map<String, String>? headers,
    String assetPrefix = 'google_fonts/',
    services.AssetBundle? bundle,
    bool cache = true,
  }) async {
    final asset = '$assetPrefix$name.ttf';
    if (await AssetManifest.contains(asset)) {
      bundle ??= services.rootBundle;
      final data = await bundle.load(asset);
      return TtfFont(
        data,
        protect: protect,
      );
    }

    pdfCache ??= PdfBaseCache.defaultCache;

    try {
      final bytes = await pdfCache.resolve(
        name: name,
        uri: Uri.parse(url),
        headers: headers,
        cache: cache,
      );

      return TtfFont(
        bytes.buffer.asByteData(bytes.offsetInBytes, bytes.lengthInBytes),
        protect: protect,
      );
    } catch (e) {
      assert(() {
        // ignore: avoid_print
        print('$e\nError loading $name, fallback to Helvetica.');
        return true;
      }());

      return Font.helvetica();
    }
  }
CrisNegoianu commented 1 year ago

Hi.

Please make this change, because we can't build the app on the beta channel because of this.

Thank you.

travishaagen commented 1 year ago

If you're in a hurry, you can do this until the 5.10.2 release is available:

  printing: # TODO 5.10.2 # https://github.com/DavBfr/dart_pdf
    git:
      url: https://github.com/DavBfr/dart_pdf.git
      ref: 029c8f53d623a1f991ece3c548cb7d83a83aff49
      path: printing