firebase 如何在自动命名的文件保存在Flutter,PDF

bnl4lu3b  于 2023-03-03  发布在  Flutter
关注(0)|答案(1)|浏览(110)

我如何将文档保存名称更改为我想要的名称?
每当我保存文档时,它只会有一个名称document.pdf。我想实现的是,我如何给予文档名称的名称,我喜欢,例如Mydocument.pdf。

Future<Uint8List>generateDocument(PdfPageFormat format) async{
  CollectionReference users = FirebaseFirestore.instance.collection('ratings');
  final snapshot = await users.doc('2-2023').collection('failed rating').doc('Very Dissatisfied-Dissatisfied').get();
  final doc = pw.Document(pageMode: PdfPageMode.outlines);
  final font1 = await PdfGoogleFonts.openSansRegular();
  final font2 = await PdfGoogleFonts.openSansBold();
  doc.addPage(
      pw.Page(
          pageTheme: pw.PageTheme(
            pageFormat: format.copyWith(
              marginBottom: 0,
              marginLeft: 0,
              marginRight: 0,
              marginTop: 0,
            ),
            orientation: pw.PageOrientation.portrait,
            theme: pw.ThemeData.withFont(
              base: font1,
              bold: font2,
            ),
          ),

          build: (context){
            return pw.Center(
                child: pw.Column(
                    mainAxisAlignment: pw.MainAxisAlignment.center,
                    children: [
                      pw.Center(
                          child: pw.Text(
                            snapshot['total'].toString(),
                          )
                      ),
                      pw.Center(
                          child: pw.Text(
                            snapshot['rating'].toString(),
                          )
                      )
                    ]
                )
            );
          }
      )

  );

  return doc.save();

}

以下是我目前所做的尝试。

return doc.save();
crcmnpdw

crcmnpdw1#

您需要dart:io将文件保存到自定义路径。请注意,您还需要:

  • 提供一个正确的路径来保存文件。你可以使用path_provider包。
  • 允许存储权限。您可以使用permission_handler包来实现此目的。

看看下面的示例应用程序,它完全符合你的需要。_savePdf()函数是神奇的地方。注意,你还需要遵循permission_handler的一些设置步骤(AndroidManifest.xmlbuild.gradle,...),这些都在指南中。

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Future<void> _savePdf() async {
    // 1. Create a working directory where you can save the file
    Directory? appDocDirectory = await getExternalStorageDirectory();

    // 2. Request permissions to access storage
    await Permission.storage.request();

    // 3. Just like you did, create the document and add your pages
    final pdf = pw.Document();
    pdf.addPage(
      pw.Page(
        build: (pw.Context context) => pw.Center(
          child: pw.Text('Hello World!'),
        ),
      ),
    );

    // 4. Set the full path where you want to save the file
    final file = File('${appDocDirectory!.path}/example.pdf');

    // 5. Write the file
    await file.writeAsBytes(await pdf.save());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _savePdf,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

相关问题