| 7 | const pdfjsLib = pdfjsLibRaw as typeof pdfjsLibModule; |
| 8 | |
| 9 | async function read_pdf(file: File) { |
| 10 | const typedArray = await file.arrayBuffer(); |
| 11 | |
| 12 | // Load the PDF file. |
| 13 | //const loadingTask = PDFJS.getDocument(pdfData); |
| 14 | //const pdf = await pdfjsLib.getDocument({ data: typedArray }).promise; |
| 15 | //const pdf = await pdfjsLib.getDocument({ data: typedArray }).promise; |
| 16 | const loadingTask = pdfjsLib.getDocument({ data: typedArray }); |
| 17 | const pdf = await loadingTask.promise; |
| 18 | |
| 19 | let textContent = ''; |
| 20 | |
| 21 | for (let i = 1; i <= pdf.numPages; i++) { |
| 22 | // Fetch the page |
| 23 | const page = await pdf.getPage(i); |
| 24 | |
| 25 | // Fetch the text content |
| 26 | const text = await page.getTextContent(); |
| 27 | |
| 28 | // Concatenate the text |
| 29 | textContent += text.items |
| 30 | .map((item) => ('str' in item ? item.str : '')) |
| 31 | .join('\n'); |
| 32 | } |
| 33 | |
| 34 | return textContent; |
| 35 | } |
| 36 | |
| 37 | async function read_docx(file: File) { |
| 38 | const arrayBuffer = await file.arrayBuffer(); |