简体   繁体   English

我如何以 flutter 格式以 base64 格式显示 pdf

[英]How can i show pdf in base64 format in flutter

I have a table in postgresql that has a field with a pdf file in base64 format.我有一个 postgresql 表格,其中有一个字段,其中包含 base64 格式的 pdf 文件。 When I get it from flutter, how can I show it with pdf viewer?, has anyone done it??当我从 flutter 得到它时,如何用 pdf 查看器显示它?,有人做过吗?

I appreciate your help我感谢您的帮助

In pubspec.yaml add the following dependency:在 pubspec.yaml 添加以下依赖项:

dependencies:
  flutter_full_pdf_viewer: ^1.0.6 # for pdf

Then run flutter pub get in Android Studio terminal to update dependencies.然后在 Android Studio 终端中运行flutter pub get以更新依赖项。

Create the file 'pdfBase64Viewer.dart:创建文件'pdfBase64Viewer.dart:

import 'dart:async'; // asynchroneous function (await)
import 'dart:io'; // write the file on user's phone
import 'dart:convert'; // handle base64 decoding
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_full_pdf_viewer/full_pdf_viewer_scaffold.dart';
import 'package:path_provider/path_provider.dart';


class PdfBase64Viewer extends StatefulWidget {
  final String url;

  const PdfBase64Viewer(String this.url);
  @override
  _PdfBase64ViewerState createState() => new _PdfBase64ViewerState();
}

class _PdfBase64ViewerState extends State<PdfBase64Viewer> {
  String pathPDF = "";

  @override
  void initState() {
    super.initState();
    createFileOfPdfUrl(widget.url).then((f) {
      setState(() {
        pathPDF = f.path;
        print(pathPDF);
      });
    });
  }


  Future<File> createFileOfPdfUrl(is_base_64) async {
    final filename = widget.url.substring(widget.url.lastIndexOf("/") + 1);

    var request = await HttpClient().getUrl(Uri.parse(widget.url));
    var response = await request.close();
    var bytes = await consolidateHttpClientResponseBytes(response);
    var base64String = utf8.decode(bytes); 
    var decodedBytes = base64Decode(base64String.replaceAll('\n', ''));

    String dir = (await getApplicationDocumentsDirectory()).path;
    File file = new File('$dir/$filename');
    await file.writeAsBytes(decodedBytes.buffer.asUint8List());

    return file;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Plugin example app')),
      body: Center(
        child: RaisedButton(
          child: Text("Open PDF"),
          onPressed: () => Navigator.push(
            context,
            MaterialPageRoute(builder: (context) => PDFScreen(pathPDF)),
          ),
        ),
      ),
    );
  }
}

class PDFScreen extends StatelessWidget {
  String pathPDF = "";
  PDFScreen(this.pathPDF);

  @override
  Widget build(BuildContext context) {
    return PDFViewerScaffold(
        appBar: AppBar(
          title: Text("Document"),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.share),
              onPressed: () {},
            ),
          ],
        ),
        path: pathPDF);
  }
}

You can adapt above class in order to assign to the variable base64String the String value that you get from your DB.您可以在 class 上方进行调整,以便将您从数据库中获得的字符串值分配给变量 base64String。

You can use it the following way:您可以通过以下方式使用它:

import 'pdfBase64Viewer.dart';

(...)
child: PdfBase64Viewer("https://filebin.net/qmvmxbgn6pzgkbzb/sample.base64?t=7g5s9rwr") // url of your base64 file
(...)

You might want to check the following documentation:您可能需要查看以下文档:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM