【问题标题】:FLUTTER-WEB file.writeAsBytes not workingFLUTTER-WEB file.writeAsBytes 不工作
【发布时间】:2020-05-17 20:48:01
【问题描述】:

我一直在尝试使用 pdf 插件生成 pdf,并使用 file.writeAsBytes 将其写入 pdf 文件,但它没有创建任何文件,甚至没有抛出任何错误或给出任何响应。我正在使用 pdf 插件并尝试效法它的榜样。

链接https://pub.dev/packages/path#-example-tab-

void makePdf() async {

  final pw.Document doc = pw.Document();
  pw.Font font = pw.Font.ttf(await rootBundle.load('assets/Exo-Bold.ttf'));

doc.addPage(
  pw.Page(
    pageFormat: pdf.PdfPageFormat.a4,
    build: (pw.Context context) {
      return pw.Center(
        child: pw.Text(
          "Hello World",
          style: pw.TextStyle(font: font),
        ),
      ); // Center
    },
  ),
);

final file = File("ms.pdf");

List<int> data = doc.save();
// print(data);

file.writeAsBytes(data).then((file) {
  print("done");
}).catchError((e) => print(e));

}

【问题讨论】:

    标签: flutter dart flutter-web


    【解决方案1】:

    我认为你需要指定路径

    String directory = (await getApplicationDocumentsDirectory()).path;
    final pdfName = '$directory/recipet.pdf';
    final File file = File(pdfName);
    await file.writeAsBytes(doc.save());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-13
      • 2020-12-12
      • 2021-03-29
      • 2021-08-19
      • 2021-06-12
      • 1970-01-01
      • 2023-02-05
      • 2021-12-12
      相关资源
      最近更新 更多