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 particular setup, not a universal browser limit. It is a useful prompt to separate the size of a PDF file from the size of the pixels used to display it.

Count pixels before rendering

Suppose a page occupies 800 by 1,035 CSS pixels. With a device pixel ratio of 2, a full-resolution backing canvas is 1,600 by 2,070 pixels. At four bytes per RGBA pixel, the raw pixel buffer alone is about 12.6 MiB. At a ratio of 3, it is about 28.4 MiB. Five such pages at ratio 3 would need roughly 142 MiB for those buffers alone.

Those are estimates, not measurements of total browser memory. Decoded images, PDF structures, GPU copies and text layers can add to the footprint. A browser may also impose separate maximum dimensions or area limits, and those limits vary by device.

The PDF.js rendering example distinguishes CSS dimensions from the larger backing canvas and applies an output transform for high-density screens. A custom viewer can add a per-page budget before assigning canvas dimensions:

function chooseOutputScale(width, height, dpr) { const maxPixels = 8_000_000; // Example budget, not a browser guarantee. const maxSide = 8192; // Test and tune for your supported devices. return Math.min( dpr || 1, Math.sqrt(maxPixels / (width * height)), maxSide / width, maxSide / height ); } async function renderPage(page, canvas, cssWidth) { const base = page.getViewport({ scale: 1 }); const viewport = page.getViewport({ scale: cssWidth / base.width }); const outputScale = chooseOutputScale( viewport.width, viewport.height, window.devicePixelRatio ); canvas.width = Math.max(1, Math.floor(viewport.width * outputScale)); canvas.height = Math.max(1, Math.floor(viewport.height * outputScale)); canvas.style.width = `${viewport.width}px`; canvas.style.height = `${viewport.height}px`; const task = page.render({ canvasContext: canvas.getContext('2d'), viewport, transform: [outputScale, 0, 0, outputScale, 0, 0] }); await task.promise; } 

This is a sizing example for a custom PDF.js canvas integration. It assumes valid, positive viewport dimensions and one render at a time for the canvas. The values are application budgets, not claims about what a browser can allocate. A smaller output scale can make text look soft; keep the CSS page size stable and communicate when a preview has reduced resolution. For very large pages or high zoom, tiled rendering is another option, with more implementation work.

The built-in PDF.js viewer has its own canvas sizing controls; check the version you ship before adding a second layer of limits. The discussion on the issue links to its maxCanvasPixels setting. A cap on the output canvas also does not bound every embedded image allocation during decoding.

Keep only the pages a reader needs

A per-page cap still permits a large total if a viewer holds dozens of canvases. Render visible pages and a small buffer around them, then release canvases that are far away. Keep placeholders at the correct page dimensions so removing pixels does not collapse the scroll position. Documents with mixed page sizes need per-page placeholder dimensions.

Scrolling and zooming can overlap with rendering. Cancel or wait for the old render task before reusing its canvas; the PDF.js example explicitly avoids drawing two pages into the same canvas at once. Discard stale results after a zoom or document switch, and render a fresh page when it returns to the visible window. Releasing a canvas means clearing references as well as removing it from the page; memory reclamation timing remains up to the browser.

Check the failure on real devices

Record the CSS page size, output scale, backing dimensions and number of live page canvases when a preview fails. Compare a phone and desktop at the same zoom, then test a long document, a wide drawing and mixed portrait/landscape pages. Scroll away and back, resize the window, and zoom while a page is still rendering.

Check that text selection and form controls still align with the displayed page. Those layers may use the CSS viewport even when the canvas backing resolution is reduced. A canvas budget can make the picture cheaper without changing the coordinates used by the interactive layers.

For another source of memory growth in a document viewer, see when to revoke PDF preview blob URLs. Blob URL lifetime and canvas pixel memory are separate costs, so both deserve a look when a tab keeps growing.