pdfrx icon indicating copy to clipboard operation
pdfrx copied to clipboard

Very Slow loading of some PDF files compared to other pdf viewers

Open Ohrest88 opened this issue 10 months ago • 7 comments

First of all, thanks for this great multi-platform flutter package!

Pdfrx seems to have performance issues in some PDF files when compared to some other pdf-viewers, this is more noticeable with PDFs with many pages.

How to reproduce:

Download locally this publicly available pdf file: https://ia801408.us.archive.org/6/items/ERIC_ED619049/ED619049.pdf

On Android mid-range device:

  1. Open it with ReadEra -> Takes ~1 second for the viewer to open
  2. Open it with Firefox -> Takes ~1 second for the Firefox pdf viewer to open

And/OR On Linux:

  1. Open it with Chromium web browser -> takes ~ 1 second
  2. Open it with Firefox -> takes ~1 second

However, opening the same file for viewing with pdfrx on Flutter takes ~4 seconds on both Android and Linux (same Linux and Android hardware as above)

The code I used was the minimum example:

class _MainPageState extends State<MainPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Pdfrx example'),
      ),
      body: PdfViewer.asset(
        'assets/ED619049.pdf',
      ),
    );
  }
}

Environment I tested with

  • Flutter version: 3.29.0
  • pdfrx version: 1.1.5
  • Platforms tested:
    • Linux (Ubuntu 24.04)
    • Android
  • PDF details:
    • Size: 30 MB
    • Pages: 1574

What I tried

  1. I tried playing with different parameters found in pdf_viewer_params.dart, for example useAlternativeFitScaleAsMinScale, enableTextSelection, maxImageBytesCachedOnMemory, however, these changes did not yield any substantial improvements
  1. I tried opening the pdf using PdfPageView class, hoping to enhance performance by loading only 1 page. However, this approach did not reduce the delay, opening the pdf file for viewing take the same amount of time.

The pdf file above is an example that illustrates the issue, but I noticed this pattern with other PDF files too

Is there a known solution for this problem?

Ohrest88 avatar Feb 18 '25 14:02 Ohrest88

Because of the current design of pdfrx, when it opens a new document, it firstly loads all the pages to know whole document structure. And it may be the cause of the slowness.

Other PDF viewers just loads only the first page (or anyway several pages only) to reduce the time to show the first page.

Basically, this is just a design dicision and it's not difficult to implement the second approach on pdfrx but it breaks compatibility with the older versions...

Anyway, I'll consider more about the dicision again.

espresso3389 avatar Feb 22 '25 08:02 espresso3389

Basically, this is just a design dicision and it's not difficult to implement the second approach on pdfrx but it breaks compatibility with the older versions...

I see, this type of feature would take the form of a boolean parameter called something like lazyLoading ?

Ohrest88 avatar Feb 22 '25 11:02 Ohrest88

No, it breaks lower layer API compatibility and could not co-exist with the existing codes.

BTW, even Flutter framework takes such kind of incompatibilities on every featured release. So it can be acceptable to introduce such level of breaking changes I guess.

espresso3389 avatar Feb 22 '25 11:02 espresso3389

Thanks for getting back on this issue.

My two cents on this: The benefits of improving load times of big PDFs through this feature would out-weight the drawbacks of breaking backward compatibility with pre-existing codes that use previous versions of pdfrx

Ohrest88 avatar Feb 22 '25 12:02 Ohrest88

Yes, I believe so. OK, I'll try to introduce some asynchronous loading mechanism.

espresso3389 avatar Feb 22 '25 13:02 espresso3389

Yes, I believe so. OK, I'll try to introduce some asynchronous loading mechanism.

thank you so much! my users load pdfs with over 1500 pages. takes 4-5 seconds to load. I appreciate your work! Many thanks!

Reyrey14-1 avatar Mar 13 '25 18:03 Reyrey14-1

the same as me, it takes around 10 seconds to load large pdf. great package btw, thanks!

dwexid avatar Mar 24 '25 12:03 dwexid

https://github.com/espresso3389/pdfrx/issues/393#issuecomment-2961780681

[@FeofanGreek] In my opinion, may be possible do two stage loading? This is only my thing. On first stage we can calculate pages, sizes, may be some thing more "quick" variables. On the second stage do scaning document for search, draw annotations and bookmarks... Yes, I totally agree to your opinion. The approach may:

Delay loading of PdfDocument.pages Certain apps depends on pdfrx does not assume pages will change after first loading We should need some flags to activate the delay loading?? getPages function (async version)? Delay loading notification callback (on pages change) PdfViewer(initialPageNumber) may wait for the page being loaded Meaning of isReady Anyway, it apparently breaks "some" compatibility with the existing codes... Should we do major upgrade for that?

espresso3389 avatar Jun 11 '25 08:06 espresso3389

I'm working on lazy_page_load branch now.

espresso3389 avatar Jun 11 '25 17:06 espresso3389

pdfrx 1.2.0 (6964170ee4557b907e148acfba9fab7084c8e3ca) contains the changes to support lazy loading of the pages.

But the release also contains breaking changes that removes supports of Pdf.js.

Anyway, try the latest version!

espresso3389 avatar Jun 12 '25 17:06 espresso3389

pdfrx 1.2.0 (6964170) contains the changes to support lazy loading of the pages.

But the release also contains breaking changes that removes supports of Pdf.js.

Anyway, try the latest version!

works great! thank you!

Reyrey14-1 avatar Jun 21 '25 00:06 Reyrey14-1