Something went wrong. Try again.
Synapse is a study workspace. Organize courses read and annotate the material, train with generated exercises and watch your progress forked from denishovart.com/synapse
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637// 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<Uint8Array> { 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<File> { 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' });}