无法使用 PdfDocument.openFile(path) 从本地存储打开 pdf - Flutter
Not able to open pdf from localstorage by using PdfDocument.openFile(path) - Flutter
我需要从本地存储打开一个 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(() {});
}
}
我需要从本地存储打开一个 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(() {});
}
}