Skip to content

Text rendering error​ #1885

@jingjiangxiaojian

Description

@jingjiangxiaojian

Describe the bug
An error occurs when my PDF content contains the following text.
Bad state: No element
[GETX] Error: _StackTrace #0 Iterable.first (dart:core/iterable.dart:663:7)
#1 TtfWriter.withChars (package:pdf/src/pdf/font/ttf_writer.dart:112:67)
#2 PdfTtfFont._buildType0 (package:pdf/src/pdf/obj/ttffont.dart:121:28)
#3 PdfTtfFont.prepare (package:pdf/src/pdf/obj/ttffont.dart:162:7)
#4 PdfDocument._write (package:pdf/src/pdf/document.dart:227:10)
#5 PdfDocument.save. (package:pdf/src/pdf/document.dart:261:13)
#6 _RemoteRunner._run (dart:isolate:1116:47)
#7 _RemoteRunner._remoteExecute (dart:isolate:1110:12)
#8 _delayEntrypointInvocation. (dart:isolate-patch/isolate_patch.dart:317:17)
#9 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:193:12)
To Reproduce
Code snippet to reproduce the behavior:

/*
 * Copyright (C) 2017, David PHAM-VAN <[email protected]>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import 'dart:async';
import 'dart:math';
import 'dart:typed_data';

import 'package:flutter/services.dart' show rootBundle;
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';

import '../data.dart';

const PdfColor green = PdfColor.fromInt(0xff9ce5d0);
const PdfColor lightGreen = PdfColor.fromInt(0xffcdf1e7);
const sep = 120.0;


List<pw.Partitions> testUI(pw.Context context, pw.PageTheme pageTheme,
    pw.MemoryImage profileImage) {
  return [
    pw.Partitions(
      children: [
        pw.Partition(
          child: pw.Column(
            crossAxisAlignment: pw.CrossAxisAlignment.start,
            children: <pw.Widget>[
              pw.Container(
                padding: const pw.EdgeInsets.only(left: 30, bottom: 20),
                child: pw.Column(
                  crossAxisAlignment: pw.CrossAxisAlignment.start,
                  children: <pw.Widget>[
                    pw.Text(
                      '想⻜的乌⻳从前,有⼀只乌⻳。⼀天,乌⻳听⻅⼀群小⻦在喊:“我们走吧!我们快走吧!”乌⻳问:“你们要去哪⼉?”小⻦说:“我们要去⼀个很美丽的地⽅。”乌⻳问:“我能和你们⼀起去吗?”小⻦说:“可你不会⻜啊!”乌⻳很伤⼼。小⻦想出了⼀个办法。他们让乌⻳叼着⼀根棍⼦,两只小⻦叼着棍⼦的两端。于是,小⻦带着乌⻳⻜起来了。乌⻳很⾼兴,他喜欢⻜的感觉。小⻦越⻜越⾼,⽩云在乌⻳⾝边飘着,⻛从他耳边吹过。下⾯是绿⾊的⽥野,蓝⾊的小河,地上的房⼦都变得很小很小。乌⻳开⼼极了,忍不住喊起来:“太美了!啊——”乌⻳从天上掉下来了!他重重地掉在地上,光滑的壳上摔出了好多裂(li)纹。从此以后,乌⻳的壳就成',
                    ),
                    pw.Padding(padding: const pw.EdgeInsets.only(top: 20)),
                    pw.Text(
                      'annotation',
                      style: pw.TextStyle(
                        fontSize: 32,
                        color: PdfColors.grey,
                        fontWeight: pw.FontWeight.bold,
                      ),
                    ),
                    pw.Padding(padding: const pw.EdgeInsets.only(top: 20)),

                    pw.Text(
                      '想飞的乌龟',

                    ),
                    pw.Padding(padding: const pw.EdgeInsets.only(top: 20)),

                  ],
                ),
              ),

            ],
          ),
        ),

      ],
    ),
  ];
}

Future<Uint8List> generateResume(PdfPageFormat format, CustomData data) async {
  final doc = pw.Document(title: 'My Résumé', author: 'David PHAM-VAN');

  final profileImage = pw.MemoryImage(
    (await rootBundle.load('assets/profile.jpg')).buffer.asUint8List(),
  );

  final pageTheme = await _myPageTheme(format);

  doc.addPage(
    pw.MultiPage(
      pageTheme: pageTheme,
      build: (pw.Context context) => testUI(context, pageTheme, profileImage),
    ),
  );
  return doc.save();
}

Future<pw.PageTheme> _myPageTheme(PdfPageFormat format) async {
  return pw.PageTheme(
    theme: pw.ThemeData.withFont(
        base: await PdfGoogleFonts.notoSansSCRegular(),
        bold: await PdfGoogleFonts.notoSansSCBold(),
        icons: await PdfGoogleFonts.materialIcons(),
         ),

  );
}

Expected behavior
The text should be displayed correctly.

Screenshots

Image

Flutter Doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.32.8, on macOS 15.4.1 24E263 darwin-arm64, locale
    zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version
    36.1.0-rc1)
[✓] Xcode - develop for iOS and macOS (Xcode 16.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2025.1)
[✓] Connected device (2 available)
    ! Error: Browsing on the local area network for Jian的iPhone. Ensure the
      device is unlocked and attached with a cable or associated with the same
      local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code
      -27)
    ! Error: Browsing on the local area network for 康康的iPhone. Ensure the device
      is unlocked and attached with a cable or associated with the same local
      area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code
      -27)
[✓] Network resources

• No issues found!

Desktop (please complete the following information):

  • iOS
  • Android
  • Browser
  • Windows
  • Linux

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
pdf version: "3.11.3", printing version: "5.14.2"
By the way, if I modify part of the content,“想飞的乌龟” to “想飞的乌龟123”, the text can be displayed, but some characters are displayed incorrectly.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions