Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it async // return item as promise #129

Open
nikosgevre opened this issue Dec 14, 2022 · 4 comments
Open

Make it async // return item as promise #129

nikosgevre opened this issue Dec 14, 2022 · 4 comments

Comments

@nikosgevre
Copy link

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.

@adrienjoly
Copy link
Owner

adrienjoly commented Dec 19, 2022

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.

@nikosgevre
Copy link
Author

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!

@MikeRalphson
Copy link

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

@abolajibisiriyu
Copy link

abolajibisiriyu commented Oct 14, 2024

import { DataEntry, PdfReader } from "pdfreader";

export async function parsePdf(file: string) {
    const items: DataEntry[] = [];

    return new Promise<DataEntry[]>((resolve, reject) => {
        new PdfReader().parseFileItems(file, (err, item) => {
            if (err) reject(err);
            else if (!item) resolve(items);
            else if (item) items.push(item);
        });
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants