Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rangeChunkSize Doesnt Work With Version 12.1.2 #2566

Closed
NourKhashan opened this issue Sep 13, 2024 · 15 comments
Closed

rangeChunkSize Doesnt Work With Version 12.1.2 #2566

NourKhashan opened this issue Sep 13, 2024 · 15 comments
Assignees
Labels
user support You've got a question, or a misconfiguration, or simply need a hint how to get things up and running

Comments

@NourKhashan
Copy link

NourKhashan commented Sep 13, 2024

I need your help!
I have Large File (1 GB), So I dont want to download whole file as I take alot of time to display fist page.
So I want to use rangeChunkSize to get 1 MB Bytes of file and when scrolling I want to get next 1 MB but it doesnt work the range header doesnt send, therefore whole file download.

Check this first
this is my Setting

import {pdfDefaultOptions} from 'ngx-extended-pdf-viewer';
// In Constructor I set defaultOptions 
pdfDefaultOptions.rangeChunkSize=1024*1024;
pdfDefaultOptions.disableRange=false;
pdfDefaultOptions.disableAutoFetch=true;
pdfDefaultOptions.disableStream =true;
//In ngOnInit I set pdfViewer and this  Is ngx-extended-pdf-viewer Component ID
this.pdfViewer.src = "URL To Get File From BackEnd";// Get Method

In Get Method In API I Set Response Header

 Response.Headers.AcceptRanges = "Bytes";
 Response.Headers.AccessControlAllowMethods = "GET,HEAD,PUT,POST,DELETE";
 Response.Headers.AccessControlAllowOrigin = "*";
 Response.Headers.AccessControlExposeHeaders = "Accept-Ranges,Content-Length,Content-Range";

Describe the bug
But in Request the range header doesnt sent with URL, and I use .net Core it support Range Header.
Do I need in backend to add another api with specific header before get request or what missconfigure that i do.

Version info
ngx-extended-pdf-viewe : 12.1.2
Angular CLI: 12.1.1
Node: 14.15.0

Thanks In Advance

@stephanrauh
Copy link
Owner

Good question... but I'm pretty sure these two settings are bad for you:

pdfDefaultOptions.disableAutoFetch=true;
pdfDefaultOptions.disableStream =true;

Just stick to the defaults. I also suspect your rangeChunkSize is too big. The default is 64 KB, and it works in my showcase without any configuration.

@stephanrauh
Copy link
Owner

I assume the second code snippet is C# code?

@NourKhashan
Copy link
Author

NourKhashan commented Sep 13, 2024

Yes it's c#

@NourKhashan
Copy link
Author

NourKhashan commented Sep 13, 2024

image
I tried to Comment rangeChunkSize and tried also to set it to 64 KB but still the same. Range Doesn't Send
I comment disableAutoFetch and disableStream

// pdfDefaultOptions.rangeChunkSize=1024*64;
pdfDefaultOptions.disableRange=false;
// pdfDefaultOptions.disableAutoFetch=true;
// pdfDefaultOptions.disableStream =true;

@stephanrauh
Copy link
Owner

The reponse header is more important than the request. The first response should look like this:
image

I've described the details here: https://pdfviewer.net/extended-pdf-viewer/range-requests

@stephanrauh stephanrauh self-assigned this Sep 14, 2024
@stephanrauh stephanrauh added the user support You've got a question, or a misconfiguration, or simply need a hint how to get things up and running label Sep 14, 2024
@NourKhashan
Copy link
Author

NourKhashan commented Sep 14, 2024

image
Yes I already followed this Link and this is my response
In first request the range is null -this is my problem range doesnt send- so according to link we return the whole file but it suppose for first call to not return the whole File it should return first chunk of file to enhancement Performance. I tried to not to return the whole file it gives me 500 Server Error.
After this In second request
in the request header range send
image
Which is incorrect. it doesnt even get first chunk.

@stephanrauh
Copy link
Owner

Ah, we're getting closer. To be honest, this is a part of the viewer I haven't fully understood.

If my research is correct, the first request is a traditional request without range requests. You know, in the beginning, the PDF viewer doesn't know whether the server supports range requests or not. When the PDF viewer detects that the server is able to process range requests, it cancels the first request and starts sending range requests.

