Why PDF previews go blank on high-density screens
A PDF preview can look fine on a laptop and go blank on a high-density phone. The file may be small on disk, yet the viewer allocates a large canvas for every rendered page. Zoom and device pixel ratio multiply both canvas dimensions, so memory grows with the square of the scale. A PDF.js issue describes a failure with large scanned drawings on high-resolution screens. That report concerns a…
High-density screens can cause PDF previews to appear blank due to memory constraints. Even though a PDF file may be small, the viewer assigns a large canvas for each rendered page, which multiplies the dimensions based on the device's pixel ratio. This leads to a significant increase in memory usage, as the raw pixel buffer for a single page can reach several dozen megabytes at high resolutions.
A suggested approach is to set a per-page budget for canvas dimensions, ensuring that the canvas size does not exceed a predetermined limit, such as 8 million pixels. This budget can help prevent excessive memory consumption while still providing a high-quality preview. By rendering only the visible pages and their surrounding pages, and releasing canvases that are far away, developers can optimize memory usage.
Additionally, canceling or waiting for the old render task before reusing a canvas can help avoid memory leaks. It's essential to test the preview on various devices, considering factors like scrolling, zooming, and document orientation. Checking the failure on real devices and comparing different scenarios can help identify memory usage issues.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to