Way to render 200+ page pdfs efficiently? #135
-
It seems to take a ton of CPU and makes tabs unresponsive |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
I have used Range-Request to achieve a optimal PDF loading. For example, on one of my projects I have this setup: const document = ref()
// ...
const { pdf: src, pages: numPages, info } = usePDF(document)
export default function () {
// composable content
// At some point of the app lifecycle...
function initLoad () {
documento.value = {
url: selectedDocument.value.bytes,
rangeChunkSize: 1024 ** 2,
disableStream: true,
disableAutoFetch: true
}
}
} The server must support Range-Request, here an example using python with werkzeug server: if stream.mimetype == 'application/pdf':
headers = [
("Content-Type", "application/pdf"),
("Accept-Ranges", "bytes"),
("Content-Encoding", "identity"),
("Access-Control-Expose-Headers", 'Accept-Ranges'),
]
stream_bytes = stream.read()
if request.range:
content_range = request.range.make_content_range(len(stream_bytes))
if content_range.stop <= len(stream_bytes):
headers.append(('Content-Range', f'bytes {str(content_range.start), str(content_range.stop), str(len(stream_bytes))}'))
stream_bytes = stream_bytes[content_range.start:content_range.stop]
headers.append(('Content-Length', len(stream_bytes)))
return request.make_response(stream_bytes, headers=headers, status=206)
elif content_range.stop > len(stream_bytes):
LOGGER.info(f"Range-Request de peticion invalida {request.range}")
return request.make_response(stream_bytes, headers=headers, status=416)
return request.make_response(stream_bytes, headers=headers, status=200) Another approach is loading the pages in a progressive way or batches, just load 50 pages at the beginning and put a placeholder where the pages are not required loading at the beginning, so when the placeholder appears on the viewport the page can starts loading. |
Beta Was this translation helpful? Give feedback.
-
This was my fix, but I believe the library should be doing this on it's own
|
Beta Was this translation helpful? Give feedback.
I have used Range-Request to achieve a optimal PDF loading.
For example, on one of my projects I have this setup:
The server must support Range-Request, here an example using python with werkzeug server: