如何更改 flutter PDF 库中文本小部件的字体?
How to change the font in a text widget in the flutter PDF library?
目前我正在进行一个项目,该项目必须根据用户提供的数据创建 PDF。但是我需要将文本小部件的字体更改为具有两种语言文本的新字体。但它不起作用。我也更改了应用程序主题部分中的 fontFamily。但是PDF中的字体没有改变。但是其余的文本发生了变化。那么如何在 flutter PDF 库中更改文本小部件中的字体。
这是我用来创建 pdf 的库的 link。 https://pub.dev/packages/pdf
Future<Uint8List> _generatePdf() async {
String name = controllerName.text;
String age;
String complain = controllerComplain.text;
String systolicPressure = controllerSystolicPressure.text;
String diastolicPressure = controllerDiastolicPressure.text;
String fullPressure = systolicPressure + "/" + diastolicPressure + " mmHg";
// Creating the total age
if (controllerAgeYears.text == '') {
} else {
age = controllerAgeYears.text + " years ";
}
if (controllerAgeMonths.text == '') {
} else {
age += controllerAgeMonths.text + ' Months';
}
// Choosing male or female to create the pdf
String mOF = '';
if (_isSelected[0] == true) {
mOF = 'Male';
} else {
mOF = 'Female';
}
// Createing the pdf
pdf.addPage(pw.MultiPage(
pageFormat: PdfPageFormat.a5,
margin: pw.EdgeInsets.all(32),
build: (pw.Context context) {
return <pw.Widget>[
pw.Text("Methsuwa Family Clinic",
style: pw.TextStyle(fontSize: 22)),
pw.Text("No: 607, Medamandiya, Panagoda, Homagama.",
style: pw.TextStyle(fontSize: 10)),
pw.Header(
level: 0,
child: pw.SizedBox(height: 2),
),
pw.SizedBox(height: 5),
pw.Text('Name: ' + name, style: pw.TextStyle(fontSize: 10)),
pw.SizedBox(height: 3),
pw.Text('Age: ' + age, style: pw.TextStyle(fontSize: 10)),
pw.SizedBox(height: 3),
pw.Text('Gender: ' + mOF, style: pw.TextStyle(fontSize: 10)),
pw.SizedBox(height: 3),
pw.Text('Complains: ' + complain,
style: pw.TextStyle(fontSize: 10)),
pw.SizedBox(height: 3),
pw.Text('Blood Pressure: ' + fullPressure,
style: pw.TextStyle(fontSize: 10)),
pw.SizedBox(height: 6),
pw.Table.fromTextArray(
context: context,
headerStyle: pw.TextStyle(fontSize: 10),
cellStyle: pw.TextStyle(fontSize: 10),
data: <List<String>>[
<String>['Drug', 'Amount', 'Days', 'When', 'Total'],
..._data.map((msg) => [
msg["drug"],
msg["amount"],
msg["days"],
msg["when"],
msg["total"]
])
]),
pw.SizedBox(height: 6),
pw.Table.fromTextArray(
context: context,
headerStyle: pw.TextStyle(fontSize: 10),
cellStyle: pw.TextStyle(fontSize: 10),
data: <List<String>>[
<String>['Investigations'],
...selectedInvestigations.map((msg) => [msg["investigation"]])
]),
pw.Text(
'Daily:එදිනෙදා, BID:දිනකට දෙවරක්, TID:දිනකට තෙවරක්, QID:දිනකට සතරවරක්, QHS:නින්දට පෙර Q4h:සෑම පැය සතරකට වරක් Q4-6h:සෑම පැය සතරත් සයත් තුල QWK:සෑම සතියකට වරක්',
),// I need to change the font in this text field. As you can see there are two languages
];
}));
pdfPrint = pdf;
pdf = null;
pdf = pw.Document();
return pdfPrint.save();
}
我用这个文件弄清楚了如何在 flutter pdf 包的文本小部件中使用自定义字体。https://github.com/DavBfr/dart_pdf/blob/master/demo/lib/examples/resume.dart
首先我们必须在变量中定义字体。
var font = await PdfGoogleFonts.abhayaLibreRegular();
然后我们必须将它添加到文本小部件,如下所示。
pw.Text('Hello World', style: TextStyle(font: font));
目前我正在进行一个项目,该项目必须根据用户提供的数据创建 PDF。但是我需要将文本小部件的字体更改为具有两种语言文本的新字体。但它不起作用。我也更改了应用程序主题部分中的 fontFamily。但是PDF中的字体没有改变。但是其余的文本发生了变化。那么如何在 flutter PDF 库中更改文本小部件中的字体。 这是我用来创建 pdf 的库的 link。 https://pub.dev/packages/pdf
Future<Uint8List> _generatePdf() async {
String name = controllerName.text;
String age;
String complain = controllerComplain.text;
String systolicPressure = controllerSystolicPressure.text;
String diastolicPressure = controllerDiastolicPressure.text;
String fullPressure = systolicPressure + "/" + diastolicPressure + " mmHg";
// Creating the total age
if (controllerAgeYears.text == '') {
} else {
age = controllerAgeYears.text + " years ";
}
if (controllerAgeMonths.text == '') {
} else {
age += controllerAgeMonths.text + ' Months';
}
// Choosing male or female to create the pdf
String mOF = '';
if (_isSelected[0] == true) {
mOF = 'Male';
} else {
mOF = 'Female';
}
// Createing the pdf
pdf.addPage(pw.MultiPage(
pageFormat: PdfPageFormat.a5,
margin: pw.EdgeInsets.all(32),
build: (pw.Context context) {
return <pw.Widget>[
pw.Text("Methsuwa Family Clinic",
style: pw.TextStyle(fontSize: 22)),
pw.Text("No: 607, Medamandiya, Panagoda, Homagama.",
style: pw.TextStyle(fontSize: 10)),
pw.Header(
level: 0,
child: pw.SizedBox(height: 2),
),
pw.SizedBox(height: 5),
pw.Text('Name: ' + name, style: pw.TextStyle(fontSize: 10)),
pw.SizedBox(height: 3),
pw.Text('Age: ' + age, style: pw.TextStyle(fontSize: 10)),
pw.SizedBox(height: 3),
pw.Text('Gender: ' + mOF, style: pw.TextStyle(fontSize: 10)),
pw.SizedBox(height: 3),
pw.Text('Complains: ' + complain,
style: pw.TextStyle(fontSize: 10)),
pw.SizedBox(height: 3),
pw.Text('Blood Pressure: ' + fullPressure,
style: pw.TextStyle(fontSize: 10)),
pw.SizedBox(height: 6),
pw.Table.fromTextArray(
context: context,
headerStyle: pw.TextStyle(fontSize: 10),
cellStyle: pw.TextStyle(fontSize: 10),
data: <List<String>>[
<String>['Drug', 'Amount', 'Days', 'When', 'Total'],
..._data.map((msg) => [
msg["drug"],
msg["amount"],
msg["days"],
msg["when"],
msg["total"]
])
]),
pw.SizedBox(height: 6),
pw.Table.fromTextArray(
context: context,
headerStyle: pw.TextStyle(fontSize: 10),
cellStyle: pw.TextStyle(fontSize: 10),
data: <List<String>>[
<String>['Investigations'],
...selectedInvestigations.map((msg) => [msg["investigation"]])
]),
pw.Text(
'Daily:එදිනෙදා, BID:දිනකට දෙවරක්, TID:දිනකට තෙවරක්, QID:දිනකට සතරවරක්, QHS:නින්දට පෙර Q4h:සෑම පැය සතරකට වරක් Q4-6h:සෑම පැය සතරත් සයත් තුල QWK:සෑම සතියකට වරක්',
),// I need to change the font in this text field. As you can see there are two languages
];
}));
pdfPrint = pdf;
pdf = null;
pdf = pw.Document();
return pdfPrint.save();
}
我用这个文件弄清楚了如何在 flutter pdf 包的文本小部件中使用自定义字体。https://github.com/DavBfr/dart_pdf/blob/master/demo/lib/examples/resume.dart
首先我们必须在变量中定义字体。
var font = await PdfGoogleFonts.abhayaLibreRegular();
然后我们必须将它添加到文本小部件,如下所示。
pw.Text('Hello World', style: TextStyle(font: font));