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(); }