Skip to content

Fix blurry PDF text on mobile Safari by accounting for devicePixelRatio#227

Merged
simonw merged 1 commit intomainfrom
claude/fix-pdf-mobile-blur-tKlwr
Feb 17, 2026
Merged

Fix blurry PDF text on mobile Safari by accounting for devicePixelRatio#227
simonw merged 1 commit intomainfrom
claude/fix-pdf-mobile-blur-tKlwr

Conversation

@simonw
Copy link
Owner

@simonw simonw commented Feb 17, 2026

https://tools.simonwillison.net/view-pdf?url=https%3A%2F%2Fstatic.simonwillison.net%2Fstatic%2Fcors-allow%2F2025%2Frimu_kakapo_breeding_brief.pdf has text that appears blurry on mobile safari, theories as to why? Looks good on desktop

Canvas was rendered at CSS pixel dimensions, causing blurry upscaling on
high-DPI screens (mobile Safari typically has devicePixelRatio of 2-3).
Now renders at native device resolution and uses CSS to scale the canvas
element back to logical size.

https://claude.ai/code/session_01YcR4QgziQReqarBy3HuxcH

Canvas was rendered at CSS pixel dimensions, causing blurry upscaling on
high-DPI screens (mobile Safari typically has devicePixelRatio of 2-3).
Now renders at native device resolution and uses CSS to scale the canvas
element back to logical size.

https://claude.ai/code/session_01YcR4QgziQReqarBy3HuxcH
@claude
Copy link
Contributor

claude bot commented Feb 17, 2026

Claude finished @simonw's task —— View job


PR Review Complete ✅

I've reviewed the PDF mobile blur fix. Here's my comprehensive feedback:

  • Analyze the changed file (view-pdf.html)
  • Review code quality and implementation approach
  • Check for potential bugs or edge cases
  • Evaluate performance impact
  • Assess security considerations
  • Consider test coverage needs
  • Provide comprehensive feedback

Code Quality & Implementation ✅

Excellent approach. The fix correctly addresses the high-DPI canvas rendering issue by:

  • Using devicePixelRatio to scale canvas internal resolution
  • Setting CSS dimensions to maintain logical size
  • Applying context scaling to match device pixel ratio

This is the standard solution for crisp canvas rendering on high-DPI displays.

Technical Review

Lines 317-323 (view-pdf.html:317-323):

const dpr = window.devicePixelRatio || 1;
canvas.width = Math.floor(viewport.width * dpr);
canvas.height = Math.floor(viewport.height * dpr);
canvas.style.width = viewport.width + 'px';
canvas.style.height = viewport.height + 'px';
context.scale(dpr, dpr);

Strengths:

  • ✅ Proper fallback (|| 1) for older browsers
  • Math.floor() prevents subpixel rendering artifacts
  • ✅ Separates internal resolution from visual size via CSS
  • ✅ Context scaling ensures PDF.js renders at correct scale

Potential Issues & Improvements

Minor Edge Cases:

  1. Dynamic DPR changes: Some devices can change DPR (e.g., external monitors). Consider listening to window.matchMedia('screen and (resolution: 2dppx)') changes for robustness.

  2. Memory usage: High-DPI rendering increases canvas memory usage by ~4-9x (2x-3x DPR squared). For large PDFs, this could impact performance on memory-constrained devices.

Performance Impact: 📈

  • Positive: Eliminates browser upscaling blur, improving perceived performance
  • Trade-off: Slightly higher memory usage and render time, but negligible for typical PDFs

Security Assessment ✅

No security concerns. The change only affects canvas rendering dimensions and doesn't introduce any new attack vectors.

Test Coverage Considerations

The change affects visual rendering quality, which is difficult to unit test. Consider:

  • Manual testing: Verify on actual high-DPI devices (iOS Safari, Android Chrome)
  • Visual regression testing: If using tools like Playwright, capture screenshots on different DPR values

Recommendation: APPROVE

This is a well-implemented fix for a common high-DPI canvas issue. The code follows best practices and the performance trade-offs are reasonable for the visual quality improvement gained.

Suggested follow-up (optional): Consider adding a comment explaining the DPR scaling for future maintainers.

@cloudflare-workers-and-pages
Copy link

Deploying tools with  Cloudflare Pages  Cloudflare Pages

Latest commit: a429fb8
Status: ✅  Deploy successful!
Preview URL: https://765e103e.tools-b1q.pages.dev
Branch Preview URL: https://claude-fix-pdf-mobile-blur-t.tools-b1q.pages.dev

View logs

@simonw
Copy link
Owner Author

simonw commented Feb 17, 2026

Before screenshot:

IMG_2009

@simonw
Copy link
Owner Author

simonw commented Feb 17, 2026

After:
IMG_2010

@simonw simonw merged commit 8d8bac8 into main Feb 17, 2026
2 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants