From 2fcdd687f7ac52f13635689544fa471215dce753 Mon Sep 17 00:00:00 2001 From: Randall Stillwell Date: Tue, 7 Apr 2026 13:27:16 -0500 Subject: [PATCH] Fix duplex scan rotation and swap front/back image labels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Duplex scanners flip the sheet between sides, so odd pages (back) are 180° rotated relative to even pages (front). Landscape back pages now rotate CCW instead of CW, and portrait back pages get a 180° flip. Also swapped "Front"/"Back" labels to match the physical card sides. Made-with: Cursor --- src/app/cards/[id]/page.tsx | 4 ++-- src/lib/pdf.ts | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/app/cards/[id]/page.tsx b/src/app/cards/[id]/page.tsx index 2fb13ea..f900b94 100644 --- a/src/app/cards/[id]/page.tsx +++ b/src/app/cards/[id]/page.tsx @@ -342,8 +342,8 @@ export default function CardDetailPage() {
- - + +
diff --git a/src/lib/pdf.ts b/src/lib/pdf.ts index cb35da1..0334555 100644 --- a/src/lib/pdf.ts +++ b/src/lib/pdf.ts @@ -26,11 +26,21 @@ export async function pdfToImages(pdfBuffer: Buffer): Promise { if (result.buffer) { let buf = result.buffer as Buffer; let meta = await sharp(buf).metadata(); + const w = meta.width || 0; + const h = meta.height || 0; - // Scanners often store landscape content with a PDF /Rotate flag that - // pdf2pic may not apply. Detect and rotate to portrait orientation. - if ((meta.width || 0) > (meta.height || 0)) { - buf = await sharp(buf).rotate(90).jpeg({ quality: 90 }).toBuffer(); + // Duplex scanners flip the sheet between sides, so odd pages + // (back/response card) are 180° rotated relative to even pages + // (front/survey). Landscape pages need rotation to portrait; + // the direction depends on which side of the sheet was scanned. + const isBackSide = i % 2 === 1; + + if (w > h) { + const angle = isBackSide ? -90 : 90; + buf = await sharp(buf).rotate(angle).jpeg({ quality: 90 }).toBuffer(); + meta = await sharp(buf).metadata(); + } else if (isBackSide) { + buf = await sharp(buf).rotate(180).jpeg({ quality: 90 }).toBuffer(); meta = await sharp(buf).metadata(); }