pdfrx 0.2.4 copy "pdfrx: ^0.2.4" to clipboard
pdfrx: ^0.2.4 copied to clipboard

Yet another PDF renderer for Flutter using PDFium.

pdfrx #

pdfrx is a PDF viewer implementation that built on the top of pdfium. The plugin currently supports Android, iOS, Windows, macOS, Linux, and Web.

Please note that "Web" is not shown in pub.dev's platform list, but IT DOES SUPPORT Web.

Getting Started #

The following fragment illustrates the easiest way to show a PDF file in assets:

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Pdfrx example'),
        ),
        body: PdfViewer(
          document: PdfDocument.openAsset('assets/hello.pdf'),
        ),
      ),
    );
  }
}

Installation #

Add this to your package's pubspec.yaml file and execute flutter pub get:

dependencies:
  pdfrx: ^0.2.4

Web #

For Web, you should add the following <script> block to your index.html just before <script src="main.dart.js"... </script> to load PDF.js:

<!-- IMPORTANT: load pdfjs files -->
<script
  src="https://cdn.jsdelivr.net/npm/pdfjs-dist@3.11.174/build/pdf.min.js"
  type="text/javascript"
></script>
<script type="text/javascript">
  pdfjsLib.GlobalWorkerOptions.workerSrc =
    "https://cdn.jsdelivr.net/npm/pdfjs-dist@3.11.174/build/pdf.worker.min.js";
  pdfRenderOptions = {
    // where cmaps are downloaded from
    cMapUrl: "https://cdn.jsdelivr.net/npm/pdfjs-dist@3.11.174/cmaps/",
    // The cmaps are compressed in the case
    cMapPacked: true,
    // any other options for pdfjsLib.getDocument.
    // params: {}
  };
</script>

Please check example's code for the actual usage.

Please note that; with pdf.js 4.0 series, they changed the way to load pdfjsLib, it does not simply work with the current code. We need further investigation to support them.

macOS #

For macOS, Flutter app restrict its capability by enabling App Sandbox by default. You can change the behavior by editing your app's entitlements files depending on your configuration. See the discussion below.

Deal with App Sandbox #

The easiest option to access files on your disk, set com.apple.security.app-sandbox to false on your entitlements file though it is not recommended for releasing apps because it completely disables App Sandbox.

Another option is to use com.apple.security.files.user-selected.read-only along with file_selector_macos. The option is better in security than the previous option.

Anyway, the example code for the plugin illustrates how to download and preview internet hosted PDF file. It uses com.apple.security.network.client along with flutter_cache_manager:

<dict>
  <key>com.apple.security.app-sandbox</key>
  <true/>
  <key>com.apple.security.network.client</key>
  <true/>
</dict>