如何在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, 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((,您要删除的不是已经存在的文件,而是刚刚创建的文件。

最新更新