skip to Main Content

Was Using Syncfusion PDF Viewer but it’s displaying grey screen upon release. Any other alternative solutions for pdf viewers in Flutter Web that supports network file?

2

Answers


  1. For the web platform, we have used PdfJs for rendering the PDF pages, so the script file must be referred to in your web/index.html file. On your web/index.html file, add the following script tags, somewhere in the head or body of the document:

    Script:

    pdfjsLib.GlobalWorkerOptions.workerSrc = “//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.4.456/pdf.worker.min.js”;

    UG documentation link: https://help.syncfusion.com/flutter/pdf-viewer/getting-started#add-the-flutter-pdf-viewer-to-an-application

    To load PDF from network using SfPdfViewer.network in macOS, network access must be enabled in your macOS application. On your macos/Runner/DebugProfile.entitlements file, add the following lines inside the tag to enable the network access in your application:

    com.apple.security.network.client

    UG documentation link: https://help.syncfusion.com/flutter/pdf-viewer/getting-started#load-document-from-the-network

    Login or Signup to reply.
  2. Here’s your solution.

    So, this is the output of the below-mentioned source code: View

    Source code:

    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key});
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      PdfControllerPinch? pdfController;
    
      @override
      void initState() {
        super.initState();
        WidgetsBinding.instance.addPostFrameCallback(
          (Duration timeStamp) async {
            const String url = "https://www.africau.edu/images/default/sample.pdf";
            final Uri parsedUri = Uri.parse(url);
            final Response res = await get(parsedUri);
            final Future<PdfDocument> doc = PdfDocument.openData(res.bodyBytes);
            pdfController = PdfControllerPinch(document: doc);
            setState(() {});
          },
        );
      }
    
      @override
      void dispose() {
        pdfController?.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: pdfController != null
              ? PdfViewPinch(controller: pdfController!)
              : const CircularProgressIndicator(),
        );
      }
      
    

    Dependent packages: http & pdfx

    Note: After installing the pdfx package, run the below-mentioned command:

    flutter pub run pdfx:install_web
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search