【问题标题】:Flutter: save a generated PDF from preview page with a custom functionFlutter:使用自定义功能从预览页面保存生成的 PDF
【发布时间】:2021-12-11 15:09:48
【问题描述】:

我正在使用pdf 包(链接到pub.dev 页面)在我的应用程序中创建文档。

如何保存预览页面中的文档? 我知道内置的printshare 函数,但我需要其他的。

我需要一个执行以下操作的函数:

  1. 将文档保存到设备中
  2. 将其上传到 Firebase 存储
  3. 删除本地文件

我知道如何执行所有这些步骤,但我无法访问正在预览的 pdf 文档的 Uint8List 字节...我尝试添加 PdfPreviewAction,但我仍然无法从中访问文档的字节

【问题讨论】:

    标签: flutter dart pdf pdf-generation dart-pub


    【解决方案1】:

    您所要做的就是调用 Document() 对象中的保存方法

    import "package:pdf/widgets.dart" as w;
    final pdf = w.Document();
    Future<Uint8List> getPDFData() async {
    return await pdf.save();
    }
    

    您可以使用 getPDFData() 方法完成所有操作 例如:

    return PdfPreview(
            build: (format) => getPdfData(),
            actions: [
              PdfPreviewAction(
                  icon: Icon(Icons.print),
                  onPressed: (context, fn, format) {
                    //if you call fn(format), you'll get Future<UInt8List>
                  }),
                  
            ],
          );
    

    【讨论】:

    • 好的,谢谢,但我想从PdfPreviewAction 调用此函数,这是底部栏中的第三个图标按钮。这可能吗?
    • 什么是 PdfPreviewAction?是你自己的课吗?据我所知,这个库没有名为 PdfPreviewAction 的类。
    • 它是printing 包的一部分。这是它的documentation
    • 试试这个,PdfPreviewAction(icon: Icon(Icons.print), onPressed: (context, fn, format) { print(fn(format)); })
    • 我会尽力让你知道...谢谢
    猜你喜欢
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 2023-03-16
    • 2017-12-05
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    相关资源
    最近更新 更多