Skip to content

Commit f8287cb

Browse files
Merge pull request #206 from appdevforall/feat/ADFA-4708-pdfjs-rootfs-module
ADFA-4708: serve Mozilla pdf.js from nginx for in-WebView PDF viewing
2 parents c505ee4 + b6d5ef2 commit f8287cb

5 files changed

Lines changed: 106 additions & 0 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
# OS
88
.DS_Store
99
Thumbs.db
10+
11+
# pdf.js prebuilt dist (fetched at build time, not vendored) — ADFA-4708
12+
static/pdfjs/dist/

‎iiab-android‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,36 @@ mkdir -p /library/www/html/k2go-docs
606606
/usr/local/bin/pdsm restart nginx
607607
ok "K2Go docs module scaffold installed (hidden until docs are added)."
608608

609+
#-----------------------------
610+
# pdf.js viewer (ADFA-4708) — static module.
611+
# Serve Mozilla pdf.js so the portal WebView can render PDFs inline/offline. The prebuilt
612+
# dist is produced by static/pdfjs/fetch-pdfjs.sh (run at build time); here we install the
613+
# nginx conf and deploy the dist to the web root. Unlike k2go-docs this is always present.
614+
#-----------------------------
615+
log "Installing pdf.js viewer static module..."
616+
cp "${K2GO_OPT_DIR}/static/pdfjs/pdfjs-nginx.conf" "/etc/nginx/conf.d/pdfjs-nginx.conf"
617+
mkdir -p /library/www/html/pdfjs
618+
# Produce the pinned pdf.js dist at build time (this is what actually downloads it).
619+
# dist/ is gitignored, so on a clean build it is absent and must be fetched here; if a
620+
# dist was pre-provided (e.g. vendored/cached) the fetch is skipped. Needs network +
621+
# curl/unzip in the build environment. pdf.js release cadence ~monthly (semver; majors
622+
# can break) -> the version is PINNED in fetch-pdfjs.sh; bump it deliberately, mainly on
623+
# a pdf.js security advisory, not on every release.
624+
if [ ! -d "${K2GO_OPT_DIR}/static/pdfjs/dist/web" ]; then
625+
log "Fetching pinned pdf.js dist (build-time)..."
626+
if ! bash "${K2GO_OPT_DIR}/static/pdfjs/fetch-pdfjs.sh"; then
627+
warn "pdf.js fetch failed; viewer will 404 until the dist is provided."
628+
fi
629+
fi
630+
if [ -d "${K2GO_OPT_DIR}/static/pdfjs/dist/web" ]; then
631+
cp -a "${K2GO_OPT_DIR}/static/pdfjs/dist/." /library/www/html/pdfjs/
632+
ok "pdf.js dist deployed."
633+
else
634+
warn "pdf.js dist not found; viewer will 404 until deployed."
635+
fi
636+
/usr/local/bin/pdsm restart nginx
637+
ok "pdf.js viewer module installed."
638+
609639
# Install demo content
610640
if [ "$ENABLE_DEMO_CONTENTS" -eq 1 ]; then
611641
install_demo_contents

‎static/pdfjs/README.md‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# pdf.js viewer (ADFA-4708) — in-WebView PDF viewing
2+
3+
The Android System WebView has no built-in PDF viewer, so a PDF served by the box would
4+
otherwise be ignored (or downloaded). This module serves Mozilla **pdf.js** from the local
5+
nginx so the portal WebView can render PDFs inline and offline.
6+
7+
## How it works
8+
- Served at `http://localhost:8085/pdfjs/` (see `pdfjs-nginx.conf`).
9+
- The app routes an internal-host PDF to `…/pdfjs/web/viewer.html?file=<encoded pdf url>`
10+
(see `PdfPolicy` / `PdfViewerUrl` in the controller app). Same origin as the box's PDFs,
11+
so there is no CORS / mixed-content problem and it works with no internet.
12+
- pdf.js is pure JavaScript (Apache-2.0) — the same dist serves ARM 32- and 64-bit.
13+
14+
## Assets (dist)
15+
The prebuilt pdf.js distribution is fetched at rootfs-build time, not vendored in git:
16+
17+
PDFJS_VERSION=4.10.38 ./fetch-pdfjs.sh # writes ./dist (web/ + build/)
18+
19+
Pin `PDFJS_VERSION` (and optionally `PDFJS_SHA256`) to the release we ship. The extracted
20+
`dist/` is deployed to `/library/www/html/pdfjs/` so `/pdfjs/web/viewer.html` resolves.
21+
22+
## Build / rootfs
23+
The `iiab-android` setup script installs `pdfjs-nginx.conf` into the :8085 server and
24+
deploys the dist to the web root (same mechanism as the other static modules). All editions.

