Read complete PDFs and surface per-page OCR failures
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
/** Read every requested page in order, retaining text/OCR provenance per page. */
|
||||
export async function readPdfPages(doc, {maxPages=Infinity, ocrPage, forceOcr=false, onProgress}={}) {
|
||||
if (!(maxPages === Infinity || (Number.isInteger(maxPages) && maxPages > 0))) throw new Error('Geçersiz sayfa sınırı.');
|
||||
const pages = Math.min(doc.numPages,maxPages);
|
||||
const results=[];
|
||||
for(let number=1;number<=pages;number++) {
|
||||
onProgress?.({sayfa:number,toplam:pages});
|
||||
const page=await doc.getPage(number);
|
||||
try {
|
||||
const content=await page.getTextContent();
|
||||
let text=content.items.map(item=>item.str + (item.hasEOL ? '\n' : ' ')).join('').trim();
|
||||
let method='text';
|
||||
if(ocrPage && (forceOcr || text.replace(/\s/g,'').length < 100)) {
|
||||
try { text=await ocrPage(page); method='ocr'; }
|
||||
catch { throw new Error(`${number}. sayfada OCR tamamlanamadı. Okunabilir belgeyle yeniden deneyin.`); }
|
||||
}
|
||||
results.push({page:number,text,method});
|
||||
} finally { page.cleanup?.(); }
|
||||
}
|
||||
return {text:results.map(p=>p.text).join('\n\n').replace(/[ \t]+/g,' ').trim(),
|
||||
pageCount:doc.numPages,usedPages:pages,pages:results,ocr:results.some(p=>p.method==='ocr'),
|
||||
complete:pages===doc.numPages};
|
||||
}
|
||||
Reference in New Issue
Block a user