简体   繁体   English

如何在 Flutter 中解码 base64 PDF 字符串?

[英]How to decode base64 PDF string in Flutter?

I know there is a package called dart:convert which let me decode base64 image.我知道有一个名为 dart:convert 的包,它可以让我解码 base64 图像。 But apparently, it doesn't work with pdf files.但显然,它不适用于 pdf 文件。 How can I decode the base64 PDF file in Flutter?如何在 Flutter 中解码 base64 PDF 文件?

I want to store it in Firebase Storage (I know how to do it) but I need the File variable to do it.我想将它存储在 Firebase 存储中(我知道怎么做),但我需要 File 变量来做到这一点。

I have a web service written in node js where I have a POST route.我有一个用 node js 编写的 Web 服务,我有一个 POST 路由。 There, I create a pdf file and encode it to base 64. The response is a base64 string, look at the code.在那里,我创建了一个 pdf 文件并将其编码为 base 64。响应是一个 base64 字符串,查看代码。

router.post('/pdf', (req, res, next) => {
    //res.send('PDF');

    const fname = req.body.fname;
    const lname = req.body.lname;

    var documentDefinition = {
        content: [ write your pdf with pdfMake.org ],
        styles: { write your style };

    const pdfDoc = pdfMake.createPdf(documentDefinition);
    pdfDoc.getBase64((data) => {

        res.send({ "base64": data });

    });
});

As you can see, it returns the pdf as a base64 string.如您所见,它将 pdf 作为 base64 字符串返回。

Now, in Flutter, I have written this:现在,在 Flutter 中,我写了这个:

http.post("https://mypostaddreess.com",body: json.encode({"data1":"data"}))
              .then((response) {
            print("Response status: ${response.statusCode}");
            print("Response body: ${response.body}");

            var data = json.decode(response.body);
            var pdf = base64.decode(data["base64"]);

          });

}

I have the PDF in the variable 'pdf' as you see.如您所见,我在变量“pdf”中有 PDF。 But I don't know how to decode it to download the pdf or show it in my Flutter app.但我不知道如何解码它以下载 pdf 或在我的 Flutter 应用程序中显示它。

@SwiftingDuster @SwiftingDuster

a little added, maybe besides decoding, it's also necessary to create a pdf file and open it.补充一点,可能除了解码之外,还需要创建一个pdf文件并打开它。

createPdf() async {
    var bytes = base64Decode(widget.base64String.replaceAll('\n', ''));
    final output = await getTemporaryDirectory();
    final file = File("${output.path}/example.pdf");
    await file.writeAsBytes(bytes.buffer.asUint8List());

    print("${output.path}/example.pdf");
    await OpenFile.open("${output.path}/example.pdf");
    setState(() {});
}

library needed: 1. open_file 2. path_provider 3. pdf需要的库: 1. open_file 2. path_provider 3. pdf

I think it's better to get the BufferArray and convert it into a pdf file.我认为最好获取 BufferArray 并将其转换为 pdf 文件。

Check out my answer from here : Get pdf from blob data从这里查看我的答案: 从 blob 数据中获取 pdf

This should convert base64 encoded pdf data into a byte array.这应该将base64编码的 pdf 数据转换为字节数组。

import 'packages:dart/convert.dart';

List<int> pdfDataBytes = base64.decode(pdfBase64)
  .map((number) => int.parse(number));

The pdf and the image plugins seems to suit your needs for displaying pdf. pdf图像插件似乎适合您显示 pdf 的需求。

The code should be roughly like so:代码应该大致如下:

import 'package:pdf/pdf.dart';
import 'package:image/image.dart';

...
Image img = decodeImage(pdfDataBytes);
PdfImage image = PdfImage(
  pdf,
  image: img.data.buffer.asUint8List(),
  width: img.width,
  height: img.height);
// Display it somehow
...

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

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