joseph-grabinger / flutter_to_pdf

Create PDFs but work with normal Flutter Widgets.
MIT License
21 stars 16 forks source link

Exception `Unsupported Font: .AppleSystemUIFont` #78

Closed rmahmadkhan closed 6 days ago

rmahmadkhan commented 3 months ago

Not using any custom fonts but getting this error when i try to generate the pdf. any leads?

Version: flutter 3.22.2 Platform: web Device: MacBook Pro M1 14" Browser: Chrome

joseph-grabinger commented 3 months ago

Do you have a minimal way of reproducing the issue? Since I wasn't able to reproduce it on Chrome.

mamarsh commented 1 month ago

I'm seeing the same thing, for a MacOS application (presumably from the same root cause).

I've included a minimal example. It doesn't misbehave if I don't specify the style for text. That is, if the text has no style specified, it seems to work just fine.

Here's the top of the stack trace:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: Unsupported Font: .AppleSystemUIFont
#0      TextStyleConverter.resolveFont (package:flutter_to_pdf/args/text_style.dart:66:11)
#1      TextStyleConverter.toPdfTextStyle (package:flutter_to_pdf/args/text_style.dart:39:21)
#2      TextConverter.toPdfWidget (package:flutter_to_pdf/widgets/text.dart:22:29)
#3      ExportInstance.matchWidget (package:flutter_to_pdf/export_instance.dart:176:40)
#4      ExportInstance._visit (package:flutter_to_pdf/export_instance.dart:58:29)
#5      ExportInstance.matchWidget (package:flutter_to_pdf/export_instance.dart:292:22)
#6      ExportDelegate._exportWidget (package:flutter_to_pdf/export_delegate.dart:109:30)
#7      ExportDelegate._exportPage (package:flutter_to_pdf/export_delegate.dart:121:39)
#8      ExportDelegate._exportDocument (package:flutter_to_pdf/export_delegate.dart:134:35)
#9      ExportDelegate.exportToPdfDocument (package:flutter_to_pdf/export_delegate.dart:61:18)
#10     _MyHomePageState.build.<anonymous closure> (package:pdf_apple_fonts/main.dart:59:32)

Here's main.dart:

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_to_pdf/flutter_to_pdf.dart';
import 'package:path_provider/path_provider.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final ExportDelegate exportDelegate = ExportDelegate();
  final reportKey = UniqueKey().toString();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          children: <Widget>[
        ExportFrame(
          frameId: reportKey,
          exportDelegate: exportDelegate,
              child: Text(
                'Sample text',
        style: Theme.of(context).textTheme.bodyMedium
              ),
        ),
        ElevatedButton(
              onPressed: () {
                exportDelegate.exportToPdfDocument(reportKey).then(
              (value) => saveFile(value, 'foo'),
                );
              },
              child: const Text('Save to PDF')
        ),
          ],
        ),
      ),
    );
  }

  Future<void> saveFile(document, String name) async {
    final Directory dir = await getApplicationDocumentsDirectory();
    final File file = File('${dir.path}/$name.pdf');

    await file.writeAsBytes(await document.save());
    debugPrint('Saved exported PDF at: ${file.path}');
  }

}
Sed-iq commented 1 month ago

i have this issue too it's a big project i can't remove every text style help

joseph-grabinger commented 6 days ago

I'm seeing the same thing, for a MacOS application (presumably from the same root cause).

I've included a minimal example. It doesn't misbehave if I don't specify the style for text. That is, if the text has no style specified, it seems to work just fine.

Thanks for the example @mamarsh!

This way it was easily reproducible and I could do some investigation. I wrapped the "Sample text" form your example in a Column and added another Text widget without any style for comparison.

The result is the following:

image

You can see that both Text widgets (with and without style) look exactly the same.

Thus I'd say the issue could be fixed by just handling a Text widget which uses the .AppleSystemUIFont in the exact same way as we handle Text widgets without any TextStyle / FontStyle provided.