DavBfr / dart_pdf

Pdf creation module for dart/flutter
https://pub.dev/packages/pdf
Apache License 2.0
1.42k stars 632 forks source link

Custom fonts don't work within an SVG #1731

Closed clyde-gustafson closed 1 month ago

clyde-gustafson commented 2 months ago

Describe the bug I have an SVG string that has a custom font. I've tested the font on text and it works. I've added the font to the document fonts, but it still doesn't render the SVG properly.

To Reproduce Code snippet to reproduce the behavior: Here's my svg: https://pastebin.com/dzw0yqbd (Had to put it in a pastebin because it was too long)

The font is here: https://github.com/grame-cncm/guidolib/blob/dev/src/guido2.ttf

Here's my code:

   Future<void> exportPdf(String desiredPath) async {
    int pageCount = 1

    final pdf = pw.Document();
    final Uint8List fontData = await File('assets/external-libraries/guido2.ttf').readAsBytes();

    PdfTtfFont font = PdfTtfFont(pdf.document, fontData.buffer.asByteData());

    pdf.document.fonts.add(font);

    for (int i = 1; i <= pageCount; i++) {
      String svgImage = await File('assets/samples/basic.svg').readAsString();

      pdf.addPage(pw.Page(build: (pw.Context context) {
        return pw.Center(
          child: pw.SvgImage(
            svg: svgImage,
          ),
        ); // Center
      }));
    }

    final file = File('$desiredPath.pdf');
    await file.writeAsBytes(await pdf.save());
  }

Expected behavior The font is valid, because when I try to just use the font in text it works fine. But when I try to use the font within an svg this is what shows up: Screenshot 2024-08-29 125626

Here's the text example: Screenshot 2024-08-29 130051

This is generated using this code:

     pdf.addPage(pw.Page(build: (pw.Context context) {
        return pw.Center(
          child: pw.Text('W', style: pw.TextStyle(font: ttf, fontSize: 100))
        ); // Center
      }));

Flutter Doctor

[√] Flutter (Channel stable, 3.19.3, on Microsoft Windows [Version 10.0.22631.4037], locale en-US)
    • Flutter version 3.19.3 on channel stable at C:\Users\13857\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ba39319843 (6 months ago), 2024-03-07 15:22:21 -0600
    • Engine revision 2e4ba9c6fb
    • Dart version 3.3.1
    • DevTools version 2.31.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at C:\Users\13857\AppData\Local\Android\sdk
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.8.6)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.8.34525.116
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2023.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314)

[√] VS Code (version 1.92.2)
    • VS Code at C:\Users\13857\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.94.0

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.22631.4037]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 128.0.6613.86
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 126.0.2592.87

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context