使用 pdf-parse 从 PDF 中提取文本时会跳过某些页面

问题描述 投票:0回答:1

我目前正在使用 pdf-parse(一个 Node.js 库)从 PDF 文件中提取文本。但是,我遇到了一个问题:在提取过程中某些页面被跳过。我检查了 PDF 文件,它似乎没有被加密或损坏。另外,当我使用 Mac PDF 查看器时,它会显示缺失的页面可搜索但不可扫描。

这是我正在使用的代码:

const pdfParse = require('pdf-parse');
const fs = require('fs');

// Read the PDF file
const pdfPath = 'path/to/pdf/file.pdf';
const pdfBuffer = fs.readFileSync(pdfPath);

// Parse the PDF
pdfParse(pdfBuffer).then((data) => {
  console.log(data.text);
}).catch((error) => {
  console.error('An error occurred:', error);
});

尽管运行上述代码,但在文本提取过程中仍会跳过 PDF 中的某些页面。我想知道什么可能导致此问题以及如何确保 pdf-parse 正确解析所有页面。任何有关解决此问题的见解或建议将不胜感激。谢谢!

我尝试使用上述代码从 PDF 的所有页面中提取文本,但我注意到有些页面被跳过。我希望 PDF 中的所有页面都被提取出来,并且我应该取回相应的文本。

javascript jquery node.js pdf pdf.js
1个回答
0
投票

尝试使用此模式逻辑:

const pdfParse = require('pdf-parse');
const fs = require('fs');

const pdfPath = 'path/to/your/pdf/file.pdf';
const pdfBuffer = fs.readFileSync(pdfPath);

pdfParse(pdfBuffer, {max: 1}).then(data => {
  data.pages.forEach((page, index) => {
    console.log(`Page ${index + 1}: ${page.text}`);
  });
}).catch(error => {
  console.error('An error occurred:', error);
});

我的代码仅在

pdf-parse
提供一种单独迭代页面的方法时才有效。

© www.soinside.com 2019 - 2024. All rights reserved.