From 8401428ca7b7f85137aabd874049cca72b2bd3cc Mon Sep 17 00:00:00 2001 From: Maurice Date: Fri, 13 Nov 2026 09:21:38 +0100 Subject: [PATCH] feat(pdf): bake place photos into the trip PDF at thumbnail size (#1501) The print engine embedded every place photo at full resolution, so a trip of a few hundred stops saved to a PDF of tens of megabytes. Same-origin photos are now fetched six at a time, scaled to a 156 px short side and inlined as JPEG data URLs; photos from other origins, failures and an aborted export keep the URL. --- client/scripts/strict-baseline.json | 2 +- client/src/components/PDF/TripPDF.test.ts | 52 ++++++++ client/src/components/PDF/TripPDF.tsx | 27 +++-- client/src/components/PDF/pdfImages.test.ts | 128 ++++++++++++++++++++ client/src/components/PDF/pdfImages.ts | 93 ++++++++++++++ wiki/PDF-Export.md | 2 + 6 files changed, 295 insertions(+), 9 deletions(-) create mode 100644 client/src/components/PDF/pdfImages.test.ts create mode 100644 client/src/components/PDF/pdfImages.ts diff --git a/client/scripts/strict-baseline.json b/client/scripts/strict-baseline.json index 1de46c9dc..45fe004df 100644 --- a/client/scripts/strict-baseline.json +++ b/client/scripts/strict-baseline.json @@ -43,7 +43,7 @@ "src/components/Map/ReservationOverlay.test.tsx": 1, "src/components/Map/transitGeometry.ts": 1, "src/components/Map/useMarkerThumbs.ts": 1, - "src/components/PDF/TripPDF.tsx": 40, + "src/components/PDF/TripPDF.tsx": 38, "src/components/Packing/PackingListPanelItemRow.test.tsx": 2, "src/components/Planner/DayPlanSidebar.constants.ts": 1, "src/components/Planner/DayPlanSidebar.tsx": 52, diff --git a/client/src/components/PDF/TripPDF.test.ts b/client/src/components/PDF/TripPDF.test.ts index d7756f6d0..44c6d3148 100644 --- a/client/src/components/PDF/TripPDF.test.ts +++ b/client/src/components/PDF/TripPDF.test.ts @@ -1486,3 +1486,55 @@ describe('downloadTripPDF — large text', () => { expect(largeToggle()!.getAttribute('aria-checked')).toBe('false') }) }) + +// ── Thumbnail-sized photos (#1501) ────────────────────────────────────────── + +describe('downloadTripPDF — photos at thumbnail size', () => { + const JPEG = 'data:image/jpeg;base64,dGh1bWI=' + + beforeEach(() => { + vi.stubGlobal('createImageBitmap', vi.fn(async () => ({ width: 1600, height: 1200, close: vi.fn() }))) + // Only the 2d context: a WebGL answer would send the route map down the basemap path. + const ctx = { fillStyle: '', fillRect: vi.fn(), drawImage: vi.fn() } + vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockImplementation( + ((type: string) => (type === '2d' ? ctx : null)) as unknown as HTMLCanvasElement['getContext'], + ) + vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue(JPEG) + server.use( + http.get('http://localhost:3000/uploads/colosseum.jpg', () => + new HttpResponse(new Uint8Array([1, 2, 3]), { headers: { 'Content-Type': 'image/jpeg' } })), + ) + }) + + afterEach(() => { + vi.unstubAllGlobals() + }) + + const withImage = (image_url: string) => ({ + ...richArgs, + assignments: { '10': [{ ...assignmentForDay, place: { ...placeWithDetails, image_url } }] } as any, + }) + + it('FE-PDF-THUMB-001: an uploaded photo is baked in as a small JPEG instead of its full-size URL', async () => { + await downloadTripPDF(withImage('/uploads/colosseum.jpg')) + const html = getIframe()!.srcdoc + + expect(html).toContain(``) + expect(html).not.toContain('colosseum.jpg') + }) + + it('FE-PDF-THUMB-002: a photo from another origin keeps its URL', async () => { + await downloadTripPDF(withImage('https://images.example.com/colosseum.jpg')) + + expect(getIframe()!.srcdoc).toContain('') + }) + + it('FE-PDF-THUMB-003: a closed export dialog stops the photos and opens nothing', async () => { + const controller = new AbortController() + controller.abort() + await downloadTripPDF({ ...withImage('/uploads/colosseum.jpg'), signal: controller.signal }) + + expect(getOverlay()).toBeNull() + expect(HTMLCanvasElement.prototype.toDataURL).not.toHaveBeenCalled() + }) +}) diff --git a/client/src/components/PDF/TripPDF.tsx b/client/src/components/PDF/TripPDF.tsx index 09944d90e..696ab1a99 100644 --- a/client/src/components/PDF/TripPDF.tsx +++ b/client/src/components/PDF/TripPDF.tsx @@ -23,6 +23,7 @@ import { fetchExchangeRates } from '../../hooks/useExchangeRates' import { getFlightLegs, getTrainLegs, usesStationRoute } from '../../utils/flightLegs' import { onlyMyPlan, planAssignments } from './pdfScope' import { createPdfMarkdown, PDF_MARKDOWN_CSS } from './pdfMarkdown' +import { shrinkPdfImages } from './pdfImages' function renderLucideIcon(icon:LucideIcon, props = {}) { return renderIconMarkup( @@ -95,6 +96,12 @@ function safeImg(url) { return /\.(jpe?g|png|gif|webp|bmp|tiff?)(\?.*)?$/i.test(url) ? absUrl(url) : null } +// A place card's picture: direct > google photo > fallback icon (null). Both go +// through safeImg so the proxy path is resolved to an absolute URL the PDF can load. +function placeImage(place: { id: number; image_url?: string | null }, photoMap: Record): string | null { + return safeImg(place.image_url) || safeImg(photoMap[place.id]) +} + // Generate SVG string from Lucide icon name (for category thumbnails) function categoryIconSvg(iconName, color = '#6366f1', size = 24) { const Icon = getCategoryIcon(iconName) @@ -123,7 +130,7 @@ function longDateRange(days, locale) { // app UI uses (google_place_id || osm_id || coords) — otherwise OSM/coords-only // places fell back to category icons in the PDF even though they show photos in-app. async function fetchPlacePhotos(assignments: AssignmentsMap, places: Place[]) { - const photoMap = {} // placeId → photoUrl + const photoMap: Record = {} // The assignment projection drops osm_id, so recover it from the full places pool. const osmById = new Map((places || []).map(p => [p.id, p.osm_id])) const allPlaces = Object.values(assignments).flatMap(a => a.map(x => x.place)).filter(Boolean) @@ -260,6 +267,15 @@ export async function downloadTripPDF({ trip, days, places, assignments: allStor // Pre-fetch place photos (Google, OSM and coords-only places) const photoMap = await fetchPlacePhotos(assignments, places) + // Each card's picture at thumbnail size, so the saved PDF does not carry every + // photo at full resolution (#1501). A picture missing here prints from its URL. + const thumbs = await shrinkPdfImages( + Object.values(assignments).flatMap(list => list.flatMap(a => { + const url = a.place && placeImage(a.place, photoMap) + return url ? [url] : [] + })), + signal, + ) const totalAssigned = new Set( Object.values(assignments).flatMap(a => a.map(x => x.place?.id)).filter(Boolean) @@ -494,15 +510,10 @@ export async function downloadTripPDF({ trip, days, places, assignments: allStor const cat = categories.find(c => c.id === place.category_id) const color = safeHexColor(cat?.color, '#6366f1') - // Image: direct > google photo > fallback icon. Both go through safeImg - // so the proxy path is resolved to an absolute URL the PDF can load. - const directImg = safeImg(place.image_url) - const googleImg = safeImg(photoMap[place.id]) - const img = directImg || googleImg - + const img = placeImage(place, photoMap) const iconSvg = categoryIconSvg(cat?.icon, color, 24) const thumbHtml = img - ? `` + ? `` : `
${iconSvg}
` diff --git a/client/src/components/PDF/pdfImages.test.ts b/client/src/components/PDF/pdfImages.test.ts new file mode 100644 index 000000000..db41eec9d --- /dev/null +++ b/client/src/components/PDF/pdfImages.test.ts @@ -0,0 +1,128 @@ +/** + * FE-PDFIMAGES-001 to FE-PDFIMAGES-007: the place photos of the trip PDF are + * baked in at thumbnail size (#1501). jsdom has neither createImageBitmap nor a + * 2d canvas, so both are stubbed: the bitmap reports the size the test names, and + * the canvas records what it was asked to draw. + */ +import { http, HttpResponse } from 'msw'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; + +import { server } from '../../../tests/helpers/msw/server'; +import { shrinkPdfImages } from './pdfImages'; + +const origin = window.location.origin; +const JPEG = 'data:image/jpeg;base64,c21hbGw='; + +let bitmapSize = { width: 1200, height: 800 }; +let drawn: { width: number; height: number }[] = []; +let inFlight = 0; +let mostInFlight = 0; + +/** A 2d context that records the size of the canvas each draw lands on; WebGL is not offered. */ +function fakeContext(this: HTMLCanvasElement, type: string) { + if (type !== '2d') return null; + return { + fillStyle: '', + fillRect: vi.fn(), + drawImage: vi.fn(() => drawn.push({ width: this.width, height: this.height })), + }; +} + +function imageResponse(type = 'image/jpeg') { + return new HttpResponse(new Uint8Array([1, 2, 3]), { headers: { 'Content-Type': type } }); +} + +beforeEach(() => { + bitmapSize = { width: 1200, height: 800 }; + drawn = []; + inFlight = 0; + mostInFlight = 0; + vi.stubGlobal('createImageBitmap', vi.fn(async () => ({ ...bitmapSize, close: vi.fn() }))); + vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockImplementation( + fakeContext as unknown as HTMLCanvasElement['getContext'] + ); + vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue(JPEG); + server.use( + http.get(`${origin}/uploads/places/:file`, async () => { + inFlight++; + mostInFlight = Math.max(mostInFlight, inFlight); + await new Promise((resolve) => setTimeout(resolve, 5)); + inFlight--; + return imageResponse(); + }), + http.get(`${origin}/uploads/notes/:file`, () => imageResponse('text/plain')), + http.get(`${origin}/uploads/missing.jpg`, () => new HttpResponse(null, { status: 404 })) + ); +}); + +afterEach(() => { + vi.unstubAllGlobals(); + vi.restoreAllMocks(); +}); + +describe('shrinkPdfImages', () => { + it('FE-PDFIMAGES-001: a same-origin photo comes back as a JPEG with its short side at 156 px', async () => { + const url = `${origin}/uploads/places/a.jpg`; + const shrunk = await shrinkPdfImages([url]); + + expect(shrunk.get(url)).toBe(JPEG); + expect(drawn).toEqual([{ width: 234, height: 156 }]); + expect(HTMLCanvasElement.prototype.toDataURL).toHaveBeenCalledWith('image/jpeg', expect.any(Number)); + }); + + it('FE-PDFIMAGES-002: a photo from another origin keeps its URL and is never fetched', async () => { + const fetchSpy = vi.spyOn(globalThis, 'fetch'); + const shrunk = await shrinkPdfImages(['https://images.example.com/a.jpg']); + + expect(shrunk.size).toBe(0); + expect(fetchSpy).not.toHaveBeenCalled(); + }); + + it('FE-PDFIMAGES-003: a photo that is small already, missing or not an image keeps its URL', async () => { + bitmapSize = { width: 120, height: 90 }; + const small = `${origin}/uploads/places/small.jpg`; + const missing = `${origin}/uploads/missing.jpg`; + const text = `${origin}/uploads/notes/readme.txt`; + const shrunk = await shrinkPdfImages([small, missing, text]); + + expect(shrunk.size).toBe(0); + expect(drawn).toEqual([]); + }); + + it('FE-PDFIMAGES-004: hundreds of photos are fetched a few at a time, each URL once', async () => { + const urls = Array.from({ length: 40 }, (_, i) => `${origin}/uploads/places/${i}.jpg`); + const shrunk = await shrinkPdfImages([...urls, ...urls]); + + expect(shrunk.size).toBe(40); + expect(drawn).toHaveLength(40); + expect(mostInFlight).toBeGreaterThan(1); + expect(mostInFlight).toBeLessThanOrEqual(6); + }); + + it('FE-PDFIMAGES-005: an aborted export starts no photo at all', async () => { + const fetchSpy = vi.spyOn(globalThis, 'fetch'); + const controller = new AbortController(); + controller.abort(); + const shrunk = await shrinkPdfImages([`${origin}/uploads/places/a.jpg`], controller.signal); + + expect(shrunk.size).toBe(0); + expect(fetchSpy).not.toHaveBeenCalled(); + }); + + it('FE-PDFIMAGES-006: without createImageBitmap nothing is fetched and every photo keeps its URL', async () => { + vi.stubGlobal('createImageBitmap', undefined); + const fetchSpy = vi.spyOn(globalThis, 'fetch'); + const shrunk = await shrinkPdfImages([`${origin}/uploads/places/a.jpg`]); + + expect(shrunk.size).toBe(0); + expect(fetchSpy).not.toHaveBeenCalled(); + }); + + it('FE-PDFIMAGES-007: a canvas that cannot encode a JPEG leaves the URL in place', async () => { + vi.spyOn(HTMLCanvasElement.prototype, 'toDataURL').mockReturnValue('data:,'); + const url = `${origin}/uploads/places/a.jpg`; + const shrunk = await shrinkPdfImages([url]); + + expect(shrunk.has(url)).toBe(false); + }); +}); diff --git a/client/src/components/PDF/pdfImages.ts b/client/src/components/PDF/pdfImages.ts new file mode 100644 index 000000000..7396f3b02 --- /dev/null +++ b/client/src/components/PDF/pdfImages.ts @@ -0,0 +1,93 @@ +/** + * Thumbnail-sized copies of the place photos for the trip PDF (#1501). + * + * The print engine embeds every image at the size it was served in, so a trip of + * two hundred stops printed to a file of fifty megabytes for pictures 52 px wide. + * Each photo this server hands out (uploads and the place-photo proxy) is fetched, + * scaled down and baked into the document as a JPEG data URL. A photo from another + * origin keeps its URL: without CORS a canvas that drew it could not be read back. + * + * Best-effort throughout. A photo that fails to load, is not an image, is already + * small or cannot be encoded keeps its URL, and none holds the export up for longer + * than FETCH_TIMEOUT_MS. + */ + +/** The short side of a copy: three times the 52 px thumbnail, sharp at print resolution. */ +const THUMB_PX = 156; +const JPEG_QUALITY = 0.72; +/** Photos in flight at once: a trip of hundreds should neither queue one by one nor decode all at once. */ +const CONCURRENCY = 6; +const FETCH_TIMEOUT_MS = 10_000; +/** Anything larger is not read into memory for a thumbnail; it keeps its URL. */ +const MAX_BYTES = 20 * 1024 * 1024; + +/** + * The shrunk copy of each same-origin photo in `urls`, keyed by its URL. A URL + * missing from the result prints as it is. Stops starting new photos once + * `signal` aborts, and returns what it has. + */ +export async function shrinkPdfImages(urls: Iterable, signal?: AbortSignal): Promise> { + const shrunk = new Map(); + const queue = [...new Set(urls)].filter(isSameOrigin); + if (queue.length === 0 || !canShrink()) return shrunk; + + const worker = async () => { + for (let url = queue.shift(); url !== undefined && !signal?.aborted; url = queue.shift()) { + const copy = await shrinkImage(url, signal); + if (copy) shrunk.set(url, copy); + } + }; + await Promise.all(Array.from({ length: Math.min(CONCURRENCY, queue.length) }, worker)); + return shrunk; +} + +function isSameOrigin(url: string): boolean { + try { + return new URL(url, window.location.origin).origin === window.location.origin; + } catch { + // Not a URL at all: there is nothing to fetch, and the document prints it as given. + return false; + } +} + +// createImageBitmap is checked first: where it is missing (old browsers, jsdom) the +// canvas is never asked for a context. +function canShrink(): boolean { + return typeof createImageBitmap === 'function' && document.createElement('canvas').getContext('2d') !== null; +} + +async function shrinkImage(url: string, signal?: AbortSignal): Promise { + const deadline = AbortSignal.timeout(FETCH_TIMEOUT_MS); + try { + const res = await fetch(url, { signal: signal ? AbortSignal.any([signal, deadline]) : deadline }); + if (!res.ok || Number(res.headers.get('content-length') ?? 0) > MAX_BYTES) return null; + const blob = await res.blob(); + if (!blob.type.startsWith('image/') || blob.size > MAX_BYTES) return null; + const bitmap = await createImageBitmap(blob); + try { + return toJpeg(bitmap); + } finally { + bitmap.close(); + } + } catch { + // Offline, timed out, aborted or undecodable: the photo prints from its URL. + return null; + } +} + +/** The bitmap with its short side at THUMB_PX, or null when it is that small already. */ +function toJpeg(bitmap: ImageBitmap): string | null { + const scale = THUMB_PX / Math.min(bitmap.width, bitmap.height); + if (!Number.isFinite(scale) || scale >= 1) return null; + const canvas = document.createElement('canvas'); + canvas.width = Math.max(1, Math.round(bitmap.width * scale)); + canvas.height = Math.max(1, Math.round(bitmap.height * scale)); + const ctx = canvas.getContext('2d'); + if (!ctx) return null; + // JPEG has no alpha channel: a transparent PNG would turn black without a white ground. + ctx.fillStyle = 'white'; + ctx.fillRect(0, 0, canvas.width, canvas.height); + ctx.drawImage(bitmap, 0, 0, canvas.width, canvas.height); + const copy = canvas.toDataURL('image/jpeg', JPEG_QUALITY); + return copy.startsWith('data:image/jpeg') ? copy : null; +} diff --git a/wiki/PDF-Export.md b/wiki/PDF-Export.md index 64f523e79..8ec0b5baf 100644 --- a/wiki/PDF-Export.md +++ b/wiki/PDF-Export.md @@ -62,6 +62,8 @@ Under the header: - **Notes**: the icon, the text and the time, if set - **Bookings**: the type icon, the title, the time, the notes of a transport (unless **Transport notes** is off), the details that fit the type (airline, flight number and route for a flight; train number, platform and seat for a train; party size for a restaurant; the venue for an event; the operator for a tour), the location and the booking code +Place photos go into the document at thumbnail size, so a trip with hundreds of places still saves to a small file. A photo linked from another website keeps its original size, because the browser does not let TREK shrink it. + ![A day page in the PDF preview: the dark day header with date and cost, the check-in at the hotel, the flight with its notes, a day note, and a place whose Markdown description prints with bold text, italics and a list](assets/PDFPreviewDay.png) ### Footer