我需要從本地存盤器中打開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,
格式。PdfPageFormat.JPEG,
);
return pageImage。
uj5u.com熱心網友回復:
試試這個!
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;
檔案?
; Uint8List?
void initState() {
super.initState()。
}
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
標題。Text("Test")。
centerTitle: true,
),
身體。Container(child: mainWid(context))。
floatingActionButton。FloatingActionButton(
onPressed: () async {
getFile();
},
孩子。Icon(Icons.add),
),
);
}
Widget mainWid(BuildContext context) {
return isLoaded == true ?
? 中心(
孩子。圖片(
影像。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"/span>) {
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!.byte;
setState(() {})。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/329851.html
標籤:
