gpt4 book ai didi

flutter - 无法使用 PdfDocument.openFile(path) 从本地存储打开 pdf - Flutter

转载 作者:行者123 更新时间:2023-12-04 17:12:06 25 4
gpt4 key购买 nike

我需要从本地存储中打开一个 pdf 文件,这里我使用 native_pdf_renderer: ^3.1.0包用于此目的。根据文档,使用方法 PdfDocument.openFile('path/to/file/on/device') 将从本地存储中获取文件。但在我的例子中它不起作用,显示错误 PlatformException (PlatformException(PDF_RENDER, Can't open file, null, null))。其他方法 PdfDocument.openAsset('assets/sample.pdf')PdfDocument.openData(uint8Data) 工作正常。如何让它发挥作用?

 FilePickerResult result = await FilePicker.platform.pickFiles(
allowMultiple: false,
);

File doc = File(result.files.single.path);

PdfPageImage thePdfThumbNail = await getSinglePageImage(doc.path);
...
Future<PdfPageImage> getSinglePageImage(String path) async {
PdfDocument newDoc = await PdfDocument.openFile(path);
final page = await newDoc.getPage(1);
final pageImage = await page.render(
width: page.width,
height: page.height,
format: PdfPageFormat.JPEG,
);
return pageImage;
}

最佳答案

试试这个!

import 'dart:io';
import 'dart:typed_data';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:native_pdf_renderer/native_pdf_renderer.dart';

class Test extends StatefulWidget {
_Test createState() => _Test();
}

class _Test extends State<Test> {
bool? isLoaded = false;
Uint8List? theImage;
File? file;

@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Test"),
centerTitle: true,
),
body: Container(child: mainWid(context)),
floatingActionButton: FloatingActionButton(
onPressed: () async {
getFile();
},
child: Icon(Icons.add),
),
);
}

Widget mainWid(BuildContext context) {
return isLoaded == true
? Center(
child: Image(
image: MemoryImage(theImage!),
),
)
: CircularProgressIndicator();
}

Future<void> getFile() async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
file = File(result!.files.single.path.toString());
var fileType = file!.path.split(".").last;
if (fileType == "pdf") {
getImage(file);
}
}

Future<void> getImage(File? file) async {
final doc = await PdfDocument.openFile(file!.path);
final page = await doc.getPage(1);
final pageImage = await page.render(width: page.width, height: page.height);
isLoaded = true;
theImage = pageImage!.bytes;
setState(() {});
}
}

关于flutter - 无法使用 PdfDocument.openFile(path) 从本地存储打开 pdf - Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69238047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com