首页 > 解决方案 > 在android上显示带有颤动的pdf

问题描述

您好,下面的代码显示了一个带有颤振的 pdf 文件,请参阅上述 pub 包但是当我在 android 上运行代码时是否出现以下错误?如何解决上述问题: PDFDocument document = waitit PDFDocument.fromAsset (fullPath);

包装advance_pdf_viewer

错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Exception: Error parsing asset file!
E/flutter (32201): #0      PDFDocument.fromAsset (package:advance_pdf_viewer/src/document.dart:66:7)

颤振代码

    Directory documentDirectory;
   if (Platform.isAndroid) {
     documentDirectory = await getExternalStorageDirectory();
   }
   else{
       documentDirectory=await getApplicationDocumentsDirectory();
   }
  String documentPath = documentDirectory.path;
  String fullPath = "$documentPath/example.pdf";
  print("FullPath: "+fullPath);
  File file = new File.fromUri(Uri.parse(fullPath));
  PDFDocument document = await PDFDocument.fromFile(file);
  Navigator.push(context, MaterialPageRoute(
    builder: (context) => PdfPreviewScreen(file: document,path:fullPath,rp: rptemp,)
   ));

例外:

Unhandled Exception: Exception: Error parsing asset file!
E/flutter ( 4693): #0      PDFDocument.fromAsset (package:advance_pdf_viewer/src/document.dart:66:7)

标签: flutterdart

解决方案


您正在从文件路径不是资产文件(与应用程序数据一起存储)加载 pdf,因此您需要使用插件的特定方法而不是从您在您的代码中使用的资产加载 pdf马上。

File file = new File.fromUri(Uri.parse(fullPath));
PDFDocument doc = await PDFDocument.fromFile(file);

您的代码应如下所示:

Directory documentDirectory;
   if (Platform.isAndroid) {
     documentDirectory = await getExternalStorageDirectory();
   }
   else{
       documentDirectory=await getApplicationDocumentsDirectory();
   }
  String documentPath = documentDirectory.path;
  String fullPath = "$documentPath/example.pdf";
  print("FullPath: "+fullPath);
  File file = new File.fromUri(Uri.parse(fullPath));
  PDFDocument document = await PDFDocument.fromFile(file);
  Navigator.push(context, MaterialPageRoute(
    builder: (context) => PdfPreviewScreen(file: document,path:fullPath,rp: rptemp,)
   ));

推荐阅读