dart_pdf icon indicating copy to clipboard operation
dart_pdf copied to clipboard

The library doesn't support Chinese

Open jiangkang opened this issue 1 year ago • 5 comments

Describe the bug the library doesn't support Chinese

To Reproduce Code snippet to reproduce the behavior:

String content = "中国";
final pdf = pw.Document();
    pdf.addPage(pw.Page(
        pageFormat: PdfPageFormat.a4,
        build: (pw.Context context) {
          return pw.Center(
            child: pw.Text(content),
          ); // Center
        }));
    final dirPath = (await getApplicationDocumentsDirectory()).path;
    final file = File("$dirPath/${DateTime.now()}.pdf");
    if (!file.existsSync()) {
      file.createSync(recursive: false);
    }
    await file.writeAsBytes(await pdf.save());

Expected behavior expect the pdf file can display Chinese text.

Screenshots image

Flutter Doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.0.5, on macOS 12.5 21G72 darwin-arm, locale
    zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 13.4.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.2)
[✓] VS Code (version 1.69.2)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

• No issues found!

Desktop (please complete the following information):

  • [x] iOS
  • [ ] Android
  • [ ] Browser
  • [ ] Windows
  • [ ] Linux

Smartphone (please complete the following information):

  • Device: Iphone 13 Pro Max
  • OS: iOS 15.5

jiangkang avatar Jul 30 '22 13:07 jiangkang

@jiangkang You need to use a TTF that supports chinese and therefore pdf plugin will work as intended.

therealjohnsummer avatar Aug 02 '22 15:08 therealjohnsummer

@jiangkang You need to use a TTF that supports chinese and therefore pdf plugin will work as intended.

@therealjohnsummer thanks, i'll try it

jiangkang avatar Aug 09 '22 07:08 jiangkang

@jiangkang You need to use a TTF that supports chinese and therefore pdf plugin will work as intended.

@therealjohnsummer thanks, i'll try it

Have you found a proper font?

2math avatar Aug 15 '22 13:08 2math

@DavBfr could help us?

stdroom avatar Sep 18 '22 02:09 stdroom

it is works!!! ` // download this font https://www.alibabafonts.com/#/font var data = await rootBundle.load("assets/fonts/AlibabaPuHuiTi-Regular.ttf"); pw.Font meFont = pw.Font.ttf(data); final pdf = pw.Document();

pdf.addPage(pw.Page(
    pageFormat: PdfPageFormat.a4,
    build: (pw.Context context) {
            return pw.Container(
              alignment: pw.Alignment.topLeft,
              padding: pw.EdgeInsets.symmetric(vertical: 15,horizontal: 10),
              color: PdfColor.fromInt(AppColors.c_white.value),
              child: pw.Text("哈哈,支持中文了 ,hhh, it's supported chinese",
                style: pw.TextStyle(
                    font:meFont,
                    fontSize: Util.setSP(15),color: PdfColor.fromInt(AppColors.c_black.value)),),
            );
          }
    }));`

stdroom avatar Sep 18 '22 04:09 stdroom

This will help https://www.youtube.com/watch?v=LLBoRBAQIw0

myselfuser1 avatar Mar 17 '23 11:03 myselfuser1

the problem has been solved perfectly

jiangkang avatar Mar 23 '23 02:03 jiangkang