char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
92d1bb0c9
char/aliceai-t5-moe
1/** 2* PDF processing utilities using PDF.js 3* Handles PDF text extraction and image conversion in the browser 4*/ 5 6import { browser } from '$app/environment' ; 7import { MimeTypeApplication , MimeTypeImage } from '$lib/enums' ; 8import * as pdfjs from 'pdfjs-dist' ; 9 10type TextContent = { 11items :Array <{ str :string }>; 12}; 13 14if ( browser ) { 15// Import worker as text and create blob URL for inline bundling 16import ( 'pdfjs-dist/build/pdf.worker.min.mjs?raw' ) 17. then (( workerModule ) => { 18const workerBlob = new Blob ([ workerModule . default ], { type :'application/javascript' }); 19 20pdfjs . GlobalWorkerOptions . workerSrc = URL . createObjectURL ( workerBlob ); 21}) 22. catch (() => { 23console . warn ( 'Failed to load PDF.js worker, PDF processing may not work' ); 24}); 25} 26 27/** 28* Convert a File object to ArrayBuffer for PDF.js processing 29* @param file - The PDF file to convert 30* @returns Promise resolving to the file's ArrayBuffer 31*/ 32async function getFileAsBuffer ( file :File ) :Promise < ArrayBuffer > { 33return new Promise (( resolve , reject ) => { 34const reader = new FileReader (); 35 36reader . onload = ( event ) => { 37if ( event . target ?. result ) { 38resolve ( event . target . result as ArrayBuffer ); 39} else { 40reject ( new Error ( 'Failed to read file.' )); 41} 42}; 43reader . onerror = () => { 44reject ( new Error ( 'Failed to read file.' )); 45}; 46reader . readAsArrayBuffer ( file ); 47}); 48} 49 50/** 51* Extract text content from a PDF file 52* @param file - The PDF file to process 53* @returns Promise resolving to the extracted text content 54*/ 55export async function convertPDFToText ( file :File ) :Promise < string > { 56if ( ! browser ) { 57throw new Error ( 'PDF processing is only available in the browser' ); 58} 59 60try { 61const buffer = await getFileAsBuffer ( file ); 62const = await pdfjs . getDocument ({ data :buffer }). promise ; 63const numPages = . numPages ; 64const textContentPromises :Promise < TextContent >[] = []; 65 66for ( let i = 1 ; i <= numPages ; i ++ ) { 67// eslint-disable-next-line @typescript-eslint/no-explicit-any 68textContentPromises . push ( . getPage ( i ). then (( page :any ) => page . getTextContent ())); 69} 70 71const textContents = await Promise . all ( textContentPromises ); 72const textItems = textContents . flatMap (( textContent :TextContent ) => 73textContent . items . map (( item ) => item . str ?? '' ) 74); 75 76return textItems . join ( '\n' ); 77} catch ( error ) { 78console . error ( 'Error converting PDF to text:' , error ); 79 80throw new Error ( 81`Failed to convert PDF to text: ${ error instanceof Error ? error . message : 'Unknown error' } ` 82); 83} 84} 85 86/** 87* Convert PDF pages to PNG images as data URLs 88* @param file - The PDF file to convert 89* @param scale - Rendering scale factor (default: 1.5) 90* @returns Promise resolving to array of PNG data URLs 91*/ 92export async function convertPDFToImage ( file :File , scale :number = 1.5 ) :Promise < string []> { 93if ( ! browser ) { 94throw new Error ( 'PDF processing is only available in the browser' ); 95} 96 97try { 98const buffer = await getFileAsBuffer ( file ); 99const doc = await pdfjs . getDocument ({ data :buffer }). promise ; 100const pages :Promise < string >[] = []; 101 102for ( let i = 1 ; i <= doc . numPages ; i ++ ) { 103const page = await doc . getPage ( i ); 104const viewport = page . getViewport ({ scale}); 105const canvas = document . createElement ( 'canvas' ); 106const ctx = canvas . getContext ( '2d' ); 107 108canvas . width = viewport . width ; 109canvas . height = viewport . height ; 110 111if ( ! ctx ) { 112throw new Error ( 'Failed to get 2D context from canvas' ); 113} 114 115const task = page . render ({ 116canvas :canvas , 117canvasContext :ctx , 118viewport :viewport 119}); 120 121pages . push ( 122task . promise . then (() => { 123return canvas . toDataURL ( MimeTypeImage . PNG ); 124}) 125); 126} 127 128return await Promise . all ( pages ); 129} catch ( error ) { 130console . error ( 'Error converting PDF to images:' , error ); 131 132throw new Error ( 133`Failed to convert PDF to images: ${ error instanceof Error ? error . message : 'Unknown error' } ` 134); 135} 136} 137 138/** 139* Check if a file is a PDF based on its MIME type 140* @param file - The file to check 141* @returns True if the file is a PDF 142*/ 143export function isPdfFile ( file :File ) :boolean { 144return file . type === MimeTypeApplication . ; 145} 146 147/** 148* Check if a MIME type represents a PDF 149* @param mimeType - The MIME type to check 150* @returns True if the MIME type is application/pdf 151*/ 152export function isApplicationMimeType ( mimeType :string ) :boolean { 153return mimeType === MimeTypeApplication . ; 154}