‎static/pdfjs/fetch-pdfjs.sh‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env bash
2+
# Fetch a pinned Mozilla pdf.js prebuilt distribution into ./dist for deployment.
3+
# pdf.js is pure JS (Apache-2.0) -> no 32/64-bit concern; the same dist serves every ABI.
4+
# Run at rootfs-build time on a networked host; the extracted dist is then deployed to
5+
# /library/www/html/pdfjs (see the iiab-android setup script).
6+
set -euo pipefail
7+
8+
# Pinned pdf.js version. pdf.js ships a stable dist ~monthly and uses semver, so a MAJOR
9+
# bump can break the viewer API. Keep this pinned and bump it DELIBERATELY — primarily when
10+
# a pdf.js security advisory lands (it has had RCE CVEs), not on every release. Also confirm
11+
# the pin runs on the oldest Android System WebView we target (app minSdk = 24 / Android 7;
12+
# recent pdf.js majors need a modern Chromium WebView). Use the newest STABLE that is still
13+
# compatible — never a nightly/beta.
14+
# TODO(Luis): confirm the version to ship (marker below is a placeholder, not verified).
15+
PDFJS_VERSION="${PDFJS_VERSION:-4.10.38}"
16+
# Optional integrity check: set PDFJS_SHA256 to the expected zip checksum to enforce it.
17+
PDFJS_SHA256="${PDFJS_SHA256:-}"
18+
19+
HERE="$(cd "$(dirname "$0")" && pwd)"
20+
DIST_DIR="${HERE}/dist"
21+
ZIP_URL="https://github.com/mozilla/pdf.js/releases/download/v${PDFJS_VERSION}/pdfjs-${PDFJS_VERSION}-dist.zip"
22+
TMP_ZIP="$(mktemp -t pdfjs-XXXXXX.zip)"
23+
trap 'rm -f "$TMP_ZIP"' EXIT
24+
25+
echo "Fetching pdf.js v${PDFJS_VERSION} ..."
26+
curl -fSL "$ZIP_URL" -o "$TMP_ZIP"
27+
28+
if [ -n "$PDFJS_SHA256" ]; then
29+
echo "${PDFJS_SHA256} ${TMP_ZIP}" | sha256sum -c -
30+
fi
31+
32+
rm -rf "$DIST_DIR"
33+
mkdir -p "$DIST_DIR"
34+
unzip -q "$TMP_ZIP" -d "$DIST_DIR"
35+
# Sanity: the viewer the app points to must exist.
36+
test -f "${DIST_DIR}/web/viewer.html" || { echo "ERROR: web/viewer.html missing from dist"; exit 1; }
37+
echo "pdf.js dist ready at ${DIST_DIR} (web/viewer.html present)."

‎static/pdfjs/pdfjs-nginx.conf‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Mozilla pdf.js viewer (ADFA-4708) — static assets served directly by nginx on :8085.
2+
# Enables viewing PDFs INSIDE the portal WebView (which has no built-in PDF viewer):
3+
# the app routes a PDF to http://localhost:8085/pdfjs/web/viewer.html?file=<pdf url>.
4+
# Same origin as the box's own PDFs, so it works fully offline (no CORS / mixed-content).
5+
#
6+
# Unlike k2go-docs this is infrastructure that is ALWAYS present (not hidden-until-content):
7+
# the viewer must resolve for the feature to work. The dist ships web/ and build/ at the root.
8+
location ^~ /pdfjs/ {
9+
alias /library/www/html/pdfjs/;
10+
autoindex off;
11+
index web/viewer.html;
12+
}

0 commit comments

Comments
 (0)