gpt4 book ai didi

android - 我正在尝试使用 "Pdf creation library"在 flutter 应用程序中生成 pdf。在用其他语言生成 pdf 时会出现异常

转载 作者:行者123 更新时间:2023-12-04 12:25:13 25 4
gpt4 key购买 nike

这是用于以其他语言字体生成 pdf 的代码:

final Uint8List fontData = File('fonts/shivaji05.ttf').readAsBytesSync();
final ttf = Font.ttf(fontData.buffer.asByteData());

这就是我在文本中使用定义字体的方式:
Text('साई बाबा', textScaleFactor: 2, style: new TextStyle(font: ttf)),

错误日志
E/flutter (26251): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: 'package:pdf/src/font.dart': Failed assertion: line 145 pos 14: 'false': 
E/flutter (26251): ---------------------------------------------
E/flutter (26251): Can not decode the string to Latin1.
E/flutter (26251): This font does not support Unicode characters.
E/flutter (26251): If you want to use strings other than Latin strings, use a TrueType (TTF) font instead.</i>

最佳答案

当我尝试使用“Pdf 创建库”在 Flutter 应用程序中生成 pdf 时,我遇到了类似的错误。在生成pdf时,我的源文本中有正确的单引号“'”“’”。

错误代码

final String soutceString = 'My source text with " ’ "';

final Document pdf = Document(deflate: zlib.encode);

pdf.addPage(
MultiPage(
build: (Context context) => <Widget>[
Paragraph(text: soutceString),
]
)
);

错误日志
flutter: 'package:pdf/src/font.dart': Failed assertion: line 145 pos 14: 'false':
---------------------------------------------
Can not decode the string to Latin1.
This font does not support Unicode characters.
If you want to use strings other than Latin strings, use a TrueType (TTF) font instead.

当我发现错误的符号时,我只是替换它。

快速修复代码

final String sourceString = 'My source text with " ’ "';

final Document pdf = Document(deflate: zlib.encode);

sourceString.replaceAll('’', '`'),

pdf.addPage(MultiPage(
build: (Context context) => <Widget>[
Paragraph(text: sourceString)
]
)
);

但我很害怕, future 会出现新的错误符号。我 将 arial.ttf 字体添加到 Assets (arial.ttf 包含我的符号)并使用它。 Adding assets

对我来说最好的案例
import 'package:flutter/services.dart' show rootBundle;



final String sourceString = 'My source text with " ’ "';

final Document pdf = Document(deflate: zlib.encode);

pdf.addPage(MultiPage(
theme: Theme.withFont(
base: Font.ttf(await rootBundle.load("assets/arial.ttf")),
bold: Font.ttf(await rootBundle.load("assets/arial.ttf")),
italic: Font.ttf(await rootBundle.load("assets/arial.ttf")),
boldItalic: Font.ttf(await rootBundle.load("assets/arial.ttf")),
),
build: (Context context) => <Widget>[
Paragraph(text: sourceString)
]
)
);

关于android - 我正在尝试使用 "Pdf creation library"在 flutter 应用程序中生成 pdf。在用其他语言生成 pdf 时会出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57638851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com