npm-pdfreader
npm-pdfreader copied to clipboard
Make it async // return item as promise
Describe the bug We need to make the function async/await compatible. I need to return the result after the whole document is parsed.
To Reproduce List the steps you followed and/or share your code to help us reproduce the bug
Expected behavior Be able to await for the whole document to be parsed.
Screenshots, outputs or logs If applicable, add screenshots, outputs or logs to help explain your problem.
Desktop (please complete the following information):
Ubuntu 20.04
Additional context Add any other context about the problem here.
If I understand well, you can probably write your own function to achieve that. Something like:
import { PdfReader } from "pdfreader";
async function parsePdf(file) {
const items = [];
return new Promise((resolve, reject) => {
new PdfReader().parseFileItems("test/sample.pdf", (err, item) => {
if (err) reject(err);
else if (!item) resolve(items);
else if (item.text) items.push(item);
});
});
}
const items = await parsePdf("test.pdf");
Please let us know if that works for you.
Thanks for the reply! :D
Yes it is working but I had already found a way to return it as a Promise and make it async/await compatible.
The point is that this would be a great feature if the package would return the final result as a Promise by default!
See https://github.com/limulus/call-me-maybe for one potential solution.