pdf.js提取器 - pdf 文件未正确解析



我在节点cli脚本中使用pdf.js-extractor。我正试图提取一个问题和答案的数据库,在处理完文件后,该数据库将具有以下结构:

[
'324',
' ',
"Di quale dei seguenti arcipelaghi fa parte l'isola di ",
'Delle isole Ponziane',
' ',
'Delle isole Pelagie',
' ',
'Delle isole Egadi',
' ',
'Delle isole Eolie',
' ',
'C',
' '
],
[ 'Favignana?', ' ' ],
[
'325',
' ',
'Di quale di queste città la cattedrale di Santa Maria ',
'Napoli',
' ',
'Firenze',
' ',
'Roma',
' ',
'Genova',
' ',
'B',
' '
],
[ 'del Fiore è conosciuta semplicemente come il ' ],
[ 'Duomo?', ' ' ]

我注意到pdf内容以错误的方式分割,答案和正确的答案字母正确列出,但问题将以错误的方法显示。

每个问题的预期正确格式如下

[
'324',
"Di quale dei seguenti arcipelaghi fa parte l'isola di Favignana?",
'Delle isole Ponziane',
'Delle isole Pelagie',
'Delle isole Egadi',
'Delle isole Eolie',
'C', // correct answer letter
],
[
'325',
'Di quale di queste città la cattedrale di Santa Maria del Fiore è conosciuta semplicemente come il Duomo?',
'Napoli',
'Firenze',
'Roma',
'Genova',
'B', // this is the correct answer letter
]

我正在使用这个代码处理pdf

pdf.extract(pdfFile, {  
firstPage: 2,
normalizeWhitespace: true
}).then( (data) =>  {
//console.log(data);
spinner.stop();
data.pages.forEach( (page) => {
const lines = PdfExtract.utils.pageToLines(page, 1);
const rows = PdfExtract.utils.extractTextRows(lines);
fileContent.push(rows);
});
fileContent = fileContent.map( (row) => {
return row.join('');
});
console.log(fileContent);
}).catch( (error) => console.log(error) );

如何正确提取pdf内容并解决问题?

我认为问题出在异步代码上。

我这样转换了你的代码。如果你的pdf数据是正确的,这可能会解决问题

const data = await pdf.extract(pdfFile, {
firstPage: 2,
normalizeWhitespace: true
});
await spinner.stop();
for(var page of data.pages) {
const lines = await PdfExtract.utils.pageToLines(page, 1);
const rows = await PdfExtract.utils.extractTextRows(lines);
fileContent.push(rows);
}
fileContent = fileContent.map((row) => {
return row.join('');
});
console.log(fileContent);

最新更新