dart_pdf
dart_pdf copied to clipboard
The library doesn't support Chinese
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
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 You need to use a TTF that supports chinese and therefore pdf plugin will work as intended.
@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 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?
@DavBfr could help us?
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)),),
);
}
}));`
This will help https://www.youtube.com/watch?v=LLBoRBAQIw0
the problem has been solved perfectly