Files
rickyandClaude Opus 5.5 66fa5f4423 PDFs show on phones, drawn with PDF.js
Android shows nothing for a PDF in a frame and iPhones only its first
page. On touch screens the document viewer now draws every page with
PDF.js 6.5.438 (vendored in public/pdfjs/<version>/, loaded only there);
desktop browsers keep their own viewer in the frame.

- Pages are drawn as they come near the screen and let go once far from
  it, so a long manual doesn't use up a phone's memory.
- Only the parts of the file those pages need are downloaded: uploaded
  files now send Accept-Ranges, and PDF.js's background fetch of the
  whole file is off.
- JBIG2/JPEG 2000 scans decode (wasm, with JS fallbacks).

Checked on an Android 14 emulator with Chromium 160 (which reports
pdfViewerEnabled but can't show a PDF in a frame).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 13:15:45 -05:00

29 lines
2.0 KiB
ERB

<%# Multi-page viewer: one attachment per "page", with prev/next. %>
<% pages = document.pages.to_a %>
<% if pages.empty? %>
<%= empty_state t(".no_files_uploaded") %>
<% else %>
<% current = pages[page - 1] %>
<div class="mb-3 flex items-center justify-between text-sm">
<%= link_to t(".prev"), url_for(page: page - 1), class: ["btn-secondary btn-sm", ("pointer-events-none opacity-40" if page <= 1)] %>
<span><%= t(".page_of_html", page: page, pages: pages.size, link: (link_to current.filename, rails_blob_path(current, disposition: :attachment), class: "link")) %></span>
<%= link_to t(".next"), url_for(page: page + 1), class: ["btn-secondary btn-sm", ("pointer-events-none opacity-40" if page >= pages.size)] %>
</div>
<div class="flex justify-center rounded bg-slate-50 p-2">
<% if current.image? %>
<%= image_tag rails_blob_path(current, disposition: :inline), alt: current.filename.to_s, class: "max-h-[80vh] max-w-full" %>
<% elsif current.content_type == "application/pdf" %>
<% pdf_url = rails_blob_path(current, disposition: :inline) %>
<%# Phones get the pages drawn by PDF.js; browsers with a PDF viewer of their own get it in the frame. %>
<div class="w-full space-y-2" data-controller="pdf-viewer" data-pdf-viewer-url-value="<%= pdf_url %>" data-pdf-viewer-pdfjs-value="<%= Rails.application.config.x.pdfjs_path %>">
<template data-pdf-viewer-target="frame"><iframe src="<%= pdf_url %>" title="<%= current.filename %>" class="h-[80vh] w-full"></iframe></template>
<p class="p-6 text-center text-sm text-slate-500" role="status" data-pdf-viewer-target="status"><%= t("js.pdf.loading") %></p>
<div class="space-y-2" data-pdf-viewer-target="pages"></div>
<noscript><iframe src="<%= pdf_url %>" title="<%= current.filename %>" class="h-[80vh] w-full"></iframe></noscript>
</div>
<% else %>
<p class="p-6 text-sm"><%= link_to t(".download", filename: current.filename), rails_blob_path(current, disposition: :attachment), class: "link" %></p>
<% end %>
</div>
<% end %>