Files
yc-modular-extractor-archived/scripts/read-pdf.mjs
T
2026-11-05 01:04:53 +08:00

9 lines
671 B
JavaScript

import {getDocument} from 'pdfjs-dist/legacy/build/pdf.mjs';
import {readFile,writeFile} from 'node:fs/promises';
const loadingTask=getDocument({data:new Uint8Array(await readFile(process.argv[2])),useSystemFonts:true});
const pdf=await loadingTask.promise;
const pages=[];
for(let i=1;i<=pdf.numPages;i++){const page=await pdf.getPage(i);const text=await page.getTextContent();pages.push({page:i,text:text.items.map(x=>x.str+(x.hasEOL?'\n':' ')).join('')});}
await writeFile(process.argv[3],JSON.stringify(pages,null,2));
console.log(JSON.stringify({pages:pdf.numPages,metadata:await pdf.getMetadata(),firstPages:pages.slice(0,15)},null,2));await loadingTask.destroy();