packages.flutter icon indicating copy to clipboard operation
packages.flutter copied to clipboard

Web "Exception: Unknown Error"

Open gisborne opened this issue 2 years ago • 6 comments

Describe the bug This code:

class PDFWidget extends StatelessWidget {
  final String path;
  final Future<PdfDocument>_page;
  late final PdfControllerPinch _controller;

  PDFWidget({
      required
    String this.path,
    Key? key
  }):
      _page = path.startsWith('http')
        ? PdfDocument.openData(InternetFile.get(path)) : PdfDocument.openAsset(path),
      super(key: key) {
        _controller = PdfControllerPinch(document: _page);
      }

  @override
  Widget build(BuildContext context) {
    return PdfViewPinch(controller: _controller);
  }
}

works fine on desktop and mobile, but all I see where the pdf should be in web is "Exception: Unknown Error".

To Reproduce Steps to reproduce the behavior:

  1. Set up assets with a pdf
  2. Render the above object with a suitable asset path.

Expected behavior The pdf should display.

Additional context MacOS M1.

Flutter (Channel stable, 2.10.3, on macOS 12.2.1 21D62 darwin-arm, locale en-US) • Flutter version 2.10.3 at /opt/homebrew/Caskroom/flutter/2.2.1/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 7e9793dee1 (7 days ago), 2022-03-02 11:23:12 -0600 • Engine revision bd539267b4 • Dart version 2.16.1 • DevTools version 2.9.2

gisborne avatar Mar 09 '22 23:03 gisborne

we are having the same issue in web also

DevFauche avatar Mar 14 '22 13:03 DevFauche

@gisborne @DevFauche Please re-check installation guide.

  1. Runned or not command flutter pub run pdfx:install_web in project folder is have file web/index.html same code: https://github.com/ScerIO/packages.flutter/blob/546ada85da994d1273f9208357ebda7a1f6435be/packages/pdfx/example/web/index.html#L35-L43 If code not exist add manually
  2. Target host requires CORS policy. For example:
// Not works. www.africau.edu not support CORS for any hosts
   PdfControllerPinch(
      document: PdfDocument.openData(
        InternetFile.get(
          'http://www.africau.edu/images/default/sample.pdf',
        ),
      ),
    );
    
 // Works, proxy add header with CORS awailable for XHR request anywhere
    PdfControllerPinch(
      document: PdfDocument.openData(
        InternetFile.get(
          'https://api.codetabs.com/v1/proxy/?quest=http://www.africau.edu/images/default/sample.pdf',
        ),
      ),
    );

SergeShkurko avatar Mar 14 '22 19:03 SergeShkurko

@gisborne @DevFauche Please re-check installation guide.

  1. Runned or not command flutter pub run pdfx:install_web in project folder is have file web/index.html same code: https://github.com/ScerIO/packages.flutter/blob/546ada85da994d1273f9208357ebda7a1f6435be/packages/pdfx/example/web/index.html#L35-L43

    If code not exist add manually

  2. Target host requires CORS policy. For example:

// Not works. www.africau.edu not support CORS for any hosts
   PdfControllerPinch(
      document: PdfDocument.openData(
        InternetFile.get(
          'http://www.africau.edu/images/default/sample.pdf',
        ),
      ),
    );
    
 // Works, proxy add header with CORS awailable for XHR request anywhere
    PdfControllerPinch(
      document: PdfDocument.openData(
        InternetFile.get(
          'https://api.codetabs.com/v1/proxy/?quest=http://www.africau.edu/images/default/sample.pdf',
        ),
      ),
    );

The code in point one helped me to get the package working on web. Thanks. (I am only trying to load from assets directory)

OppositeDragon avatar Apr 08 '22 18:04 OppositeDragon

The command flutter pub run pdfx:install_web did not do anything in my project, but the output of the command showed "success"

OppositeDragon avatar Apr 08 '22 18:04 OppositeDragon

The command flutter pub run pdfx:install_web did not do anything in my project, but the output of the command showed "success"

@SergeShkurko can you fix this?

ricardoboss avatar Jun 13 '22 17:06 ricardoboss

Also seeing this - running the above command got me this:

[Modify web/index.hml by addition pdfjs]
Already installed, operation aborted
pub finished with exit code 2

In my case, the PDF is rendering fine in a local build but gives me a 404 when deployed. I also checked my index.html and it has the script lines added in your above comment. Any updates or things to try @SergeShkurko ?

pelusodan avatar Jul 26 '22 19:07 pelusodan