npm-pdfreader icon indicating copy to clipboard operation
npm-pdfreader copied to clipboard

Make it async // return item as promise

Open nikosgevre opened this issue 2 years ago • 4 comments

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.

nikosgevre avatar Dec 14 '22 10:12 nikosgevre

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.

adrienjoly avatar Dec 19 '22 08:12 adrienjoly

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!

nikosgevre avatar Dec 19 '22 10:12 nikosgevre

See https://github.com/limulus/call-me-maybe for one potential solution.

MikeRalphson avatar May 13 '23 07:05 MikeRalphson