首页 > 解决方案 > 在 Flutter 中将 base64 字符串转换为 PDF 文件

问题描述

我正在尝试显示 PDF 文件。但是我从服务器收到的 PDF 文件采用 Base64 字符串格式。有什么方法可以直接将 Base64 字符串显示到 PDF 查看器或 WebView 中而不将其保存到文件中。

标签: flutterdartbase64

解决方案


检查这个:https ://stackoverflow.com/a/55599926/305135

(以下代码是从上面的链接复制的)

这应该将 base64 编码的 pdf 数据转换为字节数组。

import 'packages:dart/convert.dart';

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

pdf图像插件似乎适合您显示 pdf 的需求。

代码应该大致是这样的:

导入“包:pdf/pdf.dart”;导入“包:图像/image.dart”;

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

推荐阅读