如何在flutter中覆盖pdf文件?

问题描述

我正在创建一个这样的 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),// 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,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,10,pw.Container(
                      width: 70,child: pw.Center(
                        child: pw.Text(
                          'Qt. $quantity',textAlign: pw.TextAlign.center,style: pw.TextStyle(
                            font: robotoB,pw.Container(
                      width: 120,child: pw.Align(
                        alignment: pw.Alignment.centerRight,child: pw.Text(
                          '$Amount',textAlign: pw.TextAlign.right,],];
      },));
  }

现在我正在使用以下功能保存文件...

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,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,

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

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

我试过了:

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

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

任何建议或建议我哪里做错了???

解决方法

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

await File( YOURFILE ).exists();

File( YOURFILE ).existsSync();

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

await File( YOURFILE ).delete();

File( YOURFILE ).deleteSync();

此时可以新建一个同名同路径的Pdf文件。

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