【问题标题】:PDF viewer NOT OPENING PDFPDF 查看器未打开 PDF
【发布时间】:2022-01-22 04:04:59
【问题描述】:

我正在尝试从我存储在 firebase firestore 中的 url 在应用程序中打开一个 pdf。

它是开放的,但只是第一次

我尝试了所有方法,但没有任何效果

代码如下:

    import 'package:flutter/material.dart';
    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
    class viewfile extends StatefulWidget {
    final String courseid;
    viewfile({required this.courseid});

    @override
   _viewfileState createState() => _viewfileState();
     }

   class _viewfileState extends State<viewfile> {
   String _version = 'Unknown';


   final CollectionReference _productRef = 
    FirebaseFirestore.instance.collection("answerpapers");


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('Syncfusion Flutter PDF Viewer'),
          actions: <Widget>[
            IconButton(
              icon: const Icon(
                Icons.bookmark,
                color: Colors.white,
                semanticLabel: 'Bookmark',
              ),
              onPressed: () {

              },
            ),
          ],
        ),
        body: Container(
          height: 2000,
          child: Stack(
            children: [
              FutureBuilder<dynamic>(
                future: _productRef.doc(widget.courseid).get(),
                builder: (context, snapshot) {
                  if (snapshot.hasError) {
                    return Scaffold(
                      body: Center(
                        child: Text("Error : ${snapshot.error}"),
                      ),
                    );
                  }
                  if (snapshot.connectionState == ConnectionState.done) {
                    Map<String, dynamic> documentData = snapshot.data.data();
                   return Scaffold(
                       body: Container(child: SfPdfViewer.network(documentData['pdfurl'])),

                       [enter image description here][1] );


                  }




                  return Scaffold(
                    body: Center(
                      child: CircularProgressIndicator(
                        color: Color(0xff005AA6),
                      ),
                    ),
                  );
                },
              ),











            ],
          ),
        )
    );
  }
}

【问题讨论】:

  • 问“请帮助我”的问题往往是寻求高度本地化的指导,或者在某些情况下,是不适合我们的问答形式的持续或私人帮助。它也相当模糊,最好用更具体的问题代替。请阅读Why is "Can someone help me?" not an actual question?

标签: firebase flutter syncfusion pdf-viewer


【解决方案1】:

以下代码说明了从 Syncfusion Flutter PDFViewer 中的 firebase 存储加载 PDF 文档。 getPdfBytes( ) 方法用于将 Firebase 存储的 PDF 文档转换为字节,然后使用 SfPdfViewer.memory 加载字节,用于从内存或字节加载 PDF 文档。

代码片段:

Uint8List? _documentBytes;
 
String path =
'https://firebasestorage.googleapis.com/v0/b/flutterfirebase-6c279.appspot.com/o/GIS.pdf?alt=media&token=51654170-c140-4ffa-ae1a-9fb431d0dee2';
 
@override
void initState() {
  getPdfBytes();
  super.initState();
}
 
void getPdfBytes() async {
  if (kIsWeb) {
    firebase_storage.Reference pdfRef = firebase_storage.FirebaseStorage
            .instanceFor(bucket: 'flutter-web-app-edc4a.appspot.com')
        .refFromURL(path);
    //The size mentioned here is maximum size to download from firebase.
    await pdfRef.getData(104857600).then((value) {
      _documentBytes = value;
      setState(() {});
    });
  } else {
    HttpClient client = HttpClient();
    final Uri url = Uri.base.resolve(path);
    final HttpClientRequest request = await client.getUrl(url);
    final HttpClientResponse response = await request.close();
    _documentBytes = await consolidateHttpClientResponseBytes(response);
    setState(() {});
  }
}
 
@override
Widget build(BuildContext context) {
  Widget child = Center(child: CircularProgressIndicator());
  if (_documentBytes != null) {
    child = SfPdfViewer.memory(
      _documentBytes!,
    );
  }
  return Scaffold(
    appBar: AppBar(title: Text('Syncfusion Flutter PDF Viewer')),
    body: child,
  );
}

更多信息请参考以下知识库https://www.syncfusion.com/kb/12757/how-to-load-a-pdf-document-from-firebase-storage-in-syncfusion-flutter-pdfviewer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    相关资源
    最近更新 更多