flutter - 如何在flutter中覆盖pdf文件?

标签 flutter pdf pdf-generation flutter-dependencies saving-data

我正在创建一个类似于写入方法的 pdf 文件...使用此包 https://pub.dev/packages/pdf

writeOnPdf(context) async {
    final roboto = await rootBundle.load('assets/fonts/Roboto-Regular.ttf');
    var robotoF = pw.Font.ttf(roboto);
    final robotobold = await rootBundle.load('assets/fonts/Roboto-Bold.ttf');
    var robotoB = pw.Font.ttf(robotobold);
    pdf.addPage(pw.MultiPage(
      theme: pw.ThemeData.withFont(
        base: robotoF,
        bold: robotoB,
      ),
      pageFormat: PdfPageFormat.a4,
      margin: pw.EdgeInsets.fromLTRB(24, 24, 24, 24),
      // header: ,
      maxPages: 1,
      build: (pw.Context context) {
        return <pw.Widget>[
          pw.Column(
            children: [
              pw.Container(
                height: 40,
                color: PdfColors.grey200,
                padding: pw.EdgeInsets.fromLTRB(15, 0, 15, 0),
                child: pw.Row(
                  mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
                  children: [
                    pw.Expanded(
                      child: pw.Text(
                        'Description / Items',
                        style: pw.TextStyle(
                          font: robotoB,
                          // fontWeight: pw.FontWeight.bold,
                        ),
                      ),
                    ),
                    pw.Container(
                      width: 1,
                      height: 25,
                      color: PdfColors.black,
                      padding: pw.EdgeInsets.fromLTRB(10, 0, 10, 0),
                    ),
                    pw.Container(
                      width: 70,
                      child: pw.Center(
                        child: pw.Text(
                          'Qt. $quantity',
                          textAlign: pw.TextAlign.center,
                          style: pw.TextStyle(
                            font: robotoB,
                            // fontWeight: pw.FontWeight.bold,
                          ),
                        ),
                      ),
                    ),
                    pw.Container(
                      width: 1,
                      height: 25,
                      color: PdfColors.black,
                      padding: pw.EdgeInsets.fromLTRB(10, 0, 10, 0),
                    ),
                    pw.Container(
                      width: 120,
                      child: pw.Align(
                        alignment: pw.Alignment.centerRight,
                        child: pw.Text(
                          '$Amount',
                          textAlign: pw.TextAlign.right,
                          style: pw.TextStyle(
                            font: robotoB,
                            // fontWeight: pw.FontWeight.bold,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ];
      },
    ));
  }

现在我正在使用以下函数保存文件...

Future<File> savePDF() async {
    final appPath = Directory("storage/emulated/0/Myxyzapp/Invoices").path;
    pdfFile = File('$appPath/myxyzapp-invoice-$invoiceNo.pdf');
    await pdfFile.writeAsBytes(pdf.save());
    return pdfFile;
  }

    InkWell(
  onTap: () async {
    await writeOnPdf(context);
    await savePDF().then((value) {
      showDialog(
          context: (context),
          builder: (context) => AlertDialog(
                shape: ContinuousRectangleBorder(
                    borderRadius:
                        BorderRadius.circular(20)),
                content: Text(
                    'Invoice PDF regenerated successfully.'),
                actions: <Widget>[
                  FlatButton(
                    onPressed: () => Navigator.pop(
                        context, true),
                    child: Text(
                      "OK",
                      style:
                          TextStyle(fontSize: 14),
                    ),
                  ),
                ],
              ));
    });
  },
  child: Container(
    padding: EdgeInsets.fromLTRB(10, 7, 10, 7),
    decoration: BoxDecoration(
        color: Colors.grey[200],
        borderRadius: BorderRadius.circular(5)),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          'Re-generate PDF ',
          style: TextStyle(
            color: Colors.black54,
            fontSize: 14,
          ),
        ),
        Icon(
          Icons.insert_drive_file,
          color: Colors.black54,
          size: 18,
        ),
      ],
    ),
  ),
),

但是当我重新生成此文件并尝试打开它时,它仍然显示以前的数据...

注意:我的所有 flutter 代码和 writeToPdf() 小部件都在同一页面上...基本上我无法将它们分开,因为我从服务器获取数据并创建动态 PDF 文件。

我已经尝试过:

  1. 通过“pdfFile.delete();”删除旧文件然后将文件保存到路径。

只有当我手动从文件夹中删除文件,然后再次打开应用程序并生成我的 pdf 时,这才有效...

有什么建议或建议我哪里做错了吗???

最佳答案

您可以简单地检查文件是否已存在,然后将其删除。

await File( YOURFILE ).exists();

File( YOURFILE ).existsSync();

如果返回true,则删除文件:

await File( YOURFILE ).delete();

File( YOURFILE ).deleteSync();

此时,您可以创建一个具有相同名称和路径的新 Pdf 文件。

注意使用 pdfFile.delete(),您并不是删除已经存在的文件,而是删除您刚刚创建的文件。

关于flutter - 如何在flutter中覆盖pdf文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65507681/

相关文章:

dart - 根据提供的列表和参数构建和排序新列表

iphone - 需要将 UITextView 完整内容渲染成 PDF

javascript - pdf 文件大小 JavaScript 或 html5

flash - 将 flash 图表转换为图像/pdf

c# - 检索信息后生成pdf文件

asp.net-mvc - 如何使用 wkhtmltopdf 将安全 HTML (ASP.NET MVC 3) 页面转换为 PDF?

flutter - 允许在 flutter 新集成测试中运行整个测试文件夹 - 就像普通的单元测试一样

Flutter FutureBuilder 将项目附加到列表

flutter - 将“容器”与“按钮”在屏幕底部的“列”中对齐

pdf - Jasperreports 标记属性和 PDF