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

Update pdfjs to v4 with code for importing commonjs #957

Open
plocket opened this issue Nov 8, 2024 · 0 comments
Open

Update pdfjs to v4 with code for importing commonjs #957

plocket opened this issue Nov 8, 2024 · 0 comments
Labels
enhancement New feature or request reopen We won't be taking care of this any time soon.

Comments

@plocket
Copy link
Collaborator

plocket commented Nov 8, 2024

This is low priority because the current pdfjs vulnerabilities don't really apply to our situation. pdfjs v4 requires us to use import while the rest of the framework uses require. Here's one person's solution if I understand correctly.

async function importEsmModule<T>(name: string): Promise<T> {
  // FIXME: see https://stackoverflow.com/questions/65265420/how-to-prevent-typescript-from-transpiling-dynamic-imports-into-require
  // eslint-disable-next-line no-eval
  const module = eval(`(async () => {return await import("${name}")})()`);
  return module as T;
}

async function getPdf(params) {
  const { getDocument } = await importEsmModule<any>('pdfjs-dist/legacy/build/pdf.mjs');
  return getDocument(params)
}

I completely forgot how I came up with it, it's ugly as hell, but it works. And I only call the lib once so it's enough.

@plocket plocket added enhancement New feature or request reopen We won't be taking care of this any time soon. labels Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request reopen We won't be taking care of this any time soon.
Projects
None yet
Development

No branches or pull requests

1 participant