It's the server's responsibility to stop sending data when the client has canceled the request. I don't know how you've implemented your server. I suspect you must care about canceling the request explicitly.

@stephanrauh
Copy link
Owner

@timvandermeij I know my idea doesn't make much sense for the Firefox PDF viewer - but still, I wonder if I should add a mode that starts requesting range requests immediately. Maybe I'm wrong, but to me, the current approach of fetching the entire file and canceling the request doesn't feel efficient.

What's your opinion? And is there a documentation explaining how pdf.js implements streaming and range requests?

@NourKhashan
Copy link
Author

NourKhashan commented Sep 15, 2024

I tried to cancel request and try to send StatusCode CancelRequest Or even tried to created empty bytes with same length but i gives me 500
So Do u mean it's normal that Range in First Request to be null and return whole File as this is get request?

I have others Question if u dont mind

  1. As I showed to u in Second Request it start to get last chucks first does it also normal? As I expexted to get First Chucks First

  2. Why u Recommend to not change disableAutoFetch and disableStream

    • as I understand if i set disableStream to true so it means to stop stream and work with Chunks
    • And For disableAutoFetch When i Set it to True, I noticed that number of request will be decreased. as If i didn't disable it
      it will 'automatically keep fetching more data even if it isn't needed to display the current page'
      as per documentation Options
  3. Is there a way to determined how many chunks to load first -or set no. of Pages To Load First and send request based on it I know for each page has different size -and when I scroll Load the related chunks as I see there is a lot of partial request to get whole file and this a lot of requests?

  4. I tried to change pdfDefaultOptions.rangeChunkSize I dont want default value -64KB- but it doesnt reflect. I set it in ctor. Do I have to change it in another place?

@timvandermeij
Copy link

timvandermeij commented Sep 15, 2024

@timvandermeij I know my idea doesn't make much sense for the Firefox PDF viewer - but still, I wonder if I should add a mode that starts requesting range requests immediately. Maybe I'm wrong, but to me, the current approach of fetching the entire file and canceling the request doesn't feel efficient.

What's your opinion? And is there a documentation explaining how pdf.js implements streaming and range requests?

Documentation, and my knowledge, about this is a bit sparse unfortunately, but I think mozilla/pdf.js#11669 (comment) explains the situation well and should still be correct. There is also d0297cfb97c14385/src/display/api.js#L197-L206, mozilla/pdf.js#8768 and mozilla/pdf.js#6126 that might help. If you would like to know more, you can also ask in the PDF.js Matrix room; please see https://github.com/mozilla/pdf.js?tab=readme-ov-file#questions.

@NourKhashan
Copy link
Author

NourKhashan commented Sep 15, 2024

Ah Thanks, I also can't find plenty and suitable resources. But I will continue digging in this.
So thanks again I appreciate ur time.

@stephanrauh
Copy link
Owner

@NourKhashan You can debug what's going on in the client side. Just set the attribute [minifiedJSLibraries]="false" and locate the code dealing with range requests. I'm not entirely sure, but I think it's somewhere in this class: https://github.com/stephanrauh/pdf.js/blob/3b6b63aad3e986e49731c08bc314c543f6c13565/src/core/worker.js#L266

@stephanrauh
Copy link
Owner

Oh, and I'm 99% sure the issue is on the server side. On the client side, range requests work out-of-the-box if the server supports them.

Whether tweaking the size of the chunks is a good idea or not probably depends on the size of the PDF pages. For most documents, 64K is a good choice.

@NourKhashan
Copy link
Author

In Server Side I do as documentation and in documentation in Get Request it return whole File if Range Request Is Null, Range is send as null from Client Side and this is point and it take alot of time.
After this request range is send but not for beginning - but this is a proof that server support range- .
If this Related to Server Side Does it related to Response Headers As I showd to u Request and Response Header For First Request.

@stephanrauh
Copy link
Owner

I'm closing this ticket now because I think I've told you everything I know about the topic and because I don't support old version of ngx-extended-pdf-viewer. I'd like to support old versions, but it's simply not possible because I don't have enough time to spare, so I reluctantly focus on the current version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
user support You've got a question, or a misconfiguration, or simply need a hint how to get things up and running
Projects
None yet
Development

No branches or pull requests

3 participants