我试图使用‘pdfjs’获取PDF文件第4页的内容。我试图将‘pdfjs’模块替换为'const =require(“pdfjs/es5/build/pdf”),但没有成功。
有什么问题吗?提前感谢!
const pdfjs = require('pdfjs-dist'); // Fetch PDF
async function getContent(src) {
const doc = await pdfjs.getDocument(src).promise // note the use of the property promise
const page = await doc.getPage(4)
return await page.getTextContent()
}
console.log(getContent('pdfs/Quantum.pdf'))发布于 2022-08-11 22:49:15
现在回答这些问题为时已晚,但让我补充一下对那些希望解决这个问题的人来说,这个解决方案对我来说是可行的。
这是可以正常工作的代码。
// Install the latest version of pdf.js via npm i pdfjs-dist
const pdfjsLib = require("pdfjs-dist/legacy/build/pdf.js");
let pdf_path = "{RELATIVE_PATH_TO_YOUR_FILE}/sample.pdf";
async function getContent(src: any){
const doc = await pdfjsLib.getDocument(src).promise;
const page = await doc.getPage(1);
const strings: any = await page.getTextContent();
let ITEMS_STRINGS = strings.items.map((item: any) => item.str);
let PDF_STRINGS: string = ITEMS_STRINGS.join(" ");
return PDF_STRINGS;
}
console.log(await getContent(`${pdf_path}`));这会很好的
https://stackoverflow.com/questions/69017293
复制相似问题