Flutter WEB PDF创建不更新Sencond上的数据或进一步调用Function



基本上,我所做的就是在客户端用flutter web创建一个PDF。第一次它可以无缝工作,但在第二次或第三次调用时,它不会创建新文档。事实上,它增加了一个PDF页面,但信息仍然是我第一次运行时得到的。除非我刷新页面(F5(。我基本上所做的是,作为命名参数接收一些信息,如ID、日期、企业名称等。我正在使用pdf:^1.3.24插件来执行这个taks。

下一个是我的代码:

import 'dart:convert';
import 'dart:html';
import 'dart:typed_data';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
var pdf = pw.Document();
Future CreatePDF(
{ImageUrl, datePlaced, mainOrder, subOrderID, businessName}) async {
http.Response response = await http.get(
ImageUrl,
);
final Uint8List list2 = response.bodyBytes;
final image2 = PdfImage.file(
pdf.document,
bytes: list2,
);
final ByteData bytes2 = await rootBundle.load('assets/small_logo.png');
final Uint8List list = bytes2.buffer.asUint8List();
final image = PdfImage.file(
pdf.document,
bytes: list,
);
final ByteData bytes3 = await rootBundle.load('assets/qr_50.jpg');
final Uint8List list3 = bytes3.buffer.asUint8List();
final image3 = PdfImage.file(
pdf.document,
bytes: list3,
);
//Create a PDF document.
pdf.addPage(
pw.Page(
pageFormat: PdfPageFormat.letter,
build: (pw.Context context) {
return pw.Center(
child: pw.Column(
children: [
pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.spaceBetween,
children: [
pw.Container(
height: 75,
width: 75,
child: pw.Image(image2),
),
pw.Container(
height: 75,
width: 75,
child: pw.Image(image3),
),
pw.Container(
height: 75,
width: 75,
child: pw.Image(image),
),
],
),
pw.SizedBox(height: 15),
pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.center,
children: [
pw.Text(
'Orden ID: ',
style: pw.TextStyle(fontSize: 25),
),
pw.Text(
'${subOrderID}',
style: pw.TextStyle(fontSize: 25),
)
],
),
pw.Row(
mainAxisAlignment: pw.MainAxisAlignment.center,
children: [
pw.Text(
'${mainOrder}',
style: pw.TextStyle(
fontSize: 10,
background: pw.BoxDecoration(color: PdfColors.yellow),
),
),
],
),
pw.SizedBox(height: 10),
pw.Row(
children: [
pw.Text(
'Fecha de Recepción de Orden: ',
style: pw.TextStyle(),
),
pw.Text(
'${datePlaced}',
style: pw.TextStyle(),
),
],
),
pw.Row(
children: [
pw.Text(
'Comercio Responsable: ',
style: pw.TextStyle(
fontSize: 15,
),
),
pw.Text(
'Nombre de Comercio',
style: pw.TextStyle(
fontSize: 15,
),
),
],
),
pw.Row(
children: [
pw.Text(
'Si su comercio emite Factura, debera emitirla a: ',
style: pw.TextStyle(),
),
pw.Container(
decoration: pw.BoxDecoration(
// border: pw.BoxBorder(color: PdfColors.blue,width: 6),
),
child: pw.Column(
children: [
pw.Text(
'Razon Social',
style: pw.TextStyle(),
),
pw.Text(
'NIT321566',
style: pw.TextStyle(),
),
],
),
),
],
),
pw.SizedBox(height: 15),
pw.Row(
children: [
pw.Text("Esta Orden Requiere de los siguientes items: "),
],
),
pw.SizedBox(height: 30),
pw.Text("Probando"),
pw.Text("Probando"),
],
),
); // Center
},
),
); // Page
List<int> bytes = null;
bytes = pdf.save(); //Save the document
//Download the output file
AnchorElement(
href:
"data:application/octet-stream;charset=utf-16le;base64,${base64.encode(bytes)}")
..setAttribute("download", '${subOrderID}.pdf')
..click();
}

这是下载前的初始过程、第一次运行的文档和第二次的捕获

该框包含传递给创建PDF 的功能的所有信息

这是第一次生成的PDF,注意文档中只有一页

这是第二次了,注意页面现在是2y。如果再次运行该过程,它会增加。顺便说一句,下一页是空白的。

我真的很感激任何帮助。我觉得问题是从pdf文档开始产生的,是注释被处理还是关闭。。。或者类似的东西。

两件事:

  1. 移动文档初始化"pdf=pw.Document(("进入CreatePDF函数
  2. 如果出现问题,请在继续之前尝试删除文档文件

相关内容

最新更新