【问题标题】:How to change the font in a text widget in the flutter PDF library?如何更改 Flutter PDF 库中文本小部件中的字体?
【发布时间】:2021-09-06 00:55:02
【问题描述】:

目前我正在开展一个项目,该项目必须根据用户提供的数据创建 PDF。但我需要将文本小部件的字体更改为具有两种语言文本的新字体。但它不起作用。我也在应用主题部分更改了 fontFamily。但是PDF中的字体没有改变。但其余的文字发生了变化。那么如何更改flutter PDF库中文本小部件中的字体。 这是我用来创建 pdf 的库的链接。 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 flutter-dependencies


    【解决方案1】:

    我使用这个文件来弄清楚如何在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));
    

    【讨论】:

      猜你喜欢
      • 2021-10-16
      • 2019-12-06
      • 2019-09-06
      • 1970-01-01
      • 2020-03-11
      • 1970-01-01
      • 2019-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多