DEV Community

KomoPDF
KomoPDF

Posted on

Why a PDF range request downloads the whole file

A PDF viewer asks for the first 64 KiB of a 40 MiB file, but the network panel shows a 40 MiB response. The request included Range: bytes=0-65535; the server replied 200 OK and sent the whole file. The viewer may still display the document, while the bandwidth and startup behavior differ from what the developer expected.

Range support is an agreement between the client, the server, and any proxy or CDN in between. Here's how to check the actual response before changing viewer code.

Read the status and byte range together

Request a small slice from the deployed URL:

curl -sS -D - -o /dev/null -H 'Range: bytes=0-65535' 'https://example.com/document.pdf'
Enter fullscreen mode Exit fullscreen mode

For a file at least 65,536 bytes long, a successful single-range response can look like this:

HTTP/1.1 206 Partial Content
Content-Range: bytes 0-65535/41943040
Content-Length: 65536
Accept-Ranges: bytes
Enter fullscreen mode Exit fullscreen mode

The endpoints in Content-Range are inclusive. Content-Length here describes the response body, while the number after the slash describes the full representation. A smaller file can produce a shorter range. An Accept-Ranges: bytes header is useful evidence, but check a real GET too: headers from a HEAD request alone don't prove that your deployed path will return the expected bytes.

A server may ignore Range and return 200 with the complete representation. Treat that as a full download, not as the requested chunk. A 416 Range Not Satisfiable response means the requested range cannot be satisfied; its Content-Range can report the current size as bytes */41943040. See MDN's range request guide and Content-Range reference.

Test the last bytes and a second request

A PDF reader may need data from different parts of the file. Verify a suffix request as well as a request near the start:

curl -sS -D - -o /dev/null -H 'Range: bytes=-1024' 'https://example.com/document.pdf'
Enter fullscreen mode Exit fullscreen mode

For a file larger than 1,024 bytes, compare the returned interval and length with the full size. Test the same public URL the browser uses, including redirects and authentication where applicable. A storage origin may support ranges while a proxy, signed URL, or cache in the actual path changes the behavior. Avoid putting credentials or sensitive signed URLs in shared logs.

A range-capable server doesn't guarantee that the viewer will fetch only the first page. Its parser, document layout, prefetch settings, and server responses all influence requests. Look at transferred bytes and request timing for the actual document instead of assuming a fixed bandwidth saving.

Check cross-origin visibility

A request can succeed in the network panel while JavaScript cannot read every response header. When the viewer and PDF are on different origins, configure the PDF server's CORS policy for the viewer origin. If the application needs to inspect range metadata, expose the relevant headers, for example:

Access-Control-Allow-Origin: https://viewer.example
Access-Control-Expose-Headers: Accept-Ranges, Content-Range, Content-Length, ETag
Enter fullscreen mode Exit fullscreen mode

Apply the policy to the responses the browser receives, including partial responses and relevant error responses. Credentialed requests need a compatible explicit origin and credential policy. A terminal curl test doesn't enforce browser CORS, so repeat the check in the browser's network and console panels. MDN documents header exposure.

Keep chunks from the same version

A file can change between requests. Combining bytes from an old version with bytes from a new one can produce a corrupt document, even when both requests returned 206. Prefer immutable, versioned URLs for a document. For resumable requests, conditional range requests with an appropriate validator allow the server to return the complete current representation when the saved version no longer matches. On a 200 response, discard old partial data instead of appending the new body to it. See MDN on If-Range.

Finally, test a genuinely out-of-range offset, a cross-origin request, and a file replacement at the same URL. Record status, Content-Range, transferred bytes, and the version validator for each request. Those observations separate a viewer parsing problem from a delivery path that silently sent the entire file.

This article continues the PDF viewer compatibility series. For local previews and their memory lifetime, see when to revoke blob URLs.

Top comments (0)