// Images are viewable content, not documents to "convert": a photo of a // whiteboard or a scanned page should just open in the PDF viewer. We wrap // the image in a one-page PDF with pdf-lib (no external binary, works in the // browser and the shells alike), sized to the image so nothing is cropped. import { IMAGE_EXTENSIONS, fileExtension } from './formats'; export function isImageFile(filename: string, mime?: string): boolean { return ( (IMAGE_EXTENSIONS as readonly string[]).includes(fileExtension(filename)) || /^image\/(jpe?g|png)$/i.test(mime ?? '') ); } // A one-page PDF holding the image at its natural pixel size (1px = 1pt), so // the viewer's zoom controls do the rest. JPEG and PNG are the two formats // pdf-lib embeds directly. export async function imageToPdfBytes(bytes: Uint8Array, mime?: string): Promise { const { PDFDocument } = await import('pdf-lib'); const pdf = await PDFDocument.create(); const data = bytes.slice(); const isPng = mime?.includes('png') || (data[0] === 0x89 && data[1] === 0x50); const image = isPng ? await pdf.embedPng(data) : await pdf.embedJpg(data); const page = pdf.addPage([image.width, image.height]); page.drawImage(image, { x: 0, y: 0, width: image.width, height: image.height }); return pdf.save(); } // Same image, wrapped as a .pdf File, so the rest of the upload pipeline (the // server upload and the local ingest) treats it like any other PDF. export async function imageFileToPdf(file: File): Promise { const bytes = new Uint8Array(await file.arrayBuffer()); const pdf = await imageToPdfBytes(bytes, file.type); const name = file.name.replace(/\.[^.]+$/, '') + '.pdf'; return new File([pdf.slice() as BlobPart], name, { type: 'application/pdf' }